MySQL cannot create foreign key constraint

匿名 (未验证) 提交于 2019-12-03 02:49:01

问题:

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

I have the table exp:

+-------------+------------------+------+-----+---------+-------+ | Field       | Type             | Null | Key | Default | Extra | +-------------+------------------+------+-----+---------+-------+ | EID         | varchar(45)      | NO   | PRI | NULL    |       | | Comment     | text             | YES  |     | NULL    |       | | Initials    | varchar(255)     | NO   |     | NULL    |       | | ExpDate     | date             | NO   |     | NULL    |       | | InsertDate  | date             | NO   |     | NULL    |       | | inserted_by | int(11) unsigned | YES  | MUL | NULL    |       | +-------------+------------------+------+-----+---------+-------+ 

and I wan't to create a new table called sample_df referencing this, using the following:

CREATE TABLE sample_df ( df_id mediumint(5) unsigned AUTO_INCREMENT primary key, sample_type mediumint(5) unsigned NOT NULL, df_10 BOOLEAN NOT NULL, df_100 BOOLEAN NOT NULL, df_1000 BOOLEAN NOT NULL, df_above_1000 BOOLEAN NOT NULL, target INT(11) unsigned NOT NULL, assay MEDIUMINT(5) unsigned zerofill NOT NULL, insert_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, inserted_by INT(11) unsigned NOT NULL, initials varchar(255), experiment VARCHAR(45), CONSTRAINT FOREIGN KEY (inserted_by) REFERENCES user (iduser), CONSTRAINT FOREIGN KEY (target) REFERENCES protein (PID), CONSTRAINT FOREIGN KEY (sample_type) REFERENCES sample_type (ID), CONSTRAINT FOREIGN KEY (assay) REFERENCES assays (AID), CONSTRAINT FOREIGN KEY (experiment) REFERENCES exp (EID) ); 

But I get the error:

ERROR 1215 (HY000): Cannot add foreign key constraint 

To get some more information I did:

SHOW ENGINE INNODB STATUS\G 

From which I got:

FOREIGN KEY (experiment) REFERENCES exp (EID) ): Cannot find an index in the referenced table where the referenced columns appear as the first columns, or column types in the table and the referenced table do not match for constraint. 

To me the column types seem to match, since they are both varchar(45).(I also tried setting the experiment column to not null, but this didn't fix it) So I guess the problem must be that Cannot find an index in the referenced table where the referenced columns appear as the first columns. But I'm not quite sure what this means, or how to check/fix it. Does anyone have any suggestions? And what is meant by first columns?

SOLUTION:

I figured out my problem. I had a different collation for the exp table (utf8_unicode_ci) then the default for the database(utf8_general_ci). So adding

CHARACTER SET utf8 COLLATE utf8_unicode_ci 

at the end of the table creation query fixed my problem.

But I'm still curious about what is meant by first columns. Is it something to do with indexes?

回答1:

Just throwing this into the mix of possible causes, I ran into this when the referencing table column had the same "type" but did not have the same signing.

In my case, the referenced table colum was TINYINT UNSIGNED and my referencing table column was TINYINT SIGNED. Aligning both columns solved the issue.



回答2:

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.



回答3:

Solution suggested by @austen-hoogen has resolved my issue. But due to low reputation scores I am unable to vote and comment for his solution. In my case Primary key was integer, auto increment and unsigned. But foreign key (column of referencing table) was signed type. I changed it to unsigned and able to create a relation successfully.



回答4:

This error can also occur, if the references table and the current table don't have the same character set.



回答5:

As mentioned @Anton, this could be because of the different data type. In my case I had primary key BIGINT(20) and tried to set foreight key with INT(10)



回答6:

Referencing the same column more than once in the same constraint also produces this Cannot find an index in the referenced table error, but can be difficult to spot on large tables. Split up the constraints and it will work as expected.



回答7:

In my case, it turned out the referenced column wasn't declared primary or unique.

https://stackoverflow.com/a/18435114/1763217



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!