1. 程式人生 > 實用技巧 >MySQL 之 表的完整性約束

MySQL 之 表的完整性約束

為了防止不符合規範的資料進入資料庫,在使用者對資料進行插入、修改、刪除等操作時,DBMS自動按照一定的約束條件對資料進行監測,使不符合規範的資料不能進入資料庫,以確保資料庫中儲存的資料正確、有效、相容。

約束條件與資料型別的寬度一樣,都是可選引數,主要分為以下幾種:

NOT NULL :      # 非空約束,指定某列不能為空;  
UNIQUE : 	    # 唯一約束,指定某列或者幾列組合不能重複
PRIMARY KEY :  # 主鍵,指定該列的值可以唯一地標識該列記錄
FOREIGN KEY :  # 外來鍵,指定該行記錄從屬於主表中的一條記錄,主要用於參照完整性

1、not null 非空約束

create table t1(id int not null,name char(12));     # 數字型設定為非空 當不寫時 預設插入0
create table t2(id int,name char(12)  not null);    # 字串設定為非空 當不寫時 預設插入空字串
# 設定嚴格模式:
    不支援對not null欄位插入null值
    不支援對自增長欄位插入”值
    不支援text欄位有預設值

直接在mysql中生效(重啟失效):
mysql>set sql_mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION";

配置檔案新增(永久失效):
sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

2、default 設定預設值

​ 建立列時可以指定預設值,當插入資料時如果未主動設定,則自動新增預設值。

create table t3(id int,name char(12),sex enum('male','female') default 'male'); #設定性別的預設值為男

3、非空約束 和 預設值

create table t3(id int not null,name char(12) not null,sex enum('male','female') not null default 'male');

4、unique 唯一約束(值不能重複)

create table t5(id int unique, family char(12),name char(12));     # id 值唯一

5、聯合唯一

create table t5(family char(12),name char(12),unique(family,name));  # 聯合唯一(family 與 name 組合起來唯一)
create table t5(family char(12) not null,name char(12) not null,unique(family,name));  # 約束各自不能為空 且聯合唯一

6、唯一 + 非空

​ 第一個被設定了非空 + 唯一約束會被定義成主鍵 primary key, 主鍵在整張表中只能有一個

create table t6(id int not null unique, name char(12) not null unique);

7、primary key 主鍵

​ 主鍵為了保證表中的每一條資料的該欄位都是表格中的唯一值,也就是,主鍵能夠用來獨一無二的確認一個表格中的每一行資料。

​ 主鍵可以包含一個欄位或多個欄位,當主鍵包含多個欄位時,稱為組合鍵,也就是聯合主鍵。

​ 主鍵必須唯一,主鍵值非空;可以是單一欄位,也可以是多欄位組合。

create table t6(id int primary key, name char(12) not null unique);
create table t5(family char(12) ,name char(12),primary key(family,name));  # 約束各自不能為空 且聯合唯一 還佔用了整張表的主鍵

8、auto_increment (自動增加) 對某一列設定自增

create table t6(id int auto_increment, name char(12));   # 會發生報錯, 因為 auto_increment 得在key情況下使用(unique 和 primary key)
create table t7(id int unique auto_increment, name char(12) primary key) ;  # 設定id自增,並設定name為主鍵
create table t8(id int primary key auto_increment, name char(12)) ;   # 將id設定為主鍵,並設定它為自增
create table t9(id int unique auto_increment, name char(12)) auto_increment=100000;
# 設定id自增,且從100000開始自增

(1)、正常所有的操作都無法改變auto_increment 的自動計數,也沒有必要去改變它

delete from t7;     # 清空表資料但卻不能重置auto_increment計數
truncate table t7;  # 清空表並且重置auto_increment計數

(2)、 修改 auto_increment

alter table 表名 auto_increment = n;   # 修改表的auto_increment
alter table t7 auto_increment = 1000;  # 修改t7表的auto_increment自增從1000開始

9、foreign key 外來鍵

(1)、有無外來鍵的區別

1、沒有建立外來鍵時:
    create table stu(id int,name char(12),class_id int);
    create table class(cid int,cname char(12));
    insert into stu values (1,'cai',1),(2,'yong',1);
    insert into class values(1,'python');
    insert into class values(2,'java');
    select * from stu,class where class_id = cid;
    delete from stu where id = 1;
    delete from class where cid = 1;
  # 總結:在沒有建立外來鍵時,資料可以隨意的進行增刪改
2、建立外來鍵 stu2 class2:
    create table class2(cid int unique,cname char(12));
    create table stu2(id int,name char(12),class_id int,foreign key(class_id) references class2(cid));   # 將表class2的cid設定為表stu2的外來鍵
    insert into class2 values(1,'python');
    insert into stu2 values (1,'cai',1),(2,'yong',1);
    delete from class2 where cid = 1;
    insert into class2 values(2,'java');
    update class2 set cid = 1 where cid = 2;  # 不能修改
  # 總結:有了外來鍵約束之後不能再進行隨意的增刪改

(2)、級聯更新

1、stu3 class3 級聯更新:
    create table class3(cid int primary key,cname char(12));
    create table stu3(id int,name char(12),class_id int,foreign key(class_id) references class3(cid) on update cascade);   # 將表class3中的cid設定為表stu3的外來鍵,並建立級聯更新,可以同時進行增刪改
    insert into class3 values(1,'python');
    insert into stu3 values (1,'cai',1),(2,'yong',1);
    update class3 set cid = 2;  # 修改了class3中的cid,stu3中相關的資料也會跟著變化,是on update cascade設定導致的