mysql 无法建立外键约束_mysql - MySQL无法创建外键约束 - 堆栈内存溢出

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

我有表格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 | |

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

我不想使用以下内容创建一个名为sample_df的新表引用它:

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是什么意思?

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值