This question doesn't seem strictly related to SQL Server and looks more like a theoretical, general question. It would be more usefull for the community if you remove the sql-server tag.
–
clapasAug 23 '13 at 15:40

@clapas Honestly, if I were to ask it today it would be off topic. If not for the high views/votes indicating it has value to the community I'd just delete it.
–
Joel CoehoornAug 23 '13 at 16:31

13 Answers
13

Cascade Delete may make sense when the semantics of the relationship can involve an exclusive "is part of" description. For example, and OrderLine record is part of it's parent order, and OrderLines will never be shared between multiple orders. If the Order were to vanish, the OrderLine should as well, and a line without an Order would be a problem.

The canonical example for Cascade Delete is SomeObject and SomeObjectItems, where it doesn't make any sense for an items record to ever exist without a corresponding main record.

You should not use Cascade Delete if you are preserving history or using a "soft/logical delete" where you only set a deleted bit column to 1/true.
.

Cascade Update may make sense when you use a real key rather than a surrogate key (identity/autoincrement column) across tables.

The canonical example for Cascade Update is when you have a mutable foreign key, like a username that can be changed.

You should not use Cascade Update with keys that are Identity/autoincrement columns.

Cascade Update is best used in conjunction with a unique constraint.
.

You may want to get an extra strong confirmation back from the user before allowing an operation to cascade, but it depends on your application.

Cascading can get you into trouble if you set up your foreign keys wrong. But you should be okay if you do that right.

It's not wise to use cascading before you understand it thoroughly. However, it is a useful feature and therefore worth taking the time to understand.

Note that cascade updates are also often used where the "so-called" natural keys appear not to be these real effective unique keys. In fact I am convinced that cascade updates are needed only with poorly normalised database models, and they are an open gate to messy tables and messy code.
–
Philippe GrondierOct 3 '08 at 12:13

1

You are missing one important point, cascading can create huge performance issues if there are many child records.
–
HLGEMMay 14 '10 at 19:11

8

@HLGEM - I don't see the relevance. If a cascade operations causes a slow down, the equivalent manual process would either cause the same slow down or not be correctly protected in case the transaction needs to be rolled back.
–
Joel CoehoornMay 14 '10 at 19:26

2

Why would it matter whether there's a cascade update on an IDENTITY or auto-increment column? I can see why it wouldn't be necessary because you shouldn't need to change those (arbitrary) values, but if one of them did change, at least the referential integrity would be intact.
–
Kenny EvittMay 27 '10 at 20:11

Foreign keys are the best way to ensure referential integrity of a database. Avoiding cascades due to being magic is like writing everything in assembly because you don't trust the magic behind compilers.

What is bad is the wrong use of foreign keys, like creating them backwards, for example.

Juan Manuel's example is the canonical example, if you use code there are many more chances of leaving spurious DocumentItems in the database that will come and bite you.

Cascading updates are useful, for instance, when you have references to the data by something that can change, say a primary key of a users table is the name,lastname combination. Then you want changes in that combination to propagate to wherever they are referenced.

@Aidan, That clarity you refer to comes at a high cost, the chance of leaving spurious data in your database, which is not small. To me, it's usually just lack of familiarity with the DB and inability to find which FKs are in place before working with the DB that foster that fear. Either that, or constant misuse of cascade, using it where the entities were not conceptually related, or where you have to preserve history.

Using that sort of 'natural' primary key is a really poor idea in the first place.
–
Nick JohnsonOct 19 '08 at 11:39

4

RE: Comment directed to Aidan. No, leaving off CASCADE on an FK does not increase the chance of leaving spurious data. It decreases the chance that more data will be impacted by a command than was expected and increase code. Leaving out FKs entirely leave a chance of spurious data.
–
Shannon SeveranceJul 18 '09 at 17:08

4

Having at least twice in my career seen the business-threatening consequences of a misunderstood cascade delete I'm very disinclined to use them myself in all except the most clear cut cases. In both cases data had been deleted as a result of a cascade that really should have been retained but wasn't - and that it was missing was not detected until the normal backup cycle had lost the possibility of an easy restore. Vinko is correct from a purely logical point of view, however in the real world using cascades exposes one to the human fallibility and unforeseen consequences more than I'd like.
–
CruachanMay 27 '10 at 20:43

1

I've actually coded systems where a management decision has been made that the users will have to explicitly delete all children in a master-detail before they can delete the master simply to force the user to think. Not using cascades when logically one could is a similar sort of firebreak.
–
CruachanMay 27 '10 at 20:47

4

@Cruachan: The rule, in my view, is simple. If the data is not as strongly related as to be useless without the parent data, then it doesn't warrant a cascade relationship. This I what I tried to address in the last phrase on my answer.
–
Vinko VrsalovicMay 28 '10 at 0:11

If I want something removed from the database I want to explicitly tell the database what I want taking out.

Of course they are a function available in the database and there may be times when it is okay to use them, for example if you have an 'order' table and an 'orderItem' table you may want to clear the items when you delete an order.

I like the clarity that I get from doing it in code (or stored procedure) rather than 'magic' happening.

For the same reason I am not a fan of triggers either.

Something to notice is that if you do delete an 'order' you will get '1 row affected' report back even if the cascaded delete has removed 50 'orderItem's.

Why not get rid of primary keys, too? You would get the clarity of ensuring unique values in your code.
–
MusiGenesisNov 10 '08 at 16:51

2

@MusiGenesis, Aidan was not advocating removing the FK. The FK still protects the data, but without CASCADE ON .... unexpected magic does not happen.
–
Shannon SeveranceJul 18 '09 at 17:05

5

@Vinko: Delete and update have well defined default semantics. Changing the behavior via a cascade or trigger to do more work leaves a chance more was done than intended. No, I don't work without testing and yes my databases are documented. But do I remember every piece of documentation while writing code? If I want higher level semantics, like delete parent & children, than I'll write and use an SP to do that.
–
Shannon SeveranceJul 23 '09 at 23:17

2

@Vinko. the problem of magic is not with competent developers or DBA's, it's with Joe interen 5 years later who's been given a 'simple'maintenance task when the DBA is on holiday and who then screws up corporate data without anyone realising it. Cascades have their place, but it's important to consider the full circumstances, including human factors, before deploying them.
–
CruachanMay 27 '10 at 20:55

4

@Vinko: Why 'Gasp' SPs? SPs are defiantly the way to go where the database is a critical corporate asset. There's a strong argument in the sort of circumstances were talking about to restrict all data accesses to SPs, or at the least all but Select. See my answer under stackoverflow.com/questions/1171769/…
–
CruachanMay 29 '10 at 9:32

It feels good to know whoever works against the database might never leave any unwanted data. If dependencies grow I just change the constraints in the diagramm in Management Studio and I dont have to tweak sp or dataacces.

That said, I have 1 problem with cascading deletes and thats circular references. This often leads to parts of the database that have no cascading deletes.

I do a lot of database work and rarely find cascade deletes useful. The one time I have used them effectively is in a reporting database that is updated by a nightly job. I make sure that any changed data is imported correctly by deleting any top level records that have changed since the last import, then reimport the modified records and anything that relates to them. It save me from having to write a lot of complicated deletes that look from the bottom to the top of my database.

I don't consider cascade deletes to be quite as bad as triggers as they only delete data, triggers can have all kinds of nasty stuff inside.

In general I avoid real Deletes altogether and use logical deletes (ie. having a bit column called isDeleted that gets set to true) instead.

Not knowing which datbase you use, I would suggest that your manual delete performs worse than cascading delete because it is not set based. In most datbases you can delete based on a join to another table and so have a set-based, much faster delete than looping through records.
–
HLGEMNov 10 '08 at 19:28

I, like everyone else here, find that cascade deletes are really only marginally helpful (it's really not that much work to delete referenced data in other tables -- if there are lot of tables, you simply automate this with a script) but really annoying when someone accidentally cascade deletes some important data that is difficult to restore.

The only case where I'd use is if the data in the table table is highly controlled (e.g., limited permissions) and only updated or deleted from through a controlled process (like a software update) that has been verified.

ON Update Cascade:

I have had it in the past where DBAs and/or "Company Policy" have prohibited the use of "On Delete Cascade" (and others) purely because of a bad experience in the past (in once case, a guy wrote three triggers which ended up calling one another -- 3 days to recover resulted in a total ban on triggers -- because of the actions of one idjit).

Of course, in some instances, Triggers will be needed instead of "On Delete cascade", when some child data needs to be preserved, but in other cases, its perfectly valid to use the On Delete cascade method.

The Developer should be able to make the decision based upon what the development is and what the spec says. A carpet ban based on a bad experience should not be the criteria. On Delete cascade will capture ALL the children, a custom written trigger/store procedure may not if its not coded correctly.

The "Never use" thought process is draconian at best. A judgement call needs to be made each and every time, and changes made as the business model changes.

If you're working on a system with many different modules in different versions, it can be very helpful, if the cascade deleted items are part of / owned by the PK holder. Else, all modules would require immediate patches to clean up their dependent items before deleting the PK owner, or the foreign key relation would be omitted completely, possibly leaving tons of garbage in the system if cleanup is not performed correctly.

I just introduced cascade delete for a new intersection table between two already existing tables (the intersection to delete only), after cascade delete had been discouraged from for quite some time. It's also not too bad if data gets lost.

It is, however, a bad thing on enum-like list tables: somebody deletes entry 13 - yellow from table "colors", and all yellow items in the database get deleted. Also, these sometimes get updated in a delete-all-insert-all manner, leading to referential integrity totally omitted. Of course it's wrong, but how will you change a complex software which has been running for many years, with introduction of true referential integrity being at risk of unexpected side effects?

Another problem is when original foreign key values shall be kept even after the primary key has been deleted. One can create a tombstone column and an ON DELETE SET NULL option for the original FK, but this again requires triggers or specific code to maintain the redundant (except after PK deletion) key value.