2016-11-29 140 views
1

我得到的錯誤DELETE語句衝突參考約束

DELETE語句衝突與基準約束 FK_DocYDocument1 - 表DocYDocument,列SubDocID - 聲明: DELETE FROM文檔WHERE(?ID =)參數: 'D7FAA3CF ......'

表DocYDocument有列

PK ID, FK DocID and FK SubDocID. 
And the keys PK_DocYDocument, FK_DocYDocument and FK_DocYDocument1. 

FK_DocYDocument1外鍵列是SubDocID,而FK_DocYDocument外鍵列是DocID。

數據庫設計是否存在問題,或者我是否在程序中尋找錯誤?

回答

5

您有DocYDocument中的行指的是您嘗試刪除的DOCUMENT

你必須要麼匹配DocID

DELETE FROM DocYDocument WHERE DocID = ? 
DELETE FROM DOCUMENT WHERE ID = ? 

刪除DocYDocument行或改變約束要做到這一點自動

ALTER TABLE DocYDocument 
    DROP CONTRAINT FK_DocYDocument; 
ALTER TABLE DocYDocument 
    ADD CONSTRAINT FK_DocYDocument 
    FOREIGN KEY (DocID) 
    REFERENCES DOCUMENT (ID) 
    ON DELETE CASCADE; 

如果DocID是空的,你可以改爲做ON DELETE SET NULL,如果你希望。

相關問題