mysql执行带外键的sql文件时出现mysql ERROR 1215 (HY000): Cannot add foreign key constraint的解决
ERROR 1215 (HY000): Cannot add foreign key constraint
最近在建表时遇到了这个错误,然后找了下找到了解决办法,记录下:
本来是要建两张表:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
create table department(id int,name varchar(20));create table employee(id int primary key auto_increment,name varchar(20),sex enum('male','female') not null default 'male',age int,dep_id int,foreign key(dep_id) references department(id)); |
出现下面的提示:

然后开始上网查,有的说是用的引擎不同的原因,查看了下我的,两引擎一模一样,
又有的说一个是int ,因为加了auto_increment后,实际变成了int undesigned,既然变成了
int undesigned那我也设置成undesigned,再插入,错误依旧,
最后 我想到了上面的department表中的id,只是整形,可能不唯一,
然后加上了 primary key
|
1
2
3
4
|
create table department(id int primary key,name varchar(20)); |
再次试验,发现能正常创建表

这里的具体原理是什么还没搞清楚,上面的也只是猜测,特此记录
如果找到原因再更新!
更新:
https://stackoverflow.com/questions/22013511/mysql-error-1215-hy000-cannot-add-foreign-key-constraint

也就是说我上面 的猜测是正确的!
看看对unique的测试:

转自:http://www.cnblogs.com/Andy963/p/7634537.html
以上要注意的就是最后一点原因外键必须是另一个表的主键或者唯一键(即要不把那个列设为主键要不就建立一个唯一索引)
powerdesigner建立主键不再介绍,建立唯一索引的步骤如下



选择某列为unique index即可。
至于外键的建立如下:
双击表与表之间的连接线


mysql执行带外键的sql文件时出现mysql ERROR 1215 (HY000): Cannot add foreign key constraint的解决的更多相关文章
- MySQL添加外键时报错 ERROR 1215 (HY000): Cannot add foreign key constraint
1.数据类型 2.数据表的引擎 数据表 mysql> show tables; +------------------+ | Tables_in_market | +--------- ...
- 如何在c#代码中执行带GO语句的SQL文件
需要在C#代码中执行一个SQL文件的内容.遇到了两个问题: 1. 因为SQL文件中有"GO"语句,执行时报错"Incorrect syntax near 'GO'.& ...
- Navicat MYSQL 建立关联表 保存时遇到 cannot add foreign key constraint
首先建立user表,如下图 然后建立message表,userid用作外键,关联user表的id 点击上面的外键按钮,添加外键如下 结果保存时报错: cannot add foreign key co ...
- MySQL设置外键报错 #1452 - Cannot add or update a child row: a foreign key constraint fails 解决方法
MySQL数据库,当我尝试在A表中设置B表的主键为外键时,报出错误:#1452 - Cannot add or update a child row: a foreign key constraint ...
- Navicat For MySQL--外键建立与cannot add foreign key constraint分析
hrm_job.png 参考资料: https://blog.csdn.net/ytm15732625529/article/details/53729155 https://www.cnblogs. ...
- Mysql之1451 - Cannot delete or update a parent row: a foreign key constraint fails...解决办法记录
今天使用delete语句删除一张表中的一条信息时,提示了这么一个错误:1451 - Cannot delete or update a parent row: a foreign key constr ...
- mysql清空带外键的表
set FOREIGN_KEY_CHECKS =0;TRUNCATE memo;TRUNCATE customer;set FOREIGN_KEY_CHECKS =1;
- 命令行插入含有中文的sql文件,报错ERROR 1366 (HY000): Incorrect stringvalue:
--以下是插入语句: insert into sms_inbox values('123456','123456', 'cd', sysdate(), '今天天 气很好', 1, sysdate(), ...
- MySQL Error 1215: Cannot add foreign key constraint
MySQL Error 1215: Cannot add foreign key constraint DROP TABLE IF EXISTS `r_role_region`; CREATE TAB ...
随机推荐
- virtualbox和vagrant卸载脚本在macbook
virtualbox和vagrant在macbook版本的安装文件内,都有一个卸载脚本uninstall.tool vagrant2.1.5卸载脚本: #!/usr/bin/env bash #--- ...
- 2. Apache Axis2 快速学习手册之概览
这篇博文和大家一起学习下Apache Axis2 官方文档的快速指南篇 英文原文:http://axis.apache.org/axis2/java/core/docs/quickstartguide ...
- bit,Byte,Word,DWORD(DOUBLE WORD,DW)
1个二进制位称为1个bit,8个二进制位称为1个Byte,也就是1个字节(8位),2个字节就是1个Word(1个字,16位),则DWORD(DOUBLE WORD)就是双字的意思,两个字(4个字节/3 ...
- 高级数据库及一步一步搭建versant数据库
总的来说,高级数据库课程分为分布式数据库和面向对象数据库两块.分布式数据库介绍了分布式数据库的方方面面,包括数据库系统的设计.查询处理优化.事务管理和恢复.并发控制.可靠性.安全性与目录管理等.面向对 ...
- [Windows Azure] Getting Started with Windows Azure SQL Database
In this tutorial you will learn the fundamentals of Windows Azure SQL Database administration using ...
- python(50):python 向上取整 ceil 向下取整 floor 四舍五入 round
取整:ceil 向下取整:floor 四舍五入:round 使用如下:
- cocoside 编译错误可能问题
版本不对..查看/usr/xxx/.profile中console等的全局变量 安装了不和谐的版本..记得删除application/cosos 重新安装. 或者干掉cocostudio
- A implementaion for 2D blue noise
http://www.redblobgames.com/articles/noise/2d/
- HBase scan setBatch和setCaching的区别【转】
转自:http://blog.csdn.net/caoli98033/article/details/44650497 HBase的查询实现只提供两种方式: 1.按指定RowKey获取唯一一条记录,g ...
- 基于html5顶部导航3D翻转展开特效
基于html5顶部导航3D翻转展开特效是一款基于jQuery+HTML5实现的3D翻转网站导航菜单代码.效果图如下: 在线预览 源码下载 实现的代码. html代码: <header cla ...