使用限制REFERENCE冲突DELETE语句

时间:2014-09-05 11:08:22

标签: sql

当我尝试执行此代码时,我收到错误:

使用限制REFERENCE“FK_Options_users”冲突DELETE语句。冲突发生在数据库“WINTOUR”,表“PrintForm.Options”,列'user_code'

无法理解为什么以及如何解决这个问题。

declare

  @USER_CODE int;

select

  @USER_CODE = 24;

delete from Settings.Items where user_code =   @USER_CODE

delete from usnet where code =   @USER_CODE

delete from usgroups where usercode =   @USER_CODE

delete from users where code =   @USER_CODE

3 个答案:

答案 0 :(得分:1)

在我看来,您正在删除用户24,但PrintForm.Options表中有一个仍在使用它的条目,如果要将其删除,外键将不再令人满意。

你有没有错过" Printform.Options"从删除查询列表?

答案 1 :(得分:1)

您与要删除的行之一有外键关系。这意味着密钥在另一个表中使用。您必须以正确的顺序删除,以免发生。

您错过了错误中指定的元素的删除。因此,在表PrintForm.Options中的数据库WINTOUR中,use_code是要删除的用户代码的外键。

所以你需要添加

 delete from PrintForm.Options where user_code = @USER_CODE

可能就在Settings.Items之前或之后。

答案 2 :(得分:1)

Foreign Key constraint中的user_code列与给定表格中的PrintForm.Options列之间似乎存在code/user_code

如果您尝试删除给定表中的所有数据,则会发生错误,因为user_code中的PrintForm.Options列引用了表中任何一个删除数据的数据。

要解决此问题,您应该删除并重新创建约束FK_Options_users或删除外键引用的子表PrintForm.Options中的数据,即user_code = 24