mysql的外键简单使用和介绍

1.使用外键:

#添加外键,有constraint 约束名。
alter table test3
add constraint fk_test3_test1_id foreign key (test1_id) references test1(id) on delete set null on update set null;

#删除外健:
alter table test3
drop foreign key fk_test3_test1_id;

2.介绍选项:

restrict 不允许父表delete或者update记录。
cascade 当delete或者update父表记录时,级联delete或者update子表记录。
set null 子表的字段必须允许为null,delete或者update父表记录时,子表的字段值变为null。
no action 相当于restrict条件。


3.查询信息:
select * from information_schema.REFERENTIAL_CONSTRAINTS;
select * from information_schema.TABLE_CONSTRAINTS;
select * from information_schema.KEY_COLUMN_USAGE;

4.导入含外键的文件:
mysql> SET foreign_key_checks = 0;
mysql> SOURCE dump_file_name;
mysql> SET foreign_key_checks = 1;

原文地址:https://www.cnblogs.com/Tom-Idea/p/4903996.html