Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

When to use cascade deletes/updates Expand / Collapse
Author
Message
Posted Friday, January 4, 2008 8:07 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, May 20, 2011 9:21 AM
Points: 5, Visits: 34
One thing I'm not clear on is when to use cascade deletes/updates between related tables. I understand the maintenance benefit you get from using them, but I'm rather nervous about how this feature can enable simple delete and update statements triggering a number of hidden operations. Are there any articles out there that provide additional wisdom on this?
Post #438969
Posted Friday, January 4, 2008 9:19 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 1:47 PM
Points: 35,215, Visits: 31,667
Heh... deletes make me nervous, anyway... constitutes some form of lost data. I don't use cascade deletes because I want people to have to work at doing deletes. Would rather they learn to archive and then conscienciously delete in the correct order.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #439197
Posted Saturday, January 5, 2008 7:42 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, September 11, 2014 10:39 AM
Points: 2,803, Visits: 3,068
Usually people used cascade delete when a column of one table was reference by other tables (foreign key).

For example Table A has column1 unique not null. Table B has column1 foreign key to table A column1. When someone deleted the row that had column1 in Table A, then the cascade delete would delete all rows that contained column1 in Table B to maintain the referential integrity.

Yes it is dangerous. You would end up delete a lot of rows in other tables that you did not know.
Post #439289
Posted Saturday, January 5, 2008 8:22 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 1:47 PM
Points: 35,215, Visits: 31,667
In other words, you can delete the parent records and the child records will automatically be deleted.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #439297
Posted Sunday, January 6, 2008 6:12 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, September 11, 2014 10:39 AM
Points: 2,803, Visits: 3,068
If you set up the cascade delete correctly, that is exactly what happened.
I did one cascade delete in Oracle before and I never set one up in SQL Server.
Post #439388
Posted Sunday, January 6, 2008 7:17 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 30, 2014 10:08 PM
Points: 1,038, Visits: 444
Essentially if you're lazy then you can use cascaded deletes :)

You may wish to consider adding a "deleted" flag to a record rather than actually deleting it anyway.

Cascading deletes let you forget about deleting child records (eg lines on an order) before deleting a parent record (the order itself). I personally don't use them for the reasons others have mentioned. However, I'd much rather work in a database that at least enforced referential integrity (with or without cascading deletes) than having no defined referential integrity at all! I've seen a system where they had no RI declared and just deleted the order header record leaving behind the orphan order details.... silly, lazy and I'd say incompetent. :D



Post #439396
Posted Monday, January 7, 2008 9:55 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, May 20, 2011 9:21 AM
Points: 5, Visits: 34
I should clarify: I understand how cascading deletes are used (to automatically delete child rows when a parent row is deleted), but I was less clear on when or if they SHOULD be used. Everyone seems to concur that it is an exceedingly dangerous feature, so I think I'll try to avoid using them where possible.
Post #439686
Posted Tuesday, January 8, 2008 5:09 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Tuesday, September 16, 2014 1:37 AM
Points: 509, Visits: 159
Daniel

As long as you and your developers understand what is happenning (which you seem to do) and that the child delete is ALWAYS the intended behaviour then the cascading deletes are perfectly OK. It will ensure that no RI is broken.

If, however, you want some behaviour where for example you want to check and confirm a delete before processing it and the corresponding child records then you may want to consider a trigger instead. This would be the better option if there are scenarios where you don't simply want to delete the child records.

Cheers
Post #439984
Posted Wednesday, January 30, 2008 9:47 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Wednesday, January 25, 2012 8:14 AM
Points: 567, Visits: 512
Another area where cascade can be benefical is maintaining/changing large schemas that are deep into their life cycle. I have seen to many times where a critical, worthwhile schema addition has been shot down due to the inability to forcast all the touch points. I.E., I dont know who all can delete from my orders table and how they do it. (Yikes) A cascade at least allows you, for new entities, to ensure the new table will be cleaned up.
Post #449794
Posted Wednesday, January 30, 2008 10:18 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 3:57 PM
Points: 7,064, Visits: 15,270
Keep in mind - the various methods mentioned aren't necessarily contradictory/ or exclusive. In cases where DRI is paramount for me, and I want to make SURE I don't have a bad day and leave some orphans behind, I implement the confirmation pieces in the UI, with the cleanup process AND the cascade delete.

Just be sure to envision ALL of the deletes and WHERE it will cascade to. Nothing quite like putting a cascade delete on a self-joining reference: you could wipe out the entire company table on a single person delete......


----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Post #449799
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse