当前位置:网站首页>1215 – Cannot add foreign key constraint
1215 – Cannot add foreign key constraint
2022-08-09 21:43:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
最近在做新生入学系统,学生表中包括新生的班级,专业等信息,班级,专业就需要和班级表,专业表进行关联,但是在添加外键的过程中却出现了“Cannot add foreign key constraint” 的问题,也就是不能添加外键约束,为什么就不能添加外键呢?
出现这个问题主要有三个原因:
(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,就可以添加外键了。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/105725.html原文链接:https://javaforall.cn
边栏推荐
- Pagoda measurement - building LightPicture open source map bed system
- STC8H development (15): GPIO drive Ci24R1 wireless module
- RHEL7系统修复rm -rf /boot /etc/fstab
- Use zeros(), ones(), fill() methods to generate data in TF
- 【泛型编程】模板全详解
- 【云原生】4.2 DevOps 精讲篇
- [Implementation of the interface for adding, deleting, checking, and modifying a double-linked list]
- 凸集与凸函数
- laravel table migration error [easy to understand]
- 万字总结:分布式系统的38个知识点
猜你喜欢

消防安全培训|“蓝朋友”,开课了!
![[Generic Programming] Full Detailed Explanation of Templates](/img/9d/7864f999cb2e4edda2ee7723558135.png)
[Generic Programming] Full Detailed Explanation of Templates

QGIS编译SIP的问题

PMP每日一练 | 考试不迷路-8.9(包含敏捷+多选)

RHEL7系统修复rm -rf /boot /etc/fstab

Multiple reasons for MySQL slow query

Install win virtual machine on VMware

Problems with compiling SIP with QGIS

万字总结:分布式系统的38个知识点

论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》
随机推荐
Word箭头上面怎么打字
Xiaohei leetcode's refreshing rainy day trip, just finished eating Yufei Beef Noodles, Mala Tang and Beer: 112. Path Sum
凸集与凸函数
APP automation test framework - UiAutomator2 introductory
Install Mysql8.0 on windos, and solve the problem of re-login exception ERROR 1045 (28000)
MySQL慢查询的多个原因
Interpretation of the paper (DropEdge) "DropEdge: Towards Deep Graph Convolutional Networks on Node Classification"
STC8H development (15): GPIO drive Ci24R1 wireless module
Cookie, session, token
《强化学习周刊》第57期:DL-DRL、FedDRL & Deep VULMAN
蔚来杯2022牛客暑期多校训练营7 CFGJ
Interpretation of the paper (DropEdge) "DropEdge: Towards Deep Graph Convolutional Networks on Node Classification"
Tensorflow中placeholder函数的用法
String hashing (2014 SERC J question)
Several ways to draw timeline diagrams
4D Summary: 38 Knowledge Points of Distributed Systems
TF中使用zeros(),ones(), fill()方法生成数据
Use zeros(), ones(), fill() methods to generate data in TF
AI Knows Everything: Building and Deploying a Sign Language Recognition System from Zero
PMP每日一练 | 考试不迷路-8.9(包含敏捷+多选)