2012-10-08 42 views
1

我在向SQL Server中的外键添加级联删除时遇到问题。表A有三列。表A中的第1列和第2列是对表B中同一列的外键查找。我希望删除表B中的一行,以基于这些外键对表A上的行上的删除进行级联。SQL Server外键引起周期或多个级联路径

表A中的另一列有一个外键查找表C.如果表C中的行被删除,那么我想对应的单元格设置为空表A

当我加入这些限制我引发了错误:

引入表'RelatedDeliverableUnit'上的FOREIGN KEY约束'FK_RDU_TODELIVERABLEUNITREF'可能会导致循环或多个级联路径。指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

我对此有点坚持,Oracle似乎对这个逻辑非常满意。我使用Liquibase添加了这些限制。我认为错误是记在我的逻辑,而不是语法,但为了完整性这里是管理外键liquidbase脚本:

<addForeignKeyConstraint constraintName="FK_RDU_FROMDELIVERABLEUNITREF" baseTableName="relatedDeliverableUnit" 
          baseColumnNames="FROMDELIVERABLEUNITREF" referencedTableName="DELIVERABLEUNIT" referencedColumnNames="DELIVERABLEUNITREF" onDelete="CASCADE"/> 

    <addForeignKeyConstraint constraintName="FK_RDU_TODELIVERABLEUNITREF" baseTableName="relatedDeliverableUnit" 
          baseColumnNames="TODELIVERABLEUNITREF" referencedTableName="DELIVERABLEUNIT" referencedColumnNames="DELIVERABLEUNITREF" onDelete="CASCADE"/>       

    <addForeignKeyConstraint constraintName="FK_RDU_RELATIONSHIPREF" 
          baseTableName="relatedDeliverableUnit" baseColumnNames="RELATIONSHIPREF" referencedTableName="RELATIONSHIPTYPES" referencedColumnNames="RELATIONSHIPREF" onDelete="SET NULL"/>      

预先感谢任何帮助

回答

2

我无法找到相应的文件为更高版本,但SQL Server 2000 BOL解决此问题:

The series of cascading referential actions triggered by a single DELETE or UPDATE must form a tree containing no circular references. No table can appear more than once in the list of all cascading referential actions that result from the DELETE or UPDATE. The tree of cascading referential actions must not have more than one path to any given table. Any branch of the tree is terminated when it encounters a table for which NO ACTION has been specified or is the default.

而后来的版本并没有改变这一点。你落下的这个犯规:我知道做到这一点

The tree of cascading referential actions must not have more than one path to any given table

的唯一途径是实现B和A使用INSTEAD OF触发,而不是使用ON DELETE...之间的瀑布之一。

表A和C之间的关系不应受到任何这种影响。


2008 BOL

+0

谢谢你的建议。我也想知道添加这是一个单一的外键,两列查找相同的引用列,但我不确定这是否允许。我得看看我猜的触发器! – Fraser

相关问题