Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the Pros and Cons of Cascading delete and updates?

Maybe this is sort of a naive question...but I think that we should always have cascading deletes and updates. But I wanted to know are there problems with it and when should we should not do it? I really can't think of a case right now where you would not want to do an cascade delete but I am sure there is one...but what about updates should they be done always?

So can anyone please list out the pros and cons of cascading deletes and updates ? Thanks.

like image 509
Vishal Avatar asked Aug 26 '10 19:08

Vishal


2 Answers

Pros:

  • When you delete a row from the Parent table all the foreign key rows are deleted
  • This is usually faster than implementing this with triggers
  • Orphaned rows are unlikely

Cons

  • Orphans are possible
  • If by mistake you delete a row in the parent table all the rows in the corresponding child tables will be deleted and it will be PITA to figure out what you deleted
like image 142
SQLMenace Avatar answered Nov 12 '22 18:11

SQLMenace


This depends on the entities that are contained in the tables: if the side of the foreign key cannot exist without the side of the primary key, it makes sense to have cascaded delete.

E. g.: An invoice line item does not have any right to survive if the invoice is deleted.

But if you have a foreign key used for the relationship "works for" for the relationship between an employee and his/her boss, would you want to delete the employee if the boss leaves the company?

In addition: a technical issue is that some ORM (object relational mapping) tools are confused if dependent table entries change without them being responsible for that.

like image 21
Frank Avatar answered Nov 12 '22 18:11

Frank