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 ...
随机推荐
- MYSQL拒绝访问:not allowed to connect解决方法
分享下MYSQL拒绝访问报错not allowed to connect的解决方法. 可以在其它任何的主机上以root身份登录 mysql报如下错误,截取部分, message from server ...
- asp.net mvc中的用户登录验证过滤器
在WEB项目中建立 类: public class LoginFilter : ActionFilterAttribute { public override voi ...
- 【Unity】第10章 Mecanim动画系统
分类:Unity.C#.VS2015 创建日期:2016-05-02 一.简介 Unity提供了两种动画系统:一种是早期版本提供的旧版(Legacy)动画系统,旧版本(Legacy)以后将逐步被淘汰掉 ...
- android Binder的优点
Linux进程间通信的方式: 管道(Pipe) 信号(Signal) 消息队列(Message) 共享内存(Share Memory) 套接字(Socket)中断 Binder Binder 介绍: ...
- 菜鸟学Java(十)——分页查询
今天继续跟大家说说一些非常基础的东西,这次我们说说分页查询.说到分页,可能很多人都听说过什么真分页.假分页的.简单解释一下,拿第二页,每页20条为例:真分:数据库里取 的就是21-40条:假分:数据库 ...
- Linux系统性能监控之6个vmstat和6个iostat命令
这篇文章主要介绍一些Linux性能检测相关的命令. vmstat和iostat的两个命令可以运行在主流的Linux/Unix操作系统上. 如果vmstat和iostat命令不能再你的电脑上运行,请安装 ...
- .NET MVC5+ EF+AutoFac自动注入框架
1.创建一个MVC系统 VIEW显示页面代码: <link href="~/Content/bootstrap.css" rel="stylesheet" ...
- delphi中Webbrowser疑难问题集锦<转>
1.获得网页中变量值 htm中<script> var currID=123</script> 程序中可以这么调用 id := Form1.WebBrows ...
- 【delphi】多线程同步之Semaphore
另外两种多线程的同步方法 CriticalSection(临界区) 和 Mutex(互斥), 这两种同步方法差不多, 只是作用域不同; CriticalSection(临界区) 类似于只有一个蹲位的公 ...
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...