詳解MySQL 外鍵約束
官方文檔:https://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html
1.外鍵作用:
MySQL通過外鍵約束來保證表與表之間的數(shù)據(jù)的完整性和準(zhǔn)確性。
2.外鍵的使用條件
兩個表必須是InnoDB表,MyISAM表暫時不支持外鍵(據(jù)說以后的版本有可能支持,但至少目前不支持) 外鍵列必須建立了索引,MySQL 4.1.2以后的版本在建立外鍵時會自動創(chuàng)建索引,但如果在較早的版本則需要顯示建立; 外鍵關(guān)系的兩個表的列必須是數(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 時使用,如果不指定CONSTRAINT symbol,MYSQL會自動生成一個名字。ON DELETE、ON UPDATE表示事件觸發(fā)限制,可設(shè)參數(shù):RESTRICT(限制外表中的外鍵改動)CASCADE(跟隨外鍵改動)SET NULL(設(shè)空值)SET DEFAULT(設(shè)默認(rèn)值)NO ACTION(無動作,默認(rèn)的)
CASCADE:表示父表在進行更新和刪除時,更新和刪除子表相對應(yīng)的記錄 RESTRICT和NO ACTION:限制在子表有關(guān)聯(lián)記錄的情況下,父表不能單獨進行刪除和更新操作 SET NULL:表示父表進行更新和刪除的時候,子表的對應(yīng)字段被設(shè)為NULL
4.案例演示
以CASCADE(級聯(lián))約束方式
1. 創(chuàng)建勢力表(父表)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. 參照完整性測試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);#插入失敗,因為country表中不存在id為4的勢力5. 約束方式測試insert into solider values(4,’瑪雅猛虎勇士’,2);#成功插入delete from country where id=2;#會導(dǎo)致solider表中id為2和4的記錄同時被刪除,因為父表中都不存在這個勢力了,那么相對應(yīng)的兵種自然也就消失了update country set id=8 where id=1;#導(dǎo)致solider表中country_id為1的所有記錄同時也會被修改為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. 參照完整性測試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);#插入失敗,因為country表中不存在id為4的勢力3. 約束方式測試insert into solider values(4,’西西里弓箭手’,3);#成功插入delete from country where id=3;#會導(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. 參照完整性測試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);#插入失敗,因為country表中不存在id為4的勢力3. 約束方式測試insert into solider values(4,’西歐騎士’,1);#成功插入delete from country where id=1;#發(fā)生錯誤,子表中有關(guān)聯(lián)記錄,因此父表中不可刪除相對應(yīng)記錄,即兵種表還有屬于西歐的兵種,因此不可單獨刪除父表中的西歐勢力update country set id=8 where id=1;#錯誤,子表中有相關(guān)記錄,因此父表中無法修改
以上就是詳解MySQL 外鍵約束的詳細(xì)內(nèi)容,更多關(guān)于MySQL 外鍵約束的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. mybatis plus代碼生成工具的實現(xiàn)代碼2. 讓SQL Server也能使用2G以上內(nèi)存3. Microsoft Office Access凍結(jié)字段的方法4. DB2 9(Viper)快速入門5. Microsoft Office Access隱藏和顯示字段的方法6. Access創(chuàng)建一個簡單MIS管理系統(tǒng)7. SQLSERVER 臨時表和表變量的區(qū)別匯總8. SQL Server數(shù)據(jù)庫連接查詢和子查詢實戰(zhàn)案例9. How to access eclipse workspace?10. SQL語句中的ON DUPLICATE KEY UPDATE使用
