2014-09-05 81 views
0

當我嘗試excecute這個代碼,我得到錯誤的DELETE語句:衝突與限制參考

用限制性參考「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 

回答

1

看起來Foreign Key constraint存在於給定表格中的user_codePrintForm.Options列和code/user_code列之間。

如果您嘗試刪除給定表中的所有數據,將會發生錯誤,因爲PrintForm.Options中的user_code列引用了要從中刪除數據的表中的任何一個表中的數據。

要解決此問題,您應該刪除並重新創建約束FK_Options_users或從子表PrintForm.Options刪除外鍵引用即user_code = 24的數據。

1

它看起來像你對我要刪除用戶24,但PrintForm.Options表具有一個仍然使用它的條目,並刪除它,外鍵將不再是satisifed。

您是否錯過了刪除查詢列表中的「Printform.Options」?

1

您與您嘗試刪除的行之一有外鍵關係。這意味着密鑰在另一個表中使用。您必須按照正確的順序刪除,以免發生。

您錯過了在錯誤中指定的元素的刪除。因此,在表PrintForm.Options中的數據庫WINTOUR中,use_code是您要刪除的用戶代碼的外鍵。

所以你需要也許是對之前或之後Settings.Items添加

delete from PrintForm.Options where user_code = @USER_CODE