mysql 创建外键索引吗_索引-MySQL无法创建外键约束
我在為mysql數(shù)據(jù)庫中的現(xiàn)有表創(chuàng)建外鍵時遇到一些問題。
我有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 | |
+-------------+------------------+------+-----+---------+-------+
而且我不想使用以下方法創(chuàng)建一個新的表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列設(shè)置為非null,但這并不能解決問題)所以我想問題可能出在Cannot find an index in the referenced table where the referenced columns appear as the first columns。但是我 我不太清楚這意味著什么,或者如何檢查/修復(fù)它。 有沒有人有什么建議? first columns是什么意思?
總結(jié)
以上是生活随笔為你收集整理的mysql 创建外键索引吗_索引-MySQL无法创建外键约束的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 顺丰2019年获得一条新航线 可能会成为
- 下一篇: linux cmake编译源码,linu