Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法

报错的原因大概分为三种:
原因一:

添加的外键列与另一个表的唯一索引列(一般是主键)的数据类型不同
原因二:
要添加外键的表类型与另一个表的存储引擎是不是都为innodb引擎
#查看表引擎
法一: show create table 表名;
法二:show table status from 数据库 where name=‘表名’;
法三:use information_schema;
select table_catalog,table_schema,table_name,engine from tables
where table_schema=‘数据库名’ and table_name=‘表名’;
原因三:
设置的外键与另一个表中的唯一索引列(一般是主键)中的值不匹配
#解决办法:删除要成为外键的列,再次创建并默认为空字符
原因四:
新增的数据格式不对,例如往外键列中新增数据格式不对情况下:
Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法
此时需要检查下数据格式是否不符