When to use cascade deletes/updates

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

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

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

  • 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. 😀

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

  • 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

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

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

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply