sql server, cascade delete and parent/child table

百般思念 提交于 2019-12-02 00:25:36

问题


i have one simple table with following columns: id, name and parentID

i created relationship diagram between id and parentID (on a same table), like simple tree, on the same table, but when i tried to user cascade delete it was disabled for me

i know that it will be recursive delete if i will delete parent it will delete his children

have i any options for anable cascade delete without triggers?


回答1:


No, SQL Server does not allow recursive and/or multiple cascade paths.

You can use a stored procedure to delete bit by bit, or use a trigger. Sorry.




回答2:


best to use an after trigger for multiple actions.




回答3:


One option that may work for you:

  • Place ParentID and ChildID in a separate table, remove ParentID from the base table
  • Cascade delete based solely on ID-->ParentID

The problem is that when you delete a child, you won't delete the link from its parent, and when you delete a parent, you won't delete any links from its children to any grandchildren.

However, you can get around this in your app by always using an INNER JOIN against both the ParentID and ChildID, so any leftover fluff in the Parent/Child table will be ignored.

You can then run a stored procedure on whatever timed basis you want to clean out any Parent/Child relationships where either the parent or child do not exist. Rinse and repeat each time you run it, since a simple DELETE FROM parentchild WHERE NE(parent) OR NE(child) won't be recursive.



来源:https://stackoverflow.com/questions/1433808/sql-server-cascade-delete-and-parent-child-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!