我对MySQl相当陌生,当出现此错误时,我想用键连接两个表。我浏览了一些相关的问题,但似乎找不到问题所在。

首先,我创建了此表,它可以工作:

CREATE TABLE uniquepeps (ID INT NOT NULL, Sequence CHAR(30)
NOT NULL, PRIMARY KEY (Sequence));
LOAD DATA LOCAL INFILE "D://...//Uniquepeps.txt" INTO TABLE uniquepeps;


该表如下所示,并且可以正常工作:

1   AAAATTTTTGGGGGG
2   GGGGGUUUUUKKK
3   OOOOPPPPMMMNN


然后我创建了另一个表:

CREATE TABLE peps (ID INT NOT NULL, Sequencepeps CHAR(30)
NOT NULL, PROTID INT NOT NULL, PRIMARY KEY (ID), CONSTRAINT
Seq_fk FOREIGN KEY (Sequencepeps) references uniquepeps(Sequence));


但是,当我尝试加载数据却无法正常工作时,出现了一个错误

LOAD DATA LOCAL INFILE "D://...//Pepsandprots.txt" INTO TABLE peps;
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`biology`.`peps`, CONSTRAINT `Seq_fk` FOREIGN KEY (`Sequencepeps`) REFERENCES `uniquepeps` (`Sequence`) )


第二个表的数据为:

1       AAAATTTTTGGGGGG 1
2       AAAATTTTTGGGGGG 2
3       AAAATTTTTGGGGGG 3
4       AAAATTTTTGGGGGG 4
5       GGGGGUUUUUKKK   1
6       GGGGGUUUUUKKK   8
7       OOOOPPPPMMMNN   9
8       OOOOPPPPMMMNN   25


任何帮助表示赞赏!

最佳答案

该错误意味着,您试图将值插入表Sequencepeps中的peps中,而该值在表uniquepeps中的列Sequence中不存在。

仔细观察,您会发现输入文件可能存在问题,在您编写uniquepepsGGGGGUUUUUKKK中,但是在peps文件中,您有两个不同的相似名称:GGGGGUUUUUKKK(这是正确的)和GGGGUUUUUKKK(缺少G)-也许这是您的错字?

09-20 09:54