当我尝试执行此代码时,我收到错误:
使用限制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
答案 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
。