MySQL : ERROR 1215 (HY000): Cannot add foreign key constraint

后端 未结 16 1786
误落风尘
误落风尘 2020-11-30 01:37

I have read Database system concepts, 6th edition, Silberschatz. I\'m going to implement the university database system shown in chapter 2 on OS X

相关标签:
16条回答
  • 2020-11-30 02:10

    Below code worked for me

    set @@foreign_key_checks=0;
    ALTER TABLE  `table1` ADD CONSTRAINT `table1_fk1` FOREIGN KEY (`coloumn`) REFERENCES `table2` (`id`) ON DELETE CASCADE;
    
    0 讨论(0)
  • 2020-11-30 02:12

    When you get this vague error message, you can find out the more specific error by running

    SHOW ENGINE INNODB STATUS;
    

    The most common reasons are that when creating a foreign key, both the referenced field and the foreign key field need to match:

    • Engine should be the same e.g. InnoDB
    • Datatype should be the same, and with same length.
      e.g. VARCHAR(20) or INT(10) UNSIGNED
    • Collation should be the same. e.g. utf8
    • Unique - Foreign key should refer to field that is unique (usually private) in the reference table.

    Another cause of this error is:
    You have defined a SET NULL condition though some of the columns are defined as NOT NULL.

    0 讨论(0)
  • 2020-11-30 02:12

    I came across the same issue as well. Not sure why this is working but it indeed works: Try add ENGINE INNODB after your create query.

    mysql> create table course
    -> (course_id varchar(7),
    -> title varchar (50),
    -> dept_name varchar(20),
    -> credits numeric(2,0),
    -> primary key(course_id),
    -> foreign key (dept_name) references department) ENGINE INNODB;
    
    0 讨论(0)
  • 2020-11-30 02:12

    I had this error when I tried to import (in MysqlWorkbench) from a PhpAdminMySQL export. After verifying I had disabled the unique keys and foreign keys with:

    SET unique_checks=0;
    SET foreign_key_checks = 0;
    

    I still get the same error (MySQL : ERROR 1215 (HY000): Cannot add foreign key constraint). The error occurred on this create statement.

    DROP TABLE IF EXISTS `f1_pool`;
    CREATE TABLE `f1_pool` (
     `id` int(11) NOT NULL,
     `name` varchar(45) NOT NULL,
     `description` varchar(45) DEFAULT NULL COMMENT 'Optional',
     `ownerId` int(11) NOT NULL,
     `lastmodified` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    

    No foreign key or unique index, so what is wrong here? Finally (after 90 minutes puzzling) I decided to restart MySQL and do the import again with one modification: I dropped all tables before doing the import. And there was no error, all functioned, tables and views restored. So my advice, if all looks ok, first try to restart MySQL!

    0 讨论(0)
  • 2020-11-30 02:17
    CONSTRAINT vendor_tbfk_1 FOREIGN KEY (V_CODE) REFERENCES vendor (V_CODE) ON UPDATE CASCADE
    

    this is how it could be... look at the referencing column part. (V_code)

    0 讨论(0)
  • 2020-11-30 02:19

    I don't see anyone stating this explicitly and I had this same error message and my problem was that I was trying to add a foreign key to a TEMPORARY table. Which is disallowed as noted in the manual

    Foreign key relationships involve a parent table that holds the central data values, and a child table with identical values pointing back to its parent. The FOREIGN KEY clause is specified in the child table. The parent and child tables must use the same storage engine. They must not be TEMPORARY tables.

    (emphasis mine)

    0 讨论(0)
提交回复
热议问题