欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解MySQL 外鍵約束

 更新時間:2020年08月07日 10:07:17   作者:MySQL技術(shù)  
這篇文章主要介紹了MySQL 外鍵約束的相關(guān)資料,幫助大家更好的理解和學習MySQL,感興趣的朋友可以了解下

官方文檔:
https://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html

1.外鍵作用:

MySQL通過外鍵約束來保證表與表之間的數(shù)據(jù)的完整性和準確性。

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è)默認值)
NO ACTION(無動作,默認的)

CASCADE:表示父表在進行更新和刪除時,更新和刪除子表相對應的記錄
RESTRICT和NO ACTION:限制在子表有關(guān)聯(lián)記錄的情況下,父表不能單獨進行刪除和更新操作
SET NULL:表示父表進行更新和刪除的時候,子表的對應字段被設(shè)為NULL

4.案例演示

以CASCADE(級聯(lián))約束方式

1. 創(chuàng)建勢力表(父表)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. 參照完整性測試
insert into solider values(1,'西歐見習步兵',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;
#會導致solider表中id為2和4的記錄同時被刪除,因為父表中都不存在這個勢力了,那么相對應的兵種自然也就消失了
update country set id=8 where id=1;
#導致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,'西歐見習步兵',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;
#會導致solider表中id為3和4的記錄被設(shè)為NULL
update country set id=8 where id=1;
#導致solider表中country_id為1的所有記錄被設(shè)為NULL

以NO ACTION 或 RESTRICT方式 (默認)

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,'西歐見習步兵',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)記錄,因此父表中不可刪除相對應記錄,即兵種表還有屬于西歐的兵種,因此不可單獨刪除父表中的西歐勢力
update country set id=8 where id=1;
#錯誤,子表中有相關(guān)記錄,因此父表中無法修改

以上就是詳解MySQL 外鍵約束的詳細內(nèi)容,更多關(guān)于MySQL 外鍵約束的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論