【发布时间】:2016-12-19 10:55:14
【问题描述】:
在循环中删除父子节点
表 1(父表)
Id int
表2(关系表)
Id1 int FOREIGN KEY (Id1) REFERENCES Table1 (Id)
Id2 int FOREIGN KEY (Id2) REFERENCES Table1 (Id)
Id - Id1 one to one or one to zero relationship
Id - Id2 one to many
表1中的数据
Id
1
2
3
4
5
6
7
8
9
10
表2中的数据
Id1 Id2
2 1
3 1
4 2
5 2
6 4
7 4
8 5
9 5
所以它就像一棵树,根为 1
1 has two childs 2 and 3
2 has two childs 4 and 5
4 has two childs 6 and 7
5 has two childs 8 and 9
3,6,7,8,9,10 has no child
实现以下情况的最佳方法:
删除1 => 删除完整的table2和table1(表1中的10除外)
【问题讨论】:
标签: sql sql-server triggers sql-delete cascading-deletes