3

我有一个表ParentTable( id, Name, town) 及其子表human( mother, father),其中两列都包含到父表的映射。

父表的列town包含带有Town( townid, 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). 

但是当我尝试执行触发器时,系统显示以下错误

无法在表 Parent 上创建而不是删除或更新触发器 DEL_Parent。这是因为该表有一个具有级联删除或更新的外键。

如何解决问题?提前致谢。

4

2 回答 2

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操作定义的外键的表上定义触发器。

于 2013-02-28T04:14:22.360 回答
0

不能在具有使用 DELETE 操作定义的外键的表上定义 INSTEAD OF DELETE 触发器。

来源: http: //msdn.microsoft.com/en-us/library/ms191208 (v=sql.90).aspx

所以,我想如果你愿意的话,如果你仍然想强制执行它,你将不得不放弃cascade delete并用触发器来处理它。

于 2013-02-28T04:06:33.853 回答