2013-02-28 151 views
3

我有一個表ParentTableid, Name, town)及其子表humanmother, father)其中兩列包含映射到父表。SQL Server 2005外鍵級聯

父表具有town列,其中包含外鍵引用Towntownid, townname),並且該城鎮添加了外鍵引用。

現在的問題是我需要給人表的級聯約束,但在SQL Server多列中爲同一父表不能給出級聯。因此,我創建觸發器這樣,

create trigger DEL_Parent            
ON Parent 
instead of delete as  
    set nocount on  

    delete from human  
    where mother IN (select id from deleted)   
     or father IN (select id from deleted) 

delete from Parent where id in(select id from deleted). 

但是當我嘗試執行觸發時,系統顯示以下錯誤

無法創建,而不是刪除或代替更新觸發 DEL_Parent上表家長。這是因爲該表具有帶級聯刪除或更新的外鍵 鍵。

如何解決問題?提前致謝。

回答

2

修改查詢爲:

create trigger DEL_Parent            
ON Parent 
after delete as  
    set nocount on  

    delete 
    from human  
    where mother IN (select id from deleted)   
    or father IN (select id from deleted) 

    delete 
    from Parent 
    where id in(select id from deleted) 

因爲INSTEAD OF DELETE觸發器不能在具有與DELETE動作定義的外鍵的表中定義。