SQL - 更新语句的外键约束错误

时间:2012-11-02 14:05:10

标签: sql sql-server sql-server-2008 tsql

我正在尝试更新我的表(产品)列 - ProductID(FK)和ProductType(FK)。这两列与MProduct表的列MProductID(PK)和MProductType(PK)有关。

我正在尝试更新

ProductID       ProductType
9999            11
9999            12

但我收到以下错误

The UPDATE statement conflicted with the FOREIGN KEY constraint "FKHCR_MProduct".
The conflict occurred in database "XXXX", table "dbo.MProduct".

我检查了MProduct表,它有查找的所有有效记录。我不能改变我的表结构。

我也试过这个查询

Select ProductID,ProductType
from dbo.Product P
Left Join dbo.MProduct M on M.ProductID = P.ProductID
   and M.ProductType = P.ProductType
where M.ProductType is null
   and M.ProductID is null

并且没有输出,表示查找表中没有空数据。

1 个答案:

答案 0 :(得分:3)

在所有可能的情况下,您要么尝试在表中插入NULL值,要么在参考表中不存在值。

大多数情况下,这是因为您尝试在字段中插入NULL值。