詳解MySQL 外鍵約束
官方文檔:
https://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html
1.外鍵作用:
MySQL通過(guò)外鍵約束來(lái)保證表與表之間的數(shù)據(jù)的完整性和準(zhǔn)確性。
2.外鍵的使用條件
- 兩個(gè)表必須是InnoDB表,MyISAM表暫時(shí)不支持外鍵(據(jù)說(shuō)以后的版本有可能支持,但至少目前不支持)
- 外鍵列必須建立了索引,MySQL 4.1.2以后的版本在建立外鍵時(shí)會(huì)自動(dòng)創(chuàng)建索引,但如果在較早的版本則需要顯示建立;
- 外鍵關(guān)系的兩個(gè)表的列必須是數(shù)據(jù)類型相似,也就是可以相互轉(zhuǎn)換類型的列,比如int和tinyint可以,而int和char則不可以。
3.創(chuàng)建語(yǔ)法
[CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (col_name, ...)
REFERENCES tbl_name (col_name,...)
[ON DELETE reference_option]
[ON UPDATE reference_option]reference_option:
RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT該語(yǔ)法可以在 CREATE TABLE 和 ALTER TABLE 時(shí)使用,如果不指定CONSTRAINT symbol,MYSQL會(huì)自動(dòng)生成一個(gè)名字。
ON DELETE、ON UPDATE表示事件觸發(fā)限制,可設(shè)參數(shù):
RESTRICT(限制外表中的外鍵改動(dòng))
CASCADE(跟隨外鍵改動(dòng))
SET NULL(設(shè)空值)
SET DEFAULT(設(shè)默認(rèn)值)
NO ACTION(無(wú)動(dòng)作,默認(rèn)的)CASCADE:表示父表在進(jìn)行更新和刪除時(shí),更新和刪除子表相對(duì)應(yīng)的記錄
RESTRICT和NO ACTION:限制在子表有關(guān)聯(lián)記錄的情況下,父表不能單獨(dú)進(jìn)行刪除和更新操作
SET NULL:表示父表進(jìn)行更新和刪除的時(shí)候,子表的對(duì)應(yīng)字段被設(shè)為NULL
4.案例演示
以CASCADE(級(jí)聯(lián))約束方式
1. 創(chuàng)建勢(shì)力表(父表)country create table country ( id int not null, name varchar(30), primary key(id) ); 2. 插入記錄 insert into country values(1,'西歐'); insert into country values(2,'瑪雅'); insert into country values(3,'西西里'); 3. 創(chuàng)建兵種表(子表)并建立約束關(guān)系 create table solider( id int not null, name varchar(30), country_id int, primary key(id), foreign key(country_id) references country(id) on delete cascade on update cascade, ); 4. 參照完整性測(cè)試 insert into solider values(1,'西歐見(jiàn)習(xí)步兵',1); #插入成功 insert into solider values(2,'瑪雅短矛兵',2); #插入成功 insert into solider values(3,'西西里諾曼騎士',3) #插入成功 insert into solider values(4,'法蘭西劍士',4); #插入失敗,因?yàn)閏ountry表中不存在id為4的勢(shì)力 5. 約束方式測(cè)試 insert into solider values(4,'瑪雅猛虎勇士',2); #成功插入 delete from country where id=2; #會(huì)導(dǎo)致solider表中id為2和4的記錄同時(shí)被刪除,因?yàn)楦副碇卸疾淮嬖谶@個(gè)勢(shì)力了,那么相對(duì)應(yīng)的兵種自然也就消失了 update country set id=8 where id=1; #導(dǎo)致solider表中country_id為1的所有記錄同時(shí)也會(huì)被修改為8
以SET NULL約束方式
1. 創(chuàng)建兵種表(子表)并建立約束關(guān)系 drop table if exists solider; create table solider( id int not null, name varchar(30), country_id int, primary key(id), foreign key(country_id) references country(id) on delete set null on update set null, ); 2. 參照完整性測(cè)試 insert into solider values(1,'西歐見(jiàn)習(xí)步兵',1); #插入成功 insert into solider values(2,'瑪雅短矛兵',2); #插入成功 insert into solider values(3,'西西里諾曼騎士',3) #插入成功 insert into solider values(4,'法蘭西劍士',4); #插入失敗,因?yàn)閏ountry表中不存在id為4的勢(shì)力 3. 約束方式測(cè)試 insert into solider values(4,'西西里弓箭手',3); #成功插入 delete from country where id=3; #會(huì)導(dǎo)致solider表中id為3和4的記錄被設(shè)為NULL update country set id=8 where id=1; #導(dǎo)致solider表中country_id為1的所有記錄被設(shè)為NULL
以NO ACTION 或 RESTRICT方式 (默認(rèn))
1. 創(chuàng)建兵種表(子表)并建立約束關(guān)系 drop table if exists solider; create table solider( id int not null, name varchar(30), country_id int, primary key(id), foreign key(country_id) references country(id) on delete RESTRICT on update RESTRICT, ); 2. 參照完整性測(cè)試 insert into solider values(1,'西歐見(jiàn)習(xí)步兵',1); #插入成功 insert into solider values(2,'瑪雅短矛兵',2); #插入成功 insert into solider values(3,'西西里諾曼騎士',3) #插入成功 insert into solider values(4,'法蘭西劍士',4); #插入失敗,因?yàn)閏ountry表中不存在id為4的勢(shì)力 3. 約束方式測(cè)試 insert into solider values(4,'西歐騎士',1); #成功插入 delete from country where id=1; #發(fā)生錯(cuò)誤,子表中有關(guān)聯(lián)記錄,因此父表中不可刪除相對(duì)應(yīng)記錄,即兵種表還有屬于西歐的兵種,因此不可單獨(dú)刪除父表中的西歐勢(shì)力 update country set id=8 where id=1; #錯(cuò)誤,子表中有相關(guān)記錄,因此父表中無(wú)法修改
以上就是詳解MySQL 外鍵約束的詳細(xì)內(nèi)容,更多關(guān)于MySQL 外鍵約束的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- MySQL外鍵約束的刪除和更新總結(jié)
- MySQL主鍵約束和外鍵約束的實(shí)現(xiàn)
- MySQL主鍵約束和外鍵約束詳解
- MySQL多表操作的外鍵約束教程
- MySQL外鍵約束(Foreign?Key)案例詳解
- Mysql外鍵約束的創(chuàng)建與刪除的使用
- mysql增加外鍵約束具體方法
- Mysql關(guān)于數(shù)據(jù)庫(kù)是否應(yīng)該使用外鍵約束詳解說(shuō)明
- MySQL外鍵約束(FOREIGN KEY)案例講解
- MySQL 外鍵約束和表關(guān)系相關(guān)總結(jié)
- MySQL外鍵約束的實(shí)例講解
- MySQL外鍵約束(FOREIGN KEY)的具體使用
相關(guān)文章
Myeclipse連接mysql數(shù)據(jù)庫(kù)心得體會(huì)
這篇文章主要為大家詳細(xì)介紹了MyEclipse連接MySQL數(shù)據(jù)庫(kù)圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
如何將Excel文件導(dǎo)入MySQL數(shù)據(jù)庫(kù)
這篇文章主要為大家詳細(xì)介紹了Excel文件導(dǎo)入MySQL數(shù)據(jù)庫(kù)的具體方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
超簡(jiǎn)單的qps統(tǒng)計(jì)方法(推薦)
下面小編就為大家?guī)?lái)一篇超簡(jiǎn)單的qps統(tǒng)計(jì)方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
MySQL兩個(gè)查詢?nèi)绾魏喜⒊梢粋€(gè)結(jié)果詳解
利用union關(guān)鍵字,可以給出多條select語(yǔ)句,并將它們的結(jié)果組合成單個(gè)結(jié)果集,下面這篇文章主要給大家介紹了關(guān)于MySQL兩個(gè)查詢?nèi)绾魏喜⒊梢粋€(gè)結(jié)果的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
Mysql-Insert插入過(guò)慢的原因記錄和解決方案
這篇文章主要介紹了Mysql-Insert插入過(guò)慢的原因記錄和解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08

