当前位置:网站首页>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
边栏推荐
- laravel 表迁移报错[通俗易懂]
- 凸集与凸函数
- Photometric Stereo 光度立体法三维重建
- Xiaohei leetcode's refreshing rainy day trip, just finished eating Yufei Beef Noodles, Mala Tang and Beer: 112. Path Sum
- Hessian Matrix 海森矩阵
- SecureCRT background color
- 自监督学习 —— MoCo v2
- Bean生命周期
- Sudoku | Backtrack-7
- TF中random.normal()与random.truncated_normal()
猜你喜欢
cad图纸怎么复制到word文档里面?Word里插CAD图怎么弄?
4D Summary: 38 Knowledge Points of Distributed Systems
Don't tell me to play, I'm taking the PMP exam: what you need to know about choosing an institution for the PMP exam
STC8H Development (15): GPIO Drives Ci24R1 Wireless Module
QGIS编译SIP的问题
Converting angles to radians
SQLi-LABS Page-2 (Adv Injections)
Bean生命周期
CVPR22 Oral | shunt through multi-scale token polymerization from attention, code is open source
[corctf 2022] section
随机推荐
【泛型编程】模板全详解
SecureCRT sets the timeout period for automatic disconnection
Leetcode 93 IP addresses
Word第一页不要页眉怎么设置?设置Word首页不要页眉方法教程
TF使用constant生成数据
Multiple reasons for MySQL slow query
Technology Sharing | How to Handle Header Cookies in Interface Automation Testing
《强化学习周刊》第57期:DL-DRL、FedDRL & Deep VULMAN
AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis
微软word怎么转换成pdf文件?微软word转换为pdf格式的方法
ACM MM 2022 | Cloud2Sketch: Painting with clouds in the sky, AI brush strokes
L3-2 Delete up to three characters (30 points)
别叫我玩,我要考PMP:考PMP选择机构需要了解的那些事儿
Optimization of SQL Statements and Indexes
The round functions in the np, ceil function and floor function
Endpoint mode for NetCore routing
Leetcode 93 复原IP地址
[Generic Programming] Full Detailed Explanation of Templates
The overall construction process of the Tensorflow model
论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》