mysql外來鍵約束

2021-07-09 14:38:55 字數 493 閱讀 2874

目前mysql只有innodb引擎支援外來鍵約束:

新增外來鍵語法:alter table 表名 add constraint `外鍵名字` foreign key(`外來鍵字段`) references 外表表(`外來鍵表的主鍵字段`);

刪除外來鍵:alter table 表名 drop foreign key `外鍵名字`;

級聯更新和刪除,以下五種情況:

關鍵字     含義

cascade 刪除包含與已刪除鍵值有參照關係的所有記錄

setnull 修改包含與已刪除鍵值有參照關係的所有記錄,使用null值替換(只能用於已標記為not

null的字段)

restrict 拒絕刪除要求,直到使用刪除鍵值的輔助表被手工刪除,並且沒有參照時(這是預設設定,也是最安全的設定)

noaction 同restrict

什麼都不加 同restrict

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...