mysql語句之約束語句


約束語句:

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   同步刪除

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM