MySQL 外來鍵約束

2021-08-07 04:52:55 字數 4029 閱讀 1569

**:

如果表a的主關鍵字是表b中的字段,則該字段稱為表b的外來鍵,表a稱為主表,表b稱為從表。

外來鍵是用來實現參照完整性的,不同的外來鍵約束方式將可以使兩張表緊密的結合起來,

特別是修改或者刪除的級聯操作將使得日常的維護工作更加輕鬆。

外來鍵主要用來保證資料的完整性和一致性。

兩個表必須是innodb表,myisam表暫時不支援外來鍵

外來鍵列必須建立了索引,mysql 4.1

.2以後的版本在建立外來鍵時會自動建立索引,但如果在較早的版本則需要顯示建立;

外來鍵關係的兩個表的列必須是資料型別相似,也就是可以相互轉換型別的列,比如int和tinyint可以,而int和char則不可以;

建立外來鍵語法:

**如下

[constraint [symbol]] foreign key

[index_name] (index_col_name, ...)

references tbl_name (index_col_name,...)

[on delete reference_option]

[on update reference_option]

reference_option:

restrict | cascade | set null | no action

如果子表試圖建立乙個在父表中不存在的外鍵值,innodb會拒絕任何insert或update操作。如果父表試圖update或者delete任何子表中存在或匹配的外鍵值,最終動作取決於外來鍵約束定義中的on update和on delete選項。innodb支援5種不同的動作,如果沒有指定on delete或者on update,預設的動作為restrict:

cascade: 從父表中刪除或更新對應的行,同時自動的刪除或更新自表中匹配的行。on delete canscade和on update canscade都被innodb所支援。

set null: 從父表中刪除或更新對應的行,同時將子表中的外來鍵列設為空。注意,這些在外鍵列沒有被設為not null時才有效。on delete set null和on update set set null都被innodb所支援。

no action: innodb拒絕刪除或者更新父表。

restrict: 拒絕刪除或者更新父表。指定restrict(或者no action)和忽略on delete或者on update選項的效果是一樣的。

set default: innodb目前不支援。

外來鍵約束使用最多的兩種情況:

1)父表更新時子表也更新,父表刪除時如果子表有匹配的項,刪除失敗;

2)父表更新時子表也更新,父表刪除時子表匹配的項也刪除。

前一種情況,在外鍵定義中,我們使用on update

cascade

ondelete

restrict;

後一種情況,可以使用on

update

cascade

ondelete

cascade。

使用案例:

(1)建立表:

**如下

create

table

ifnot

exists

`article` (

`id`

int(11) not

null auto_increment,

`category_id`

int(11) not

null,

`name`

char(16) not

null,

primary

key (`id`),

key`fk_1` (`category_id`)

) engine=innodb default charset=utf8 auto_increment=2 ;

insert

into

`article` (`id`, `category_id`, `name`) values

(1, 1, '文章1');

create

table

ifnot

exists

`category` (

`id`

int(11) not

null auto_increment,

`name`

char(16) not

null,

primary

key (`id`)

) engine=innodb default charset=utf8 auto_increment=2 ;

insert

into

`category` (`id`, `name`) values

(1, '分類1');

建立外來鍵約束:

**如下

alter

table

`article`

addconstraint

`fk_1`

foreign

key (`category_id`) references

`category` (`id`);

(2)刪除主表category中資料:delete

from

`category`

where id=1,會報錯:

#1451 - cannot delete

orupdate a parent row: a foreign

keyconstraint fails (`test`.`article`, constraint

`fk_1`

foreign

key (`category_id`) references

`category` (`id`))

(3)從表article中,新增不存在的category_id:insert

into article(category_id,name) values(2,'分類2') 會報錯:

#1452 - cannot add

orupdate a child row: a foreign

keyconstraint fails (`test`.`article`, constraint

`fk_1`

foreign

key (`category_id`) references

`category` (`id`))

(4)更改更新刪除約束

**如下

--刪除外來鍵

alter

table article drop

foreign

key fk_1

--新增外來鍵

alter

table

`article`

addconstraint

`fk_1`

foreign

key ( `category_id` )

references

`category` ( `id` )

ondelete

cascade

onupdate

cascade

此時如下操作:

**如下

--此時article中的記錄也會被刪除

delete

from category where id=1;

--此時article中的category_id也會被更新成3

update

`test`.`category`

set`id` = '3'

where

`category`.`id` =2;

mysql約束與外來鍵 MySQL 外來鍵與約束

外來鍵的建立 建表如下 create table parent id int not null,primary key id type innodb create table child id int,parent id int,foreign key parent id references pa...

MySQL 外來鍵約束

建立測試主表.id 是主鍵.create table test main id int,value varchar 10 primary key id 建立測試子表.create table test sub id int,main id int,value varchar 10 primary k...

MySQL外來鍵約束

innodb型別表有乙個其他儲存引擎不支援的特性 外來鍵約束。當b表的外來鍵關聯到a表的主鍵時 表b是父表a表的子表 如果刪除了a表,那麼b中的外來鍵則仍然關聯著乙個不存在的表的主鍵。而外鍵約束則設定了當約束被破壞時應該應用的的規則,包括防止約束破壞。建立乙個外來鍵約束的語法是 foreign ke...