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

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

    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.

提交回复
热议问题