Error Code: 1215. Cannot add foreign key constraint

简介:

解决办法

(1)外键对应的字段数据类型不一致

(2)两张表的存储引擎不一致

(3)设置外键时“删除时”设置为“SET NULL”

     于是,我利用排除法,首先查看表的存储引擎,发现都是InnoDB引擎,排除第二条;设置外键时“删除时”设置为“SET NULL”,我改为其他的选项,发现也不能保存,故排除了第三项;接着,我查看了外键对应的字段的数据类型,发现它们竟然不一致

t_stu_info中的id字段是bigint(8),而t_tea_info中的t_stu字段是varchar(20) ,将后者也改为bigint(8),可以成功地保存

      故这次错误应该是外键对应的字段的数据类型不一致











本文转自 yntmdr 51CTO博客,原文链接:http://blog.51cto.com/yntmdr/1874386,如需转载请自行联系原作者
目录
相关文章
|
数据库
MongoError: E11000 duplicate key error collection: blog.users index: email_1 dup key
MongoError: E11000 duplicate key error collection: blog.users index: email_1 dup key
|
关系型数据库 MySQL 数据库
解决出现的SQLIntegrityConstraintViolationExceptionw:Duplicate entry ‘10‘ for for key ‘user.PRIMARY‘问题
解决出现的SQLIntegrityConstraintViolationExceptionw:Duplicate entry ‘10‘ for for key ‘user.PRIMARY‘问题
220 0
|
关系型数据库 MySQL 数据库
View ‘information_schema.SCHEMATA‘ references invalid table(s) or column(s) or function(s) or define
View ‘information_schema.SCHEMATA‘ references invalid table(s) or column(s) or function(s) or define
234 0
|
存储 关系型数据库 MySQL
超详细!Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法
超详细!Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法
3809 0
超详细!Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法
|
数据库
Incorrect table definition; there can be only one auto column and it must be defined as a key
Incorrect table definition; there can be only one auto column and it must be defined as a key
179 0
Incorrect table definition; there can be only one auto column and it must be defined as a key
|
存储 关系型数据库 MySQL
ERROR 1215 (HY000): Cannot add foreign key constraint
ERROR 1215 (HY000): Cannot add foreign key constraint
ERROR 1215 (HY000): Cannot add foreign key constraint
|
SQL 关系型数据库 MySQL
[Err] 1294 - Invalid ON UPDATE clause for 'comment_time' column【详细解决办法】
[Err] 1294 - Invalid ON UPDATE clause for 'comment_time' column【详细解决办法】
738 0
[Err] 1294 - Invalid ON UPDATE clause for 'comment_time' column【详细解决办法】
|
关系型数据库 MySQL 数据库
MySQL问题解决:Cannot delete or update a parent row: a foreign key constraint fails
MySQL问题解决:Cannot delete or update a parent row: a foreign key constraint fails
1402 0