2017-04-11 72 views
0

我有兩個表:tblACTypeCharacteristics和tblAircrafts。MySQL無法添加或更新子行

tblACTypeCharacteristics定義:

create table if not exists tblACTypeCharacteristics(
idAC_type  varchar(255) not null, 
numPassengers int, 
primary key(idAC_type)); 

tblAircrafts定義:

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); 

在tblACTypeCharacteristics,最大數量爲每種類型的飛機定義乘客。在tblAircraft中都列出了可用的飛機。 我可以通過鍵入例如插入一個新的飛機:

insert into tblaircrafts (txtAC_tag, txtAC_type) values ('OE-LDA','A319-112'); 

但由於周圍有飛機的負載,我不想手工每一個補充。 我想通過csv文件導入它們(我確實有幾架飛機的列表)。 我導入它如下:

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針對的arent的tblACTypeCharacteristic表中,它應該是不錯的AC_types ..

CSV文件的樣子,隨後的前幾行:

first few lines of the aircraft csv

任何建議爲什麼錯誤仍然存​​在? 非常感謝你提前!

+1

解決問題的一種有用方法是刪除或取消激活外鍵,然後導入行。假設它成功導入,然後可以執行查詢來查找哪些子行具有不在父表中的鍵。 – Bampfer

回答

0

我終於找到了解決辦法。我只是通過執行禁用外鍵檢查

SET foreign_key_checks = 0; 

在設置外鍵和它的工作之前!之後我能夠添加csv記錄。

+0

您是否可以將外鍵檢查重新打開? – Bampfer

+0

是的,當我添加記錄時,我將外鍵檢查重新設置爲1 – user7335295

相關問題