MySQL添加外键时报错 ERROR 1215 (HY000): Cannot add foreign key constraint
1.数据类型 2.数据表的引擎
数据表
mysql> show tables;
+------------------+
| Tables_in_market |
+------------------+
| customers_info |
| orders |
+------------------+
2 rows in set (0.00 sec)
遇到错误信息
mysql> alter table orders add constraint fk_orders foreign key(c_id) references customers_info(c_num);
ERROR 1215 (HY000): Cannot add foreign key constraint
首先想到可能类型不同,于是查看表结构
mysql> desc customers_info;
+----------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+-------+
| c_num | int(10) unsigned | NO | PRI | 0 | |
| c_name | varchar(70) | YES | | NULL | |
| c_birth | datetime | NO | | NULL | |
| c_phone | varchar(50) | YES | | NULL | |
| c_gender | char(1) | YES | | NULL | |
+----------+------------------+------+-----+---------+-------+
5 rows in set (0.02 sec) mysql> desc orders;
+--------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------------+------+-----+---------+----------------+
| o_num | int(11) | NO | PRI | NULL | auto_increment |
| o_date | date | YES | | NULL | |
| c_id | int(10) unsigned | YES | | NULL | |
+--------+------------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
发现c_id和c_num类型是相同的,那就有可能是引擎出问题了,查看创建数据表时的引擎
mysql> show create table customers_info\G;
*************************** 1. row ***************************
Table: customers_info
Create Table: CREATE TABLE `customers_info` (
`c_num` int(10) unsigned NOT NULL DEFAULT '',
`c_name` varchar(70) DEFAULT NULL,
`c_birth` datetime NOT NULL,
`c_phone` varchar(50) DEFAULT NULL,
`c_gender` char(1) DEFAULT NULL,
PRIMARY KEY (`c_num`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec) ERROR:
No query specified mysql> show create table orders\G;
*************************** 1. row ***************************
Table: orders
Create Table: CREATE TABLE `orders` (
`o_num` int(11) NOT NULL AUTO_INCREMENT,
`o_date` date DEFAULT NULL,
`c_id` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`o_num`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec) ERROR:
No query specified
发现customers_info表的引擎是MyISAM, 修改此表的引擎为InnoDB
mysql> alter table customers_info engine = innoDB;
Query OK, 0 rows affected (0.57 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table orders add constraint fk_orders foreign key(c_id) references customers_info(c_num);
Query OK, 0 rows affected (0.62 sec)
Records: 0 Duplicates: 0 Warnings: 0
返回Query OK表明外键建立成功了,查看一下
mysql> show create table orders\G;
*************************** 1. row ***************************
Table: orders
Create Table: CREATE TABLE `orders` (
`o_num` int(11) NOT NULL AUTO_INCREMENT,
`o_date` date DEFAULT NULL,
`c_id` int(10) unsigned DEFAULT NULL,
PRIMARY KEY (`o_num`),
KEY `fk_orders` (`c_id`),
CONSTRAINT `fk_orders` FOREIGN KEY (`c_id`) REFERENCES `customers_info` (`c_num`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
MySQL添加外键时报错 ERROR 1215 (HY000): Cannot add foreign key constraint的更多相关文章
- mysql执行带外键的sql文件时出现mysql ERROR 1215 (HY000): Cannot add foreign key constraint的解决
ERROR 1215 (HY000): Cannot add foreign key constraint 最近在建表时遇到了这个错误,然后找了下找到了解决办法,记录下: 本来是要建两张表: 1 2 ...
- ERROR 1215 (HY000): Cannot add foreign key constraint
MySQL中在为一个varchar类型数据列添加外键时,会发生上面所示的错误,这里我google了一下,感觉它们碰到的问题跟我这个说的有点不相干,尝试了多种方式后来才发现是:主表(table1)所对应 ...
- MySQL添加外键报错 - referencing column 'xx' and referenced column 'xx' in foreign key constraint 'xx' are incompatible
MySQL给两个表添加外键时,报错 翻译意思是:外键约束“xx”中的引用列“xx”和引用列“xx”不兼容 说明两个表关联的列数据类型不一致,比如:varchar 与 int,或者 int无符号 与 i ...
- mysql 添加外键报错:
1.报错信息 Cannot add or update a child row: a foreign key constraint fails 2.原因分析 [1]字段的数据类型 父表: 子表: 以上 ...
- MYSQL 添加外键报错
2014年6月16日 10:48:51 出错的部分提示摘录: #1452 - Cannot add or update a child row: a foreign key constraint fa ...
- mysql添加外键无法成功的原因
最近很忙,碰到很多问题都忘了发上来做个记录,现在又忘了,FUCK,现在碰到一个问题, 就是mysql添加外键总是无法成功,我什么都试了,就是没注意signed和unsigned,FUCK,因为我用my ...
- MySQL中MyISAM与InnoDB区别及选择,mysql添加外键
InnoDB:支持事务处理等不加锁读取支持外键支持行锁不支持FULLTEXT类型的索引不保存表的具体行数,扫描表来计算有多少行DELETE 表时,是一行一行的删除InnoDB 把数据和索引存放在表空间 ...
- mysql添加外键失败解决方案
mysql重启命令: [root@wshCentOS centOS7Share]# service mysqld stopRedirecting to /bin/systemctl stop mys ...
- mysql添加外键语句
sql语句格式: · 添加外键约束:alter table 从表 add constraint 外键(形如:FK_从表_主表) foreign key (从表外键字段) references 主表(主 ...
随机推荐
- 【python】【转】if else 和 elif
else和elif语句也可以叫做子句,因为它们不能独立使用,两者都是出现在if.for.while语句内部的.else子句可以增加一种选择:而elif子句则是需要检查更多条件时会被使用,与if和els ...
- 单片微机原理P0:80C51结构原理
本来我真的不想让51的东西出现在我的博客上的,因为51这种东西真的太low了,学了最多就所谓的垃圾科创利用一下,但是想一下这门课我也要考试,还是写一点东西顺便放博客上吧. 这一系列主要参考<单片 ...
- Android 联系人信息的读取注意判断是否为NULL Android联系人的删除实质
在Android系统联系人界面删除一条短信实际上并不是真正的删除,而是在数据库中标记raw_contacts表中Contact_id为null以及data表中raw_contact_id为空,这是为了 ...
- Oracle坑之-空字符串与NULL
空字符串与NULL 首先有如下代码 SELECT * FROM Pdc_DataDomain DD INNER JOIN Pdc_DD_Table DDT ON DD.DataDomainID = D ...
- sql server 2005+ 翻页
select * from ( select row_number() over (order by dm) row,* from t_zycp where dm like '%4%' ) tmp
- Cocos2d-x 坑之二:目录改动后, cannot run on the selected destination
1:2dx开发中,目录改动后,经常会碰到这个提示错误: cannot run on the selected destination 解决方法:一般是因为 Info.plist文件属性问题,把 Ta ...
- 应用程序域 z
应用程序域(AppDomain)已经不是一个新名词了,只要熟悉.net的都知道它的存在,不过我们还是先一起来重新认识下应用程序域吧,究竟它是何方神圣. 应用程序域 众所周知,进程是代码执行和资源分配的 ...
- linux下出现+ ls --color=auto -l --color=auto...++ echo -ne '\033]0;root@imon-2:~'等
[root@imon-2 ~]# cd /root/ + cd /root/ ++ echo -ne '\033]0;root@imon-2:~' [root@imon-2 ~]# ll + ls - ...
- 《C语言程序设计现代方法》第2章 编程题
7 编写一个程序,要求用户输入一个美金数量,然后显示出如何使用最少的20美元.10美元.5美元和1美元来付款. 提示:将付款金额除以20,确定20美元的数量,然后从付款金额中减去20美元的总金额.对其 ...
- HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...