------------恢復(fù)內(nèi)容開(kāi)始------------
create database 數(shù)據(jù)庫(kù)名
use 數(shù)據(jù)庫(kù)名
create table 表名 (
字段名1,類(lèi)型,約束
字段名2,類(lèi)型,約束
...
)
1)直接在建表時(shí)字段類(lèi)型后加 primary key
2)在表最后加 constraint 約束名 primary key(字段名)
3)表外修改 alter table 表名 add constraint 約束名 primary key(字段名)
1)直接在建表類(lèi)型后加 check(約束條件)
2)在表最后加 constraint 約束名 check(約束條件)
3)表外修改 alter table 表名 add constraint 約束名 check(約束條件)
注:mysql不支持檢查約束,但是寫(xiě)上檢查約束不會(huì)報(bào)錯(cuò)
1)直接在創(chuàng)建表的類(lèi)型后加 not null
2) 在表最后加入 constraint 約束名 check(字段名 is not null)
3)在表外修改 alter table 表名 modify 字段名 字段類(lèi)型 not null
1)直接在創(chuàng)建表的類(lèi)型后加 unique
2) 在表的最后加入 constraint 約束名 unqiue(字段名)
3) 在表外修改 alter table 表名 add constraint 約束名 unique(字段名)
1)直接在創(chuàng)建表的類(lèi)型后加 references 父表名(父表主鍵名)
2)在表的最后加入 constraint 約束名 foreign key(字段名) references 父表名(父表主鍵名)
3)在表外修改 alter table 表名 add constraint 約束名 foreign key(字段名) references 父表名(父表主鍵名)on delete set null on updata cascade
1)直接在創(chuàng)建表的類(lèi)型后加 default 默認(rèn)值
2)在表外修改 alter table 表名 add constraint 約束名
alter table 表名 drop constraint 約束名
1)添加字段
alter table 表名 add 字段名 字段類(lèi)型 注:在表中已經(jīng)有值時(shí),不能加非空約束
2)刪除字段
alter table 表名 drop 字段名
3)修改字段類(lèi)型
alter table 表名 modify 字段名 新字段類(lèi)型
4)修改字段名
alter table 表名 change 字段名 新字段名 字段類(lèi)型
5)修改表名
alter table 表名 rename as 新表名
6)刪除表
drop table 表名
show tables
聯(lián)系客服