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的更多相关文章

  1. mysql执行带外键的sql文件时出现mysql ERROR 1215 (HY000): Cannot add foreign key constraint的解决

    ERROR 1215 (HY000): Cannot add foreign key constraint 最近在建表时遇到了这个错误,然后找了下找到了解决办法,记录下: 本来是要建两张表: 1 2 ...

  2. ERROR 1215 (HY000): Cannot add foreign key constraint

    MySQL中在为一个varchar类型数据列添加外键时,会发生上面所示的错误,这里我google了一下,感觉它们碰到的问题跟我这个说的有点不相干,尝试了多种方式后来才发现是:主表(table1)所对应 ...

  3. MySQL添加外键报错 - referencing column 'xx' and referenced column 'xx' in foreign key constraint 'xx' are incompatible

    MySQL给两个表添加外键时,报错 翻译意思是:外键约束“xx”中的引用列“xx”和引用列“xx”不兼容 说明两个表关联的列数据类型不一致,比如:varchar 与 int,或者 int无符号 与 i ...

  4. mysql 添加外键报错:

    1.报错信息 Cannot add or update a child row: a foreign key constraint fails 2.原因分析 [1]字段的数据类型 父表: 子表: 以上 ...

  5. MYSQL 添加外键报错

    2014年6月16日 10:48:51 出错的部分提示摘录: #1452 - Cannot add or update a child row: a foreign key constraint fa ...

  6. mysql添加外键无法成功的原因

    最近很忙,碰到很多问题都忘了发上来做个记录,现在又忘了,FUCK,现在碰到一个问题, 就是mysql添加外键总是无法成功,我什么都试了,就是没注意signed和unsigned,FUCK,因为我用my ...

  7. MySQL中MyISAM与InnoDB区别及选择,mysql添加外键

    InnoDB:支持事务处理等不加锁读取支持外键支持行锁不支持FULLTEXT类型的索引不保存表的具体行数,扫描表来计算有多少行DELETE 表时,是一行一行的删除InnoDB 把数据和索引存放在表空间 ...

  8. mysql添加外键失败解决方案

    mysql重启命令: [root@wshCentOS centOS7Share]# service mysqld stopRedirecting to /bin/systemctl stop  mys ...

  9. mysql添加外键语句

    sql语句格式: · 添加外键约束:alter table 从表 add constraint 外键(形如:FK_从表_主表) foreign key (从表外键字段) references 主表(主 ...

随机推荐

  1. 关于DPC和workitem的简单用法

    这个随笔是记录我半个月左右的时间,从想法到查资料请教,以及到实践的成果. 我想实现的是,隔定时时间写文件,本以为调用写的函数就可以实现了,结果各种BSOD,IRQL_NOT_LESS_OR_EQUAL ...

  2. after I see Little Dorrit

    也许是我太追名逐利,所以我不肯承认自己花费了大把的时间看电影,通过写博客好像自己从中感悟到了什么,好像看电影也是一种学习的方式. 也许是我平静自内心的方式,我太忙于玩或者学习,甚至没有机会非常沉静 一 ...

  3. 个人学习笔记--MyBatis-的搭建及第一个程序

    1.导入Jar包 2.设置全局配置文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE c ...

  4. redis入门教程

    21) Redis 简介Redis 是一个开源的使用 ANSI C 语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value 数据库.2) 数据类型2.1. Redis 的 KeyRedi ...

  5. BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐

    Description The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is graz ...

  6. 对SVM的个人理解

    对SVM的个人理解 之前以为SVM很强大很神秘,自己了解了之后发现原理并不难,不过,“大师的功力在于将idea使用数学定义它,使用物理描述它”,这一点在看SVM的数学部分的时候已经深刻的体会到了,最小 ...

  7. PYTHON之DEF

    def sayHello(): print('Hello World!') while True: s = input('Enter something : ') if s == 'quit': br ...

  8. 读取spring配置文件的方法(spring读取资源文件)

    1.spring配置文件 <bean id="configproperties" class="org.springframework.beans.factory. ...

  9. linux svn服务器搭建、客户端操作、备份与恢复

    Subversion(SVN)是一个开源的版本控制系統,管理着随时间改变的数据.这些数据放置在一个中央资料档案库中,这个档案库很像一个普通的文件服务器,它会记住每一次文件的变动,这样就可以把档案恢复到 ...

  10. bzoj2333

    好题,先离线把连通块变成连续的区间每次连通块合并就相当于两个区间合并这样就轻易的用线段树解决了 type node=record wh:string[]; x,y:longint; end; ..*] ...