当我有外键时MySQL插入多个值

时间:2012-09-22 04:34:40

标签: mysql foreign-keys

我已经停了好几个小时,我希望得到你的帮助。

我有两个数组:

CREATE TABLE if not exists chr(
id_chr int(10) NOT NULL AUTO_INCREMENT,
number int(10),
genename varchar(30) not null,
ensembleid varchar(30) not null,
strand int(10) not null,
band varchar(10) not null, 
genestart int(30) not null,
geneend int(30) not null,
biotype varchar(30),
status varchar(10),
transcriptsnum int(10),
gc float(10) not null,
PRIMARY KEY(id_chr)
);

CREATE TABLE if not exists gene(
id_gene int(10) NOT NULL AUTO_INCREMENT,
name varchar(30) not null,
transcriptid int(30) not null,
idchr int(10) not null,
PRIMARY KEY(id_gene)
);

到目前为止,我已经将值插入chr表(大约8000行),我也希望对表基因进行多次插入。

我收到错误:

Error: Cannot add or update a child row: a foreign key constraint fails

问题是,如何插入foreign key的值,因为我必须插入多行?

1 个答案:

答案 0 :(得分:0)

Try this:

INSERT INTO gene(name,transcriptid,idchr)
select value1,value2,id_chr from char