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

后端 未结 16 1953
误落风尘
误落风尘 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: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!

提交回复
热议问题