MySQL中簡(jiǎn)單的數(shù)據(jù)操縱語言(DML),數(shù)據(jù)定義語言(DDL)
數(shù)據(jù)操縱語言(DML-Data Manipulation Language)
代表關(guān)鍵字:insert,delete,update
數(shù)據(jù)定義語言(DDL-Data Definition Language)
代表關(guān)鍵字:create ,drop,alter
1、創(chuàng)建表前,判斷表是否存在,如果存在即刪除
drop table if exists t_表名;//最好用t_命名表
2、創(chuàng)建表,create table
create table t_表名();
3、常用數(shù)據(jù)類型
3.1、字符串
Char(長(zhǎng)度)//定長(zhǎng)字符串,存儲(chǔ)空間大小固定,適合作為主鍵或外鍵
Varchar(長(zhǎng)度)//可變長(zhǎng)字符串,存儲(chǔ)空間等于實(shí)際數(shù)據(jù)空間
3.2、數(shù)值類型
Double(有效數(shù)字位數(shù),小數(shù)位)//數(shù)值型
Float(有效數(shù)字位數(shù),小數(shù)位)//數(shù)值型
Int(長(zhǎng)度)//整型
Bigint(長(zhǎng)度)//長(zhǎng)整型
3.3、日期和時(shí)間類型
Date//日期型 年月日
Time//時(shí)間值或持續(xù)時(shí)間
4、建表
drop table if exist t_user;
create table t_user(
user_id int(10),//id為數(shù)字
user_name varchar(20),//姓名可變長(zhǎng)度
sex char(2),//性別定長(zhǎng)節(jié)省空間
birthday date,
email varchar(30),
);
5、修改表結(jié)構(gòu)
alter table t_user add tel char(11);//增加手機(jī)號(hào)碼
alter table t_student modify student_name varchar(100) ;//modify修改姓名長(zhǎng)度
6、插入數(shù)據(jù)
insert into t_表名(結(jié)構(gòu)字段) values (數(shù)值)
insert into t_user(user_id, user_name, birthday, email)
values
(001, 'shitou', '1993-03-11', 'zhengyubio@126.com')
7、修改數(shù)據(jù)
update 表名 set 字段名稱1=需要修改的值1, 字段名稱2=需要修改的值2 where ...
8、刪除數(shù)據(jù)
delete from 表名 where...