SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


When to use cascade deletes/updates


When to use cascade deletes/updates

Author
Message
Daniel Mory
Daniel Mory
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
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?
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85989 Visits: 41094
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Loner
Loner
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3546 Visits: 3350
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.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)SSC Guru (85K reputation)

Group: General Forum Members
Points: 85989 Visits: 41094
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Loner
Loner
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3546 Visits: 3350
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.
Ian Yates
Ian Yates
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1822 Visits: 445
Essentially if you're lazy then you can use cascaded deletes Smile

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. BigGrin



Daniel Mory
Daniel Mory
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
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.
Martin Cairney
Martin Cairney
Mr or Mrs. 500
Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)

Group: General Forum Members
Points: 521 Visits: 161
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
einman33
einman33
Say Hey Kid
Say Hey Kid (711 reputation)Say Hey Kid (711 reputation)Say Hey Kid (711 reputation)Say Hey Kid (711 reputation)Say Hey Kid (711 reputation)Say Hey Kid (711 reputation)Say Hey Kid (711 reputation)Say Hey Kid (711 reputation)

Group: General Forum Members
Points: 711 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.
Matt Miller (4)
Matt Miller (4)
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12215 Visits: 18574
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......w00t

----------------------------------------------------------------------------------
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?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search