Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL cannot create foreign key constraint

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?

like image 295
numfar Avatar asked Feb 03 '14 11:02

numfar


People also ask

Can not add foreign key constraint MySQL?

The usual cause are generally a mismatch in the type of the column of the primary table and the foreign table. It can also be a mismatch in the Engine type of two tables i.e. MyISAM or InnoDB. Datatype both columns should have same datatype. int(11) on one table and smallint(5) on another will cause problem.

Why do foreign key constraints fail?

The error comes when you are trying to add a row for which no matching row in in the other table. “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.

What is error code 1072 in MySQL?

1072 - Key column 'UserID' doesn't exist in table.


2 Answers

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.

like image 180
Austen Hoogen Avatar answered Sep 22 '22 20:09

Austen Hoogen


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

like image 30
Pratik Singhal Avatar answered Sep 21 '22 20:09

Pratik Singhal