失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > mysql 创建外键索引吗_索引-MySQL无法创建外键约束

mysql 创建外键索引吗_索引-MySQL无法创建外键约束

时间:2021-01-23 05:12:15

相关推荐

mysql 创建外键索引吗_索引-MySQL无法创建外键约束

我在为mysql数据库中的现有表创建外键时遇到一些问题。

我有experiment表:

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

| 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 | |

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

而且我不想使用以下方法创建一个新的表experiment引用此表:

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)

);

但是我得到了错误:

ERROR 1215 (HY000): Cannot add foreign key constraint

为了获得更多信息,我做了:

SHOW ENGINE INNODB STATUS\G

从中我得到:

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.

对我来说,列类型似乎是匹配的,因为它们都是varchar(45)。(我还尝试将experiment列设置为非null,但这并不能解决问题)所以我想问题可能出在Cannot find an index in the referenced table where the referenced columns appear as the first columns。但是我 我不太清楚这意味着什么,或者如何检查/修复它。 有没有人有什么建议? first columns是什么意思?

如果觉得《mysql 创建外键索引吗_索引-MySQL无法创建外键约束》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。