mysql 无法建立外键约束_MySQL无法创建外键约束

bd96500e110b49cbb3cd949968f18be7.png

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?

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值