1

我有一个看起来像这样的多行插入:

insert into table VALUES
(1, 2, 3),
(4, 5, 6),
(7, 8, 9);

假设第一个属性 (1, 4, 7) 是另一个表的外键,并假设这个引用的表没有值“4”。引发 MySQLExeption,错误代码为 1452。

例外:无法添加或更新子行:外键约束失败 ( dbName/tableName, CONSTRAINT idFOREIGN KEY ( customer_id) REFERENCES referencedTable( customer_id))

有没有办法确定哪个值导致了错误?我很想给我的用户一个错误消息,内容如下:

Error: '4' does not exist in the referenced table. 

我正在使用 .NET mysql 连接器来执行插入。

谢谢-

乔纳森

4

1 回答 1

0

一种选择可能是首先查询引用的表。

select id from referencedTable where id not in (1, 4, 7)

似乎应该有一个更清洁的方法......

于 2010-04-20T02:26:30.760 回答