我将数据从txt文件加载到我的表中时出现问题
表:
CREATE TABLE s_schueler (
s_schnr int(20) NOT NULL AUTO_INCREMENT,
s_name varchar(30) NOT NULL,
s_k_klasse varchar(10) NOT NULL,
KEY s_k_klasse (s_schnr),
CONSTRAINT s_schueler_ibfk_1 FOREIGN KEY (s_k_klasse) REFERENCES k_klassen (k_id)
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=latin1;
CREATE TABLE `k_klassen` (
`k_id` char(4) NOT NULL,
`k_bez` varchar(30) NOT NULL,
PRIMARY KEY (`k_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
查询:
LOAD DATA
INFILE 'C:/Users/User/Desktop/SQL-schueler2.txt'
INTO TABLE s_schueler
FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n'
(s_name,s_k_klasse);
由于我在Auto_INCREMENT上有我的ID,所以不要将它用作参数
文字档案:
Ruben 01VL
Sean 03TA
Paul 03TB
错误:
错误代码:1452。无法添加或更新子行:外键约束失败(test
。s_schueler
,CONSTRAINT s_schueler_ibfk_1
FOREIGN KEY(s_k_klasse
)参考k_klassen
(k_id
)ON UPETE CASCADE ON UPDATE CASCADE)
答案 0 :(得分:0)
关于LOAD DATA INFILE的MySQL参考手册:
要在加载操作期间忽略外键约束,请在执行LOAD DATA之前发出SET foreign_key_checks = 0语句。