詳解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ì)力表(父表)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)記錄,因此父表中無(wú)法修改
以上就是詳解MySQL 外鍵約束的詳細(xì)內(nèi)容,更多關(guān)于MySQL 外鍵約束的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. MySQL分區(qū)的優(yōu)點(diǎn)2. MYSQL(電話號(hào)碼,身份證)數(shù)據(jù)脫敏的實(shí)現(xiàn)3. 從舊版本SQL Server中重新存儲(chǔ)數(shù)據(jù)4. Mysql入門系列:MYSQL表達(dá)式求值和MYSQL類型轉(zhuǎn)換5. mysql-bin.000001文件的來(lái)源及處理方法6. 如何實(shí)現(xiàn)MySQL數(shù)據(jù)庫(kù)的備份與恢復(fù)7. 什么是Access數(shù)據(jù)庫(kù)8. MySQL 字符串函數(shù):字符串截取9. Oracle根據(jù)逗號(hào)拆分字段內(nèi)容轉(zhuǎn)成多行的函數(shù)說(shuō)明10. MySql遠(yuǎn)程連接的實(shí)現(xiàn)方法

網(wǎng)公網(wǎng)安備