MySQL中外鍵設定詳解

2021-07-10 08:41:56 字數 3827 閱讀 5878

mysql外來鍵設定詳解

(1) 外來鍵的使用:

外來鍵的作用,主要有兩個:

乙個是讓資料庫自己通過外來鍵來保證資料的完整性和一致性

乙個就是能夠增加er圖的可讀性

有些人認為外來鍵的建立會給開發時運算元據庫帶來很大的麻煩.因為資料庫有時候會由於沒有通過外來鍵的檢測而使得開發人員刪除,插入操作失敗.他們覺得這樣很麻煩

其實這正式外來鍵在強制你保證資料的完整性和一致性.這是好事兒.

例如:有乙個基礎資料表,用來記錄商品的所有資訊。其他表都儲存商品id。查詢時需要連表來查詢商品的名稱。單據1的商品表中有商品id欄位,單據2的商品表中也有商品id欄位

。如果不使用外來鍵的話,當單據1,2都使用了商品id=3的商品時,如果刪除商品表中id=3的對應記錄後,再檢視單據1,2的時候就會查不到商品的名稱。

當表很少的時候,有人認為可以在程式實現的時候來通過寫指令碼來保證資料的完整性和一致性。也就是在刪除商品的操作的時候去檢測單據1,2中是否已經使用了商品id為3的商

品。但是當你寫完指令碼之後系統有增加了乙個單據3 ,他也儲存商品id找個字段。如果不用外來鍵,你還是會出現查不到商品名稱的情況。你總不能每增加乙個使用商品id的字段的

單據時就回去修改你檢測商品是否被使用的指令碼吧,同時,引入外來鍵會使速度和效能下降。

(2) 新增外來鍵的格式:

alter table yourtablename

add [constraint 外鍵名] foreign key [id] (index_col_name, ...)

references tbl_name (index_col_name, ...)

[on delete ]

[on update ]

說明:on delete/on update,用於定義delete,update操作.以下是update,delete操作的各種約束型別:

cascade:

外來鍵表中外鍵欄位值會被更新,或所在的列會被刪除.

restrict:

restrict也相當於no action,即不進行任何操作.即,拒絕父表update外來鍵關聯列,delete記錄.

set null:

被父面的外來鍵關聯欄位被update ,delete時,子表的外來鍵列被設定為null.

而對於insert,子表的外來鍵列輸入的值,只能是父表外來鍵關聯列已有的值.否則出錯.

外來鍵定義服從下列情況:(前提條件)

1)所有tables必須是innodb型,它們不能是臨時表.因為在mysql中只有innodb型別的表才支援外來鍵.

2)所有要建立外來鍵的字段必須建立索引.

3)對於非innodb表,foreign key子句會被忽略掉。

注意:建立外來鍵時,定義外鍵名時,不能加引號.

如: constraint 'fk_1' 或 constraint "fk_1"是錯誤的

(3) 檢視外來鍵:

show create table ***;可以檢視到新建的表的**以及其儲存引擎.也就可以看到外來鍵的設定.

刪除外來鍵:

alter table drop foreign key '外鍵名'.

注意:只有在定義外來鍵時,用constraint 外鍵名 foreign key .... 方便進行外來鍵的刪除.

若不定義,則可以:

先輸入:alter table drop foreign key -->會提示出錯.此時出錯資訊中,會顯示foreign key的系統缺省外鍵名.--->

用它去刪除外來鍵.

(4) 舉例

例項一:

4.1create table parent(id int not null,

primary key (id)

) type=innodb; 

-- type=innodb 相當於 engine=innodb

create table child(id int, parent_id int,

index par_ind (parent_id),

foreign key (parent_id) references parent(id)

on delete cascade

) type=innodb;

向parent插入資料後,向child插入資料,插入時,child中的parent_id的值只能是parent中有的資料,否則插入不成功;

刪除parent記錄時,child中的相應記錄也會被刪除;-->因為: on delete cascade

更新parent記錄時,不給更新;-->因為沒定義,預設採用restrict.

4.2若child如下:

mysql>

create table child(id int not null primary key auto_increment,parent_id int,

index par_ind (parent_id),

constraint fk_1 foreign key (parent_id) references

parent(id) on update cascade on delete restrict)

type=innodb;

用上面的:

1).則可以更新parent記錄時,child中的相應記錄也會被更新;-->因為: on update cascade

2).不能是子表操作,影響父表.只能是父表影響子表.

3).刪除外來鍵:

alter table child drop foreign key fk_1;

新增外來鍵:

alter table child add constraint fk_1 foreign key (parent_id) references

parent(id) on update restrict on delete set null;

(5) 多個外來鍵存在:

product_order表對其它兩個表有外來鍵。

乙個外來鍵引用乙個product表中的雙列索引。另乙個引用在customer表中的單行索引:

create table product (category int not null, id int not null,

price decimal,

primary key(category, id)) type=innodb;

create table customer (id int not null,

primary key (id)) type=innodb;

create table product_order (no int not null auto_increment,

product_category int not null,

product_id int not null,

customer_id int not null,

primary key(no),

-- 雙外來鍵

index (product_category, product_id),

foreign key (product_category, product_id)

references product(category, id)

on update cascade on delete restrict,

-- 單外來鍵

index (customer_id),

foreign key (customer_id)

references customer(id)) type=innodb;

(6) 說明:

1.若不宣告on update/delete,則預設是採用restrict方式.

2.對於外來鍵約束,最好是採用: on update cascade on delete restrict 的方式.

Mysql中外鍵詳解

1.定義外來鍵的操作 2.這個外來鍵的名字該如何定義?3.乙個表的外來鍵必是另乙個表的主鍵 比如,如果不定義student中sno為主鍵,那麼score的sno為外來鍵就無法定義 create table student sno varchar 20 not null primary key,cno...

MySQL中 外來鍵約束

alter table yourtablename add constraint 外鍵名 foreign key id index col name,references tbl name index col name,on delete on update 說明 on delete on upda...

MySQL中外鍵約束

外來鍵的好處 可以使得兩張表關聯,保證資料的一致性和實現一些級聯操作 如果在父表中找不到候選鍵,則不允許在子表上進行insert update 外來鍵定義服從下列情況 所有tables必須是innodb型,它們不能是臨時表。在引用表中,必須有乙個索引,外來鍵列以同樣的順序被列在其中作為第一列。這樣乙...