1215 - Cannot add foreign key constraint
2016-08-21 21:48
435 查看
最近在做新生入学系统,学生表中包括新生的班级,专业等信息,班级,专业就需要和班级表,专业表进行关联,但是在添加外键的过程中却出现了“Cannot add foreign key constraint” 的问题,也就是不能添加外键约束,为什么就不能添加外键呢?
![](https://img-blog.csdn.net/20160821213005884?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
出现这个问题主要有三个原因:
(1)外键对应的字段数据类型不一致
(2)设置外键时“删除时”设置为“SET NULL”
(3)两张表的存储引擎不一致
经过检查,前两种情况排除,但是两张表的存储引擎都不清楚,于是又百度了一些关于存储引擎的东西:
查询表的引擎:
a、show table status from db_name where name='table_name';
b、show create table table_name;
查询结果如图:
班级表的引擎:
![](https://img-blog.csdn.net/20160821213031212?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
】
学生表的引擎:
![](https://img-blog.csdn.net/20160821213123713?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
果然是两张表的引擎不一致,那就修改成一样的呗
修改表的引擎:
alter table table_name engine=innodb;
首先将basicclassentities表的ndbcluster引擎修改为InnoDB,但是又出现了新的错误:"1217 - Cannot delete or update a parent row: a foreign key constraint fails",于是尝试将freshstudententities表的引擎修改为ndbcluster,同样的问题。
"1217
- Cannot delete or update a parent row: a foreign key constraint fails",这可能是MySQL在引擎中设置了foreign key关联,造成无法更新或删除数据。可以通过设置FOREIGN_KEY_CHECKS变量来避免这种情况:
SET FOREIGN_KEY_CHECKS = 0; 删除完成后设置 :SET
FOREIGN_KEY_CHECKS = 1;
还是不能修改表的引擎,于是又继续探索mysql的各种引擎(http://www.cnblogs.com/ggjucheng/archive/2012/11/11/2765469.html),经过分析,由于ndbcluster和集群,分布式有关,自己的电脑上没有足够的权限,所以修改不了,只要将两张表的引擎都修改为InnoDB,就可以添加外键了。
出现这个问题主要有三个原因:
(1)外键对应的字段数据类型不一致
(2)设置外键时“删除时”设置为“SET NULL”
(3)两张表的存储引擎不一致
经过检查,前两种情况排除,但是两张表的存储引擎都不清楚,于是又百度了一些关于存储引擎的东西:
查询表的引擎:
a、show table status from db_name where name='table_name';
b、show create table table_name;
查询结果如图:
班级表的引擎:
】
学生表的引擎:
果然是两张表的引擎不一致,那就修改成一样的呗
修改表的引擎:
alter table table_name engine=innodb;
首先将basicclassentities表的ndbcluster引擎修改为InnoDB,但是又出现了新的错误:"1217 - Cannot delete or update a parent row: a foreign key constraint fails",于是尝试将freshstudententities表的引擎修改为ndbcluster,同样的问题。
"1217
- Cannot delete or update a parent row: a foreign key constraint fails",这可能是MySQL在引擎中设置了foreign key关联,造成无法更新或删除数据。可以通过设置FOREIGN_KEY_CHECKS变量来避免这种情况:
SET FOREIGN_KEY_CHECKS = 0; 删除完成后设置 :SET
FOREIGN_KEY_CHECKS = 1;
还是不能修改表的引擎,于是又继续探索mysql的各种引擎(http://www.cnblogs.com/ggjucheng/archive/2012/11/11/2765469.html),经过分析,由于ndbcluster和集群,分布式有关,自己的电脑上没有足够的权限,所以修改不了,只要将两张表的引擎都修改为InnoDB,就可以添加外键了。
相关文章推荐
- 错误代码: 1215 Cannot add foreign key constraint
- ERROR 1215 (HY000): Cannot add foreign key constraint
- 1215 - Cannot add foreign key constraint
- 1215-Cannot add foreign key constraint
- mysql异常 [Err] 1215 - Cannot add foreign key constraint
- 1215 - Cannot add foreign key constraint
- MySQL 1215 Cannot add foreign key constraint 错误解决办法
- 1215 - Cannot add foreign key constraint
- ERROR 1215 (HY000): Cannot add foreign key constraint 解决方法
- 1215-Cannot add the foreign key constraint
- Laravel 5.5 迁移报错:General error: 1215 Cannot add foreign key constraint
- MySQL添加外键时报错 ERROR 1215 (HY000): Cannot add foreign key constraint
- 数据库建表的时候报 “1215 Cannot add foreign key constraint”
- MySQL添加外键时报错:1215 Cannot add the foreign key constraint的解决方法
- MySQL添加外键时报错:1215 Cannot add the foreign key constraint的解决方法
- MySQL中创建外键的错误:1215 Cannot add the foreign key constraint
- 异常处理:1215 - Cannot add foreign key constraint
- 1215 Cannot add the foreign key constraint
- 1215 - Cannot add foreign key constraint
- [Err] 1215 - Cannot add foreign key constraint