MySQL 外來鍵 foreign key constraint
阿新 • • 發佈:2020-12-22
- 取消外來鍵約束
set @@foreign_key_check=0
-
describe information_schema.key_column_usage;
- 查看錶上的索引和foreign key
select * from information_schema.key_column_usage where table_name='b'\G
- 查看錶的哪個column被哪個表的column所引用
select * from information_schema.key_column_usage where referenced_table_name='
- 新建兩張測試表
-
增加外來鍵約束
alter table b add constraint fk_b_a foreign key (id_a) references a (id) on update cascade on delete cascade;
- 刪除外來鍵約束
alter table b drop foreign key fk_b_a;
- 外來鍵的約束關係
父表: 被引用的表,column必須為primary key 子表: 引用主表,外來鍵的某一column引用父表的primary key cascade: 在父表update/delete,同步update/delete子表的記錄 set null: 在父表update