DELETE语句与REFERENCE约束冲突 [英] The DELETE statement conflicted with the REFERENCE constraint
本文介绍了DELETE语句与REFERENCE约束冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
The DELETE statement conflicted with the REFERENCE constraint "FK_User_History_Tbl_Customer". The conflict occurred in database "KHUB_DB", table "dbo.User_History", column 'UserId'.
The statement has been terminated.
推荐答案
您正试图从表中删除记录,该表在另一个表中有引用表。
这里REFERENCE
约束是FK_User_History_Tbl_Customer
。
表格是User_History
和Tbl_Customer
。
UserId
列由Tbl_Customer
表引用。
当您尝试从User_History
表中删除一行时,它会知道同一行在Tbl_Customer
表。
因此,您需要先从Tbl_Customer
表中删除然后从表User_History
中删除。
如果要删除一行,则必须执行按顺序执行两个查询。
You are trying to Delete the record from a Table which has a reference in another Table.
HereREFERENCE
constraint is "FK_User_History_Tbl_Customer
".
Tables areUser_History
andTbl_Customer
.
UserId
column is referenced byTbl_Customer
Table.
When you try to delete a row fromUser_History
Table, it comes to know that the same row has some related row inTbl_Customer
Table.
So, you need to delete fromTbl_Customer
Table first and then delete from TableUser_History
.
If you want to delete a row, then you have to execute the following two queries in sequence.
DELETE FROM Tbl_Customer
WHERE UserId = [[[UserIdToDelete]]];
DELETE FROM User_History
WHERE UserId = [[[UserIdToDelete]]]
上面的解决方案解释了问题所在,但没有明确解释如何修复它 - 可以在不编写任何SQL查询或其他代码的情况下完成。
最简单的解决方案是转到SSMS并打开数据库图
选择关系
然后在其属性中选择On Delete - cascade
然后一切都由SSMS完成
The solutions above explain what the problem is but do not explain explicitly how to fix it - which can be done without writing any SQL queries or other code.
The easiest solution is to go to SSMS and open the database diagram
select the relation
Then in its properties select On Delete - cascade
Then everything is done for you by SSMS
你好,
首先你必须删除来自外国表的记录然后你必须删除 主键表中的记录(主人表)。
祝福....
hello,
first you have to delete the record from the Foreign Table and then after you have to delete the record from the Primary Key Table(Master Table).
best wishes....
这篇关于DELETE语句与REFERENCE约束冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文