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