MySQL 表約束

約束概述

對數據表中數據的限制條件叫表的約束,目的是爲了保證表中記錄的完整和有效。例如非空、惟一等。mysql

查看約束

1 經過查看建表語句 查看錶中的約束sql

show create table tb_name;

2 經過檢查約束表 查看約束數據庫

select * from information_schema.table_constraints where table_name='usr';

Tips:在MySQL數據庫中 全部的約束都存放在information_schema.table_constraints表中~~~~參看~~~~~information_schema數據庫介紹django

主鍵約束

什麼是主鍵約束

在MySQL中主鍵是一種約束,也是一種索引。被設爲主鍵的字段 要求 不重複 不能爲空 不能缺省。主鍵的做用爲惟一的肯定表中的一條記錄。須要注意的是 當主鍵是複合主鍵時,不重複的含義是 組合起來不重複便可,單列值容許重複,但不容許同時重複code

建立主鍵

1 在字段中定義主鍵

create table if not exists user(
    id int(11) auto_increment primary key,
  	name char(64) not null default 'No Name'
);

2 字段定義結束後定義主鍵

create table if not exists user(
		id int(11) auto_increment,
  	name char(64) not null default 'No Name',
  	primary key(id,name)
);
-- 注意 聲明主鍵的完整格式應該是這樣的
-- constraint 約束名 primary key key_name

3 建表完成後添加主鍵

上邊的兩種方式都是在建立表時候指定主鍵約束,也能夠在表建成後添加主鍵約束orm

alter table tb_name add primary key(key_name)
-- tb_name   表名
-- primary key() 括號裏填寫字段名,能夠是一個 也能夠是多個(複合主鍵)

刪除主鍵

-- 若是主鍵字段中包含有自增 auto_increment,須要先刪除自增屬性 在刪除主鍵
-- 若是沒有自增直接刪除主鍵便可

-- 刪除自增類型。 !!!注意 自增是類型 因此 可使用 修改字段的類型就ok

--1 修改字段的名 修改後須要從新指定類型
alter table tb_name change col_name col_name int;

--2 簡單的直接修改類型
alter table tb_name modify col_name int;

-- 刪除掉 auto_increment屬性後能夠刪除主鍵
alter table tb_name drop primary key;

主鍵的分類

1 單一主鍵

建立 1 中 建立的 id 就是單一的主鍵,須要注意,定義字段時聲明主鍵是沒法建立複合主鍵的。索引

2 複合主鍵

建立 2 中的建立主鍵的方式 容許建立單一主鍵也容許建立複合主鍵,推薦使用這種方法進行主鍵的建立。ip

3 聯合主鍵

當處理多對多的關係時,須要建立中間表來進行多對多的關聯,此時中間表的主鍵成爲聯合主鍵,他能惟一的肯定多對多的若干實體間的對應關係。聯合主鍵也能夠是複合主鍵即聯合主鍵由第三表的多個列複合而成。保持惟一。rem

外鍵約束

什麼是外鍵

外鍵依託於具備惟一約束的字段,通常來說主鍵會多一些,可是也能夠不是主鍵。前提是它們具備惟一約束。外鍵依賴的字段能夠來自本(自參照表)也能夠是其餘表,通常的是其餘表的狀況多一些。此時要求 主表必須已經存在於數據庫中,或者是當前正在建立的表。若是被依賴字段在本表中,那麼這樣的約束結構稱爲 自參照完整性。文檔

Tips: 雖然主鍵不容許爲空不容許重複,可是外鍵能夠爲空,也能夠重複。外鍵約束要求每個值都來自於主鍵,特別的插入空值時,MySQL不支持缺省,必須顯式表達出來,例如

insert into user(name,leader_id) values('monkey',null);  
-- 顯示的寫出 null 不然 會報錯
insert into user(name,leader_id) values('monkey');  
-- 是不合法的語句
  • 在父表的表名後面指定列名或列名的組合。這個列或列的組合必須是父表的主鍵或候選鍵。
  • 外鍵中列的數目必須和父表的主鍵中列的數目相同。
  • 外鍵中列的數據類型必須和父表主鍵中對應列的數據類型相同。

建立外鍵

1 建表時建立外鍵
[constraint <fk_name>] foreign key(fk_col_name [,col_name2,…])
references <tb_name> (pk_col_name1 [,pk_col_name2,…])
-- fk_name				外鍵名
-- fk_col_name		設置外鍵字段的字段名
-- tb_name				外鍵關聯的主表表名
-- pk_col_name 		主表被依賴的字段名
2 建表完成後建立
alter table tb_name add [constriant <fk_name>] foreign key(fk_name) references tb_name(pk_col_name)
-- fk_name				外鍵名
-- fk_col_name		設置外鍵字段的字段名
-- tb_name				外鍵關聯的主表表名
-- pk_col_name 		主表被依賴的字段名

例子

-- 建立主表 leader
create table leader(
  	id int(11) primary key auto_increment, 
  	name char(64) not null
);

-- 建立外鍵所在表 employee
create table employee(
  	id int(11) primary key auto_increment, 
  	name char(64) not null,
  	leader_id int(11),
  	constraint fk_leader_employee foreign key(leader_id) references leader(id)
);
create table employee(
  	id int(11) primary key auto_increment, 
  	tid char(11),
  	name char(64) not null,
  	leader_id int(11),
  	constraint fk_leader_employee foreign key(name) references leader(name)
);
注意
  • 外鍵 和 關聯的主表字段類型必須一致,可是容許size不一致,可是這樣可能會出現沒法預期的問題,不推薦這樣作,曾經在使用django的ORM時,手寫了migrations文件,致使出現相似狀況,很大的坑。
  • 外鍵字段能夠是主表的多個字段(複合主鍵或者其餘的狀況都是被容許的,可是此時要保證關聯字段和被關聯字段要能對應起來)
  • 特別要說明的是,做爲其餘表外鍵的主表字段必須是具備惟一約束(主鍵字段原生就自帶 unique 屬性,若是不是主鍵,那必需要手動的設置unique 當是外鍵是多個字段時,須要設置複合惟一

刪除外鍵

同刪除主鍵相似,不一樣的是,每張表只有一個主鍵,所以不須要指定主鍵名,可是外鍵 外鍵名是不虛指定的。

alter table tb_name drop foreign key fk_name;
-- fk_name  			外鍵約束名 也就是 constrain 後面的名字

惟一約束

什麼是惟一約束

惟一約束就是要求字段中的值是惟一,特別的它容許爲空,但只能出現一個空值。惟一約束能夠確保一列不出現重複值,或者若干列不出現重複的組合。

建立惟一約束

1 字段定義時添加

create table leader(
  	id int(11) primary key auto_increment,
  	name char(64) unique,
);
-- name 字段惟一 ,不容許有重名的出現

2 字段定義完成後添加

create table leader(
  	id int(11) primary key auto_increment, 
  	name char(64),
  	constraint uq_name unique key(name)    -- 在這裏寫惟一約束 與主鍵相似
);

注意

  • 惟一約束和主鍵約束的區別在於主鍵中不準出現null 可是惟一 要求只能出現一個空值
  • 一張表中能夠有若個個惟一約束,可是隻能存在一個主鍵約束

3 建表完成後添加

alter table tb_name add [constaint uq_name] unique key(uq_col_name)
-- tb_name 				要修改的表名
-- uq_name 				約束名<索引名>
-- uq_col_name 		聲明惟一的字段名
-- 約束名 能夠不寫

刪除惟一約束

alter table tb_name drop index fk_name;
-- tb_name 			表名
-- fk_name    	約束名<索引名>

默認值約束和非空約束

default 應該被稱爲默認值約束,他也是一種約束,做用和簡單,指定某字段的默認值

建立默認值約束

1 字段中定義默認值/非空

create table user(
		id int(11) auto_increment primary key,
  	age int(4) default 18,
  	name char(64) not null
);

2 表建成後添加

alter table tb_name change col_name col_type[size] default <默認值>
-- 就是簡單的修改表的結構語句
alter table user change col_name col_type[size] {default 默認值 | not null | null }

刪除默認值/非空約束

-- 修改表結構 將默認值 設置爲null
alter table tb_name change col_name col_type[size] default null;
-- 或者
-- 修改表結構 修改字段的類型
alter table tb_name modify col_name col_type[size];

檢查約束

MySQL不支持檢查約束,官方文檔上說,即便設置了檢查約束,可是mysql不會強制的要求這樣作。建立以後在information_schema.table_constraints 找不到相關的記錄。甚至他可能自己就不存在。

什麼是檢查約束

檢查約束通常來講是爲了知足用戶實際的實體完整性而存在的。例如 身高應該 high > 10 and high < 280

建立檢查約束

1 建立表時添加

check <約束條件>
-- 例子
create table userinfo(
		id int(11) auto_increment primary key,
  	high int(4) default 160,
  	weight int(4) default 50,
  	check(heigh > 10 and heigh < 280)
);

2 建立完成後添加

alter table userinfo add constraint check_name check(約束條件)
-- 例如
alter table userinfo add constraint check_weight check(weight > 10 and weight < 300);

刪除檢查約束

alter table tb_name drop constraint check_name;
-- 例如
alter table userinfo drop constraint check_weight;
-- 值得注意的是,mysql 雖說建立成功了,可是在information_schema 數據庫的 table_constraints 表中並不能找到相關的信息。MySQL不支持檢查約束。
相關文章
相關標籤/搜索