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

2021-10-17 20:09:07 字數 656 閱讀 7174

外來鍵的建立:

建表如下:

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 parent(id)

) type=innodb;

如果你想實現,parent的記錄在刪除的同時chil記錄也刪的話可以這樣寫

create table child(

id int,

parent_id int,

foreign key (parent_id) references parent(id) on delete cascade

) type=innodb;

如果你想實現:parent的記錄在更新的同時chil記錄也更新的話可以這樣寫

create table child(     id int,     parent_id int,         foreign key (parent_id) references parent(id) on update cascade ) type=innodb;

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

My SQL外來鍵約束

外來鍵約束對子表的含義 如果在父表中找不到對應的候選鍵,則不能對子表進行insert update操作 外來鍵約束對父表的含義 在父表上進行update delete以更新或刪除在子表中有一條或多條對應匹配行的候選鍵時,父表的行為取決於 在定義子表的外來鍵時指定的on update on delet...