DELETE语句与REFERENCE约束冲突 [英] The DELETE statement conflicted with the REFERENCE constraint

查看:1737
本文介绍了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.

Here REFERENCE constraint is "FK_User_History_Tbl_Customer".

Tables are User_History and Tbl_Customer.
UserId column is referenced by Tbl_Customer Table.

When you try to delete a row from User_History Table, it comes to know that the same row has some related row in Tbl_Customer Table.

So, you need to delete from Tbl_Customer Table first and then delete from Table User_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆