詳解MySQL 外鍵約束
官方文檔:https://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html
1.外鍵作用:
MySQL通過外鍵約束來保證表與表之間的數(shù)據(jù)的完整性和準(zhǔn)確性。
2.外鍵的使用條件
兩個(gè)表必須是InnoDB表,MyISAM表暫時(shí)不支持外鍵(據(jù)說以后的版本有可能支持,但至少目前不支持) 外鍵列必須建立了索引,MySQL 4.1.2以后的版本在建立外鍵時(shí)會(huì)自動(dòng)創(chuàng)建索引,但如果在較早的版本則需要顯示建立; 外鍵關(guān)系的兩個(gè)表的列必須是數(shù)據(jù)類型相似,也就是可以相互轉(zhuǎn)換類型的列,比如int和tinyint可以,而int和char則不可以。3.創(chuàng)建語法
[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
該語法可以在 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(無動(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ì)力表(父表)countrycreate 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,’西歐見習(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,’西歐見習(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è)為NULLupdate 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,’西歐見習(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)記錄,因此父表中無法修改
以上就是詳解MySQL 外鍵約束的詳細(xì)內(nèi)容,更多關(guān)于MySQL 外鍵約束的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 50種方法巧妙優(yōu)化你的SQL Server數(shù)據(jù)庫2. mysql like語句問題3. 恢復(fù)誤刪數(shù)據(jù)(SQL Server 2000)--Log Explorer4. 解決mybatis一對(duì)多關(guān)聯(lián)查詢多條數(shù)據(jù)只顯示一條的問題5. 用SQL Server為Web瀏覽器提供圖像(三)(轉(zhuǎn))6. Mysql入門系列:MYSQL創(chuàng)建、刪除和選擇數(shù)據(jù)庫7. mybatis 實(shí)現(xiàn)多條update同時(shí)執(zhí)行8. MySQL公司CEO采訪 MySQL如何從開源中獲利9. MySQL 8.0.19安裝詳細(xì)教程(windows 64位)10. 用SQL Server為Web瀏覽器提供圖像(四)(轉(zhuǎn))
