简体   繁体   English

MySQL错误1452 - 无法插入数据

[英]MySQL Error 1452 - can't insert data

I am inserting some data into the following MySQL tables: 我将一些数据插入以下MySQL表:

CREATE TABLE genotype
(
Genotype VARCHAR(20),
Fitness FLOAT NULL,
Tally INT NULL,
PRIMARY KEY (Genotype)
)ENGINE=InnoDB;


CREATE TABLE gene
(
Gene VARCHAR(20),
E FLOAT NOT NULL,
Q2 FLOAT NOT NULL, 
PRIMARY KEY (Gene)
)ENGINE=InnoDB;

CREATE TABLE genotypegene
(
Genotype VARCHAR(20),
Gene VARCHAR(20),
FOREIGN KEY (Genotype) REFERENCES genotype(Genotype),
FOREIGN KEY (Gene) REFERENCES gene(Gene)
)ENGINE=InnoDB;

I inserted the data into genotype/gene first, but get the following error when trying to insert into genotypegene: 我首先将数据插入基因型/基因,但在尝试插入基因型时会出现以下错误:

ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`populationdb`.`genotypegene`, CONSTRAINT `genotypegene_ibfk_2` FOREIGN KEY (`Gene`) REFERENCES `gene` (`Gene`))

The data I'm inserting is into this table is: Genotype1,Gene1 Genotype1,Gene2 Genotype1,Gene3 Genotype1,Gene4 我在这个表中插入的数据是:Genotype1,Gene1 Genotype1,Gene2 Genotype1,Gene3 Genotype1,Gene4

There is one copy of Genotype1 in the genotype table, the idea being that each genotype can contain many genes, but each gene can exist in multiple genotypes (at the moment, there is only 1 genotype, but later I will insert more). 在基因型表中有一个基因型1的副本,其思想是每个基因型可以包含许多基因,但每个基因可以存在多种基因型(目前,只有1种基因型,但后来我会插入更多基因型)。 I read here that I could turn off Foreign Key Checks, but I am reluctant to do so without knowing the reason for this error. 在这里读到我可以关闭外键检查,但我不愿意在不知道这个错误的原因的情况下这样做。 Is this because there is only one copy of Genotype1 in the genotype table? 这是因为基因型表中只有一个Genotype1的副本吗? (I have checked that Genotype1, Gene1 etc. are in the same format/spelling in their primary key tables). (我已检查Genotype1,Gene1等在其主键表中的格式/拼写相同)。

Just in case, here is the code I am using to insert the data: 以防万一,这是我用来插入数据的代码:

 mysql> LOAD DATA LOCAL INFILE 'C:\\.....genotypegene.csv'
   -> INTO TABLE genotypegene
   -> FIELDS TERMINATED BY ','
   -> (Genotype, Gene);

Thanks 谢谢

One approach to find out what is causing this would be to do the following: 找出造成这种情况的原因之一是执行以下操作:

Disable Foreign Keys 禁用外键

SET FOREIGN_KEY_CHECKS = 0;

Load The Data 加载数据

Do this using your command: 使用您的命令执行此操作:

mysql> LOAD DATA LOCAL INFILE 'C:\\.....genotypegene.csv'
    -> INTO TABLE genotypegene
    -> FIELDS TERMINATED BY ','
    -> (Genotype, Gene);

Find Orphaned Data 查找孤立数据

select gtg.* from genotypegene gtg
where (gtg.Gene not in (select g.Gene from gene g) 
    or gtg.Genotype not in (select gt.Genotype from genotype gt));

This should give you a list of those rows that are causing your FK constraint violation. 这应该为您提供导致您的FK约束违规的那些行的列表。

Fix The Orphaned Data 修复孤立数据

Update them? 更新他们? Delete them? 删除它们? Fix them in the CSV? 将它们修复为CSV? Insert parent row into Gene table? 将父行插入Gene表? Do whatever is appropriate. 做任何适当的事。

Enable FK Checks 启用FK检查

SET FOREIGN_KEY_CHECKS = 1;

If nothing else this should give you a clue as to why your are getting the FK constraint violation error. 如果没有别的,这应该给你一个线索, 为什么你得到FK约束违规错误。

Good luck! 祝好运!

This error sometimes appears when the separator between fields coincides with some value that the field has. 当字段之间的分隔符与字段具有的某个值一致时,有时会出现此错误。 Example: If the separator is the comma (,). 示例:如果分隔符是逗号(,)。 Possibly some field has this comma and believes it is a field change. 可能有些字段有这个逗号,并认为这是一个字段变化。 Check these cases. 检查这些情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM