我有一個表ParentTable
,其中我有parentId
作爲主鍵。在SQL Server 2008中對錶進行層級刪除
現在我有三個子表ChildTable1
,ChildTable2
和ChildTable3
。
- 在
ChildTable1
,主鍵是
ch1Id
和外鍵是fkparentId
(PK從ParentTable
) - 在
ChildTable2
,主鍵是
ch2Id
和外鍵是fkparentId
和fkch1Id
(從ChildTable1
主鍵) in
ChildTable3
,主鍵是ch3Id
,外鍵是fkparentId, fkch1Id
和fkch2Id
(來自ChildTable2
的主鍵)。
而且這樣下去..
我想編寫一個查詢以下列方式hierarchially刪除這些表。
首先刪除ChildTable3
>然後刪除ChildTable2
>然後刪除ChildTable1
並最後刪除ParentTable
。
我有這樣類似
delete from ChildTable3
where fkch2Id = ch2.ch2Id
and fkch1Id = ch1.ch1Id
and fkparentId = p.parentId
則前進到ChildTable2刪除等的一個模糊的概念。希望我明確表示。
要從ChildTable3中刪除哪些行?所有的行?滿足ChildTable3中某些條件的行或滿足其他表中某些條件的行? – 2012-02-21 07:18:46
我想從ChildTable3和其他提到的表中刪除所有行。 – 2012-02-21 07:23:03
然後你只需要從ChildTable3中刪除,然後從ChildTable2中刪除等等,最後你從ParentTable中刪除。無需加入其他表格。 – 2012-02-21 07:26:44