約束語句:
not null 非空 default 默認約束語句 unique 唯一約束語句 primary 主鍵 = 唯一 + 非空 auto_increment 自動增長 foreign key(從表id) reference 主表名(id); 表與表之間建立聯系
primary key 主鍵
特點: (1) 不能重復,唯一的特性 (2) 有主鍵,則有索引,效率更高 (3) 可以建立關聯 (4) 每一張表必須有一個主鍵 (5) 一般把Id 作為主鍵 語法:create table t1 (id int primary key,name char(10)); 多個主鍵:create table t1(id int,name char(10),primary key(id name));
auto_increment 自動增長
特點; 一般與 primary key 連在一起用 只能加整形 插入值時,可以不傳,也可以傳null 語法: 單列 create table t1 (id int primary key); 多列 create table t1 (id int,name char(5),primary key(id name));
foreign key(從表id)references 主表名(主表id)
表與表之間建立聯系 級聯: 語法:在創建外鍵時 在后面添加 on update cascade 同步更新 on delete cascade 同步刪除