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 主表(主 ...
随机推荐
- iOS开发之自定义控制器切换
iOS8以后, 苹果公司推出了UIPresentationController, 通过其(presentedController 和 presentingController)来控制modal控制器操 ...
- WiFi无线模块学习1——HLK-M30使用
产品概述 概述: 通过该模块,传统的串口设备在不需要更改任何配置的情况下,即可通过Internet 网络传输自己的数据.为用户的串口设备提供完整快读的解决方案. 技术参数 可查询技术规格表 主要应用领 ...
- Android之EditText组件学习
一.基础学习 1.Button是TextView的一个子类,所以按钮本身是一个特殊的文本,属性和TextView相似 2.EditText类似html里的input type="text&q ...
- SaltStack的salt-api里如何指定用户执行cmd.script
在杨威的协助下,命令行,API调用都完美搞定. 主要是RUNAS参数的位置,以及它作为CURL POST -D DATA的使用. salt '1.2.3.4' cmd.script "sal ...
- Android 获取手机联系人信息
//获取联系人 Uri rawContacts = Uri.parse("content://com.android.contacts/raw_contacts"); Conten ...
- Sql中的datetime类型的空值和c#中的DateTime的空值的转换方法
[一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/p/3412796.html] 在NET 2.0以上版本提供了一种新的方法 就是加问号,DateTim ...
- ANDROID_MARS学习笔记_S04_004_用HTTPCLENT发带参数的get和post请求
一.代码 1.xml(1)activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/r ...
- delphi非IE内核浏览器控件TEmbeddedChrome下载|TEmbeddedChrome代码
下载地址: 点击下载 代码示例: 在TForm的oncreate方法中写入一些代码 procedure TForm1.FormCreate(Sender: TObject); begin Chromi ...
- u-boot使用
下载与烧写 使用U-boot将映像文件烧写到板上的Flash,一般步骤是: (1)通过网络.串口.U盘.SD卡等方式将文件传输到SDRAM: (2)使用Nand Flash或Nor Flash相关的读 ...
- bzoj1023
研究了一下仙人掌首先,仙人掌虽然不是树,但却有很强的树的既视感如果把每个环都看做一个点,那么他就是一棵树当然这不能直接缩环,因为环和环可以有一个交点如果是树,求直径都会做,令f[i]表示i到子树的最长 ...