当我尝试使用 --local-infile 选项加载 CSV 数据文件时,我收到以下错误:
第 2 行的错误 1452 (23000):无法添加或更新子行:外键约束失败 (
realtax
.city_desc
, CONSTRAINTcity_desc_ibfk_1
FOREIGN KEY (cityid
) REFERENCEScity
(id
) ON DELETE CASCADE ON UPDATE CASCADE)
错误发生在 CSV 文件的第一行(即,根本没有加载任何行)。
这是实际的mysql加载命令:
load data LOCAL infile 'out/desc.in' into table city_desc fields terminated by ',' lines terminated by '\n';
这是 CSV 文件的第一行:
# head -1 out/desc.in
NULL,1000,"R4","B1","NWC125","SUNRISE ACRES #1 W 120 FT OF S 75",0.207
但是,当我使用 mysql CLI 时,我可以毫无问题地加载第一行。结果如下:
mysql> 插入 city_desc 值 (NULL,1000,"R4","B1","NWC125","SUNRISE ACRES #1 W 120 FT OF S 75",0.207);
查询正常,1 行受影响(0.04 秒)
这是它所引用的表数据行:
mysql> select * from city limit 1;
+--------+---------+-----------------+
| 编号 | 道具ID | pidn |
+--------+---------+-----------------+
| 1000 | 10208 | S912999001L0800 |
+--------+---------+-----------------+
1 行在集合中(0.00 秒)
以下是每个表的 SQL 定义:
create table if not exists city
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
prop_id int,
pidn varchar(100)
) AUTO_INCREMENT=1000, engine=innodb;
create table if not exists city_desc
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
cityid int not null,
zoning varchar(20),
state_cd varchar(20),
map_id varchar(20),
legal_desc varchar(100),
legal_acres numeric(5,5),
foreign key (cityid) references city(id)
on update cascade on delete cascade
) AUTO_INCREMENT=500000, engine=innodb;
create index propid_city on city(prop_id);
create index pidn_city on city(pidn);
我阅读了其他关于关闭引用检查的线程:
SET foreign_key_checks = 0;
但我不想破解修复程序,而是想知道手动工作但不能使用 --local-infile 选项的原因。外国参考文献在那里(记录 id 1000),为什么它不起作用?
更新:这是我在插入失败后立即运行 SHOW ENGINE INNODB STATUS 后收到的信息:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
121212 13:50:55 Transaction:
TRANSACTION 0 901853, ACTIVE 0 sec, process no 3217, OS thread id 140224990570240 inserting, thread declared inside InnoDB 492
mysql tables in use 1, locked 1
5 lock struct(s), heap size 1216, 9 row lock(s), undo log entries 9
MySQL thread id 15389, query id 1442612 localhost root
load data LOCAL infile 'out/desc.in' into table city_desc fields terminated by ',' lines terminated by '\n'
Foreign key constraint fails for table `realtax`.`city_desc`:
,
CONSTRAINT `city_desc_ibfk_1` FOREIGN KEY (`cityid`) REFERENCES `city` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
Trying to add in child table, in index `cityid` tuple:
DATA TUPLE: 2 fields;
0: len 4; hex 80000000; asc ;; 1: len 4; hex 800ea15e; asc ^;;
But in parent table `realtax`.`city`, in index `PRIMARY`,
the closest match we can find is record:
PHYSICAL RECORD: n_fields 5; compact format; info bits 0
0: len 4; hex 800003e8; asc ;; 1: len 6; hex 00000007aed1; asc ;; 2: len 7; hex 800000002d0110; asc - ;; 3: len 4; hex 800027e0; asc ' ;; 4: len 15; hex 533931323939393030314c30383030; asc S912999001L0800;;