2010-04-20 66 views
1

我有一个多行插入,看起来像:确定哪些多行插入的值失败外键约束

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

假设第一属性(1,4,7)为外键到另一个表,并假定这个引用的表没有值'4'。一个MySQLExeption被抛出,错误代码1452

例外:不能添加或更新子行,外键约束失败(dbName/tableName,约束id外键(customer_id)参考文献referencedTablecustomer_id))

有一种确定哪个值导致错误的方法?我很乐意给我的用户的错误消息是这样说:

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

我使用的.NET MySQL连接到执行插入。

Thanks-

乔纳森

回答

0

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

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

看来,应该有一个更清洁的方式...