MySQL添加外键时报错 ERROR 1215 (HY000): Cannot add foreign key constraint

时间:2022-09-22 18:00:04

    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 '0',
  `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)