• 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_infoG;
    *************************** 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 ordersG;
    *************************** 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 ordersG;
    *************************** 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)
  • 相关阅读:
    Linux0.11内核--fork进程分析
    Linux0.11内核--内存管理之1.初始化
    Linux0.11内核--进程调度分析之2.调度
    Linux0.11内核--进程调度分析之1.初始化
    github
    推荐大家一个靠谱的论文检测平台。重复的部分有详细出处以及具体修改意见,能直接在文章上做修改,全部改完一键下载就搞定了。他们现在正在做毕业季活动, 赠送很多免费字数,可以说是十分划算了!地址是:https://www.paperpass.com/
    妈妈再也不用担心我找idea激活码了
    eclipse集成tomcat
    DNS--localhost
    RFC 2819)第5节"Definitions"除外的全部内容
  • 原文地址:https://www.cnblogs.com/shootercheng/p/6063123.html
Copyright © 2020-2023  润新知