建表

对于插入新数据时,对于由于外键关联的主表缺少对应信息而导致子表插入数据失败的,可以暂时关闭外键检查。

1
2
3
4
5
6
7
8
9
关闭外键检查
SET FOREIGN_KEY_CHECKS = 0;

查看是否关闭
show VARIABLES like "FOREIGN_KEY_CHECKS";

重新打开外键检查
SET FOREIGN_KEY_CHECKS = 1;

导入时报错 Failed to open the referenced table

mysql导入报错信息

1
2
3
[root@lw 20230323bak]# mysql -h 193.169.203.15 -u root -p -P 5958 apple < apple-20230323.sql 
Enter password:
ERROR 1824 (HY000) at line 5: Failed to open the referenced table 'qrtz_triggers'

解决方法:导入时关闭关键检查

1
cat <(echo "SET FOREIGN_KEY_CHECKS=0;") apple-20230323.sql |mysql -h 193.169.203.15 -u root -p -P 5958 apple

参考:
带你去流浪的博客 (cnblogs.com)