MySQL cannot create foreign key constraint

后端 未结 12 2144
夕颜
夕颜 2020-12-08 12:39

I\'m having some problems creating a foreign key to an existing table in a mysql database.

I have the table exp:

+-------------+--------         


        
12条回答
  •  醉梦人生
    2020-12-08 13:15

    According to http://dev.mysql.com/doc/refman/5.5/en/create-table-foreign-keys.html

    MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order.

    InnoDB permits a foreign key to reference any index column or group of columns. However, in the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.

    So if the index in referenced table is exist and it is consists from several columns, and desired column is not first, the error shall be occurred.

    The cause of our error was due to violation of following rule:

    Corresponding columns in the foreign key and the referenced key must have similar data types. The size and sign of integer types must be the same. The length of string types need not be the same. For nonbinary (character) string columns, the character set and collation must be the same.

提交回复
热议问题