MySQL无法添加或更新子行
我有两张表:TBL类型特征和tblAircrafts TBL特征定义:MySQL无法添加或更新子行,mysql,csv,Mysql,Csv,我有两张表:TBL类型特征和tblAircrafts TBL特征定义: create table if not exists tblACTypeCharacteristics( idAC_type varchar(255) not null, numPassengers int, primary key(idAC_type)); create table if not exists tblAircrafts( idAC int not null auto_inc
create table if not exists tblACTypeCharacteristics(
idAC_type varchar(255) not null,
numPassengers int,
primary key(idAC_type));
create table if not exists tblAircrafts(
idAC int not null auto_increment,
txtAC_tag varchar(255) not null,
txtAC_type varchar(255) not null,
primary key(idAC, txtAC_tag));
tblAircrafts定义:
create table if not exists tblACTypeCharacteristics(
idAC_type varchar(255) not null,
numPassengers int,
primary key(idAC_type));
create table if not exists tblAircrafts(
idAC int not null auto_increment,
txtAC_tag varchar(255) not null,
txtAC_type varchar(255) not null,
primary key(idAC, txtAC_tag));
此外,我还添加了一个外键,如下所示:
alter table tblaircrafts add foreign key(txtAC_type)
references tblactypecharacteristics(idAC_type);
load data local infile 'C:\\Users\\t_lichtenberger\\Desktop\\tblAircrafts.csv'
into table tblaircrafts
character set utf8
fields terminated by ';'
lines terminated by '\n'
ignore 1 lines;
在TBL特征中,定义了每种类型飞机的最大乘客数量。
tblAircraft中列出了所有可用的飞机。
我可以通过键入以下内容插入新飞机:
insert into tblaircrafts (txtAC_tag, txtAC_type) values ('OE-LDA','A319-112');
但是由于周围有很多飞机,我不想手工添加每一架。
我想通过csv文件导入它们(我有一些飞机的列表)。
我按如下方式导入它:
alter table tblaircrafts add foreign key(txtAC_type)
references tblactypecharacteristics(idAC_type);
load data local infile 'C:\\Users\\t_lichtenberger\\Desktop\\tblAircrafts.csv'
into table tblaircrafts
character set utf8
fields terminated by ';'
lines terminated by '\n'
ignore 1 lines;
但是,当我想将.csv文件导入tblaircraft表时,出现以下错误:
15:08:37 alter table tblaircrafts add foreign key(txtAC_type) references tblactypecharacteristics(idAC_type) Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`pilotproject`.`#sql-11d0_2da`, CONSTRAINT `#sql-11d0_2da_ibfk_1` FOREIGN KEY (`txtAC_type`) REFERENCES `tblactypecharacteristics` (`idAC_type`)) 0.641 sec
我无法解释为什么。列数相同,列的数据类型相同。我已经仔细检查了csv中不在TBL类型特征表中的AC_类型,它应该是好的
csv文件的前几行如下所示:
alter table tblaircrafts add foreign key(txtAC_type)
references tblactypecharacteristics(idAC_type);
load data local infile 'C:\\Users\\t_lichtenberger\\Desktop\\tblAircrafts.csv'
into table tblaircrafts
character set utf8
fields terminated by ';'
lines terminated by '\n'
ignore 1 lines;
有没有关于错误仍然发生的建议?
提前非常感谢 我终于找到了解决办法。我只是通过执行
SET foreign_key_checks = 0;
在设置外键之前,它工作了!之后,我可以添加csv记录。一种有效的隔离问题的方法是删除或停用外键,然后导入行。假设导入成功,则可以执行查询以查找哪些子行的键不在父表中。是否能够在之后重新启用外键检查?是,添加记录后,我将外键检查设置回1