Solved Cascade Update and Cascade Delete

Teri Bridges

Member
Local time
Yesterday, 18:19
Joined
Feb 21, 2022
Messages
187
I think I get the basic idea, but I could use some guidance on these two features.
I am not sure when it is a good idea or a bad idea.

I need the user to be able to delete a record in the form view so I set my relationship to cascade delete. I hope this means all follow-on data is deleted as well.

For example Lesson>Topic>Event
If I delete the topic the event associated with that topic is deleted, but the lesson would not.

Any guidance or good foundational rules would be greatly appreciated.
 
Your interpretation is correct. However, I personally don't enable cascade delete. I would rather control how records are deleted, that is if I even allow deletion. Once a record is deleted, you won't be able to recover it, unless you have a backup. So, it's always advised to archive the data instead. On the other hand, I do enable cascade updates.
 
Your interpretation is correct. However, I personally don't enable cascade delete. I would rather control how records are deleted, that is if I even allow deletion. Once a record is deleted, you won't be able to recover it, unless you have a backup. So, it's always advised to archive the data instead. On the other hand, I do enable cascade updates.
I am not sure I fully understand the cascade updates. If I make a change to a record from a form it updates the records. Do you have an example of the cascade update you can share?
 
I am not sure I fully understand the cascade updates. If I make a change to a record from a form it updates the records. Do you have an example of the cascade update you can share?
Well, I should have really said I would be more willing or support enabling cascade updates than cascade deletes. If you use surrogate primary keys, like Autonumbers, cascade updates won't affect you much. Here's one potential example. Let's say you have a parent table with a natural primary key, as in.

ProjectsTable
ProjectCode (PK)
ProjectName
etc.

That table might contain something like:

ABCDE; Project1
HIJKL; Project2
QRSTU; Project3

Now, your database might also have a child table for the tasks associated with each project.

ProjectTasksTable
TaskID (PK)
ProjectCode (FK)
TaskDescription

It might contain something like:

1; ABCDE; Task1
2; ABCDE; Task2
3; HIJKL; Task1
4; HIJKL; Task2
5; HIJKL; Task3
6; QRSTU; Task1

The above is just a quick and simple example to, hopefully, explain how cascade updates work.

So, now, you know cascade delete works by removing all the tasks associated with the project code ABCDE, if you delete that record from the projects table. What cascade update will do is if instead you changed the project code from ABCDE to WXYZO, then all the tasks with a foreign key of ABCDE will also change/update to have an FK of WXYZO.

Does that help?
 
Well, I should have really said I would be more willing or support enabling cascade updates than cascade deletes. If you use surrogate primary keys, like Autonumbers, cascade updates won't affect you much. Here's one potential example. Let's say you have a parent table with a natural primary key, as in.

ProjectsTable
ProjectCode (PK)
ProjectName
etc.

That table might contain something like:

ABCDE; Project1
HIJKL; Project2
QRSTU; Project3

Now, your database might also have a child table for the tasks associated with each project.

ProjectTasksTable
TaskID (PK)
ProjectCode (FK)
TaskDescription

It might contain something like:

1; ABCDE; Task1
2; ABCDE; Task2
3; HIJKL; Task1
4; HIJKL; Task2
5; HIJKL; Task3
6; QRSTU; Task1

The above is just a quick and simple example to, hopefully, explain how cascade updates work.

So, now, you know cascade delete works by removing all the tasks associated with the project code ABCDE, if you delete that record from the projects table. What cascade update will do is if instead you changed the project code from ABCDE to WXYZO, then all the tasks with a foreign key of ABCDE will also change/update to have an FK of WXYZO.

Does that help?
Thank you, I get it now. Happy Dance!
 
I'm a bit late but in case it helps, have a look at my article
 
Cascade update ... I've never used it. I think anyone who needs something like that has a lot of clutter in their database beforehand.

Deleting is not the typical case for a database with important data anyway. That's where you're more likely to collect.
 
Cascade update ... I've never used it. I think anyone who needs something like that has a lot of clutter in their database beforehand.

To answer in your typical style, anyone who makes comments like that doesn't understand the purpose of cascade updates
 
doesn't understand the purpose of cascade updates
That's right, in my case.
I find changing keys very problematic. Why not create these correctly in the first place?
 
Please read my 3-part article linked in post #8. The relevant section is towards the end of part 1
 
I can't find any interesting information.
In my opinion, with a surrogate key there is no reason to have to change it later.
 
On the other hand, I ALWAYS rely on the database engine for Cascade Delete when it is appropriate. I would never presume to be better than the db engine at deleting related records. The key is to understand which relationships qualify for cascade. For example, you would NEVER specify cascade delete on the relationship between Customers and Orders because you would #1 probably never want to delete a customer and #2 you certainly wouldn't want to delete actual order history. Therefore, if you allow deletes on Customers, without cascade delete, you can only delete Customers who do not have any Orders. However, you would ALWAYS use Cascade Delete on the relationship between Orders and OrderDetails because OrderDetails have no meaning except within the context of an Order. That is what RI does for you;)

As the others have already mentioned, there should never be a need for cascade update. If your PK might change, it is far better to use a surrogate key with a unique index on the candidate key. That way no cascade is necessary. You are changing "data" rather than a PK
 
Last edited:
I am pretty sure I have never used a cascade operation. I just do not like the idea.

Let's say you have a customer account, and use an Alpha index.

Personally, I would use an autonumber PK for the customer table, and add an index on another field called customer_sort_key. So the users can have an autonumber PK field, and also have a customer_sort_key of ACME for the ACME Corporation. Now ACME Corporation gets taken over by Widget Company.. You can change the customer_sort_key to WIDGET, so users can obtain reports sorted differently. But the system uses the autonumber PK to relate orders or jobs, for instance, to the customer and never uses the real world sort key. So there never needs to be a cascade update operation.

I wouldn't cascade delete records either. I would rather tell users that they just cannot delete the ACME account because there are historical records using the account.

Now if they really want a process that deletes sales history, price history, and so on for ACME, I can develop one, but I would rather the client thinks very carefully about it first, and then I will add a process to manage the deletes.
 
By chance Mike Wolfe wrote an article about this topic today:
 
I wouldn't cascade delete records either. I would rather tell users that they just cannot delete the ACME account because there are historical records using the account.
Did you read my explanation of how Cascade Delete works? When RI is enabled, it will PREVENT the deletion of parent records when there are child records in ANY related table unless Cascade Delete is checked for that relationship. It is up to YOU to understand the relationships and to apply Cascade Delete ONLY when appropriate. You might have 6 tables related to a parent. You are willing to allow the parent to be deleted even when there is data in 2 of the tables but if there is data in any of the other 4, then delete should not be allowed. Use cascade delete on 2 relationships but not on the other 4. It is not all or nothing.

Just saying you won't use Cascade Delete means that YOU have to write code in your forms to handle that situation. So, if accounts are related to 6 other tables, you need to run 6 dcount()s to determine that no related data exists. Then if you add a seventh table, you have to remember to modify the code.
Now if they really want a process that deletes sales history, price history, and so on for ACME, I can develop one, but I would rather the client thinks very carefully about it first, and then I will add a process to manage the deletes.
Cascade delete logically has nothing to do with this. You can make it as hard as you want to delete accounts. Only accounts that you allow to be deleted will be deleted, it is the Cascade that takes place as a result of that decision that you also need to now code. You MUST create a transaction and then inside the transaction, you would delete the related records from the 6 tables, then you would delete the account record. The transaction is necessary because you don't want some of the deletes to work if all of them don't work) But if you allow Cascade delete on any relationship where you would ever allow deletes of the parent, you let the database engine control the related process and that makes sure that your code doesn't miss anything.

Refusing to use well tested Jet/ACE code because you don't understand it is like taping up your left little finger to prevent its use because sometimes you hit the wrong key with it. Use the correct tool for the job. Cascade Delete is a very delicate tool. Very few relationships should use it but when they should, why make extra work for yourself?

Rather than making an arbitrary "rule" to never use a tool from your tool box, I tried to explain how you can logically determine whether the use of the tool is appropriate. Would you refuse to use your chainsaw because most of the time you don't want to cut down your trees?

Whether you use Cascade Update or not depends on what type of PK the table has. If the PK is an autonumber, you can't change it so no Cascade would ever happen so the point is moot. If you do use a changeable key, you then have a reason to check the Cascade update box but you are making a poor design choice when you use a changeable value as your PK. If you have a potentially changeable value you want to use as a PK, then it is best to use a unique index on the candidate key and substitute an autonumber that is used for relationships. I always do this when a "key" is coming to me from a different application. For example, I might interface with a human resources application and so their PK is EmployeeID. For now it is numeric but I don't have any control over it or whether it can change or not so I would make it a unique index and use a surrogate key in my application.
 
Last edited:
@Pat Hartman

Pat, I get what you're saying, and maybe I'm wrong, but I would rather manually control record deletion.

I don't make a changeable value as the PK. I said I would add another (indexed) field for values that have meaning but may need to change.
 
FWIW I never use cascade update either as I use auto number PK fields. However, I do use cascade delete in cases where it does what I need with no additional code required
 
Pat, I get what you're saying, and maybe I'm wrong, but I would rather manually control record deletion.
I presume that you make Transactions to encapsulate the updates so that they all happen or that none of them happen. If so, you just have more code to write and test but end up with the same solution. Every batch operation that involves multiple updates MUST be enclosed in a Transaction, not just this particular type.

I learned a long time ago to use the "Access way" rather than to roll my own unless I had a specific reason for deviating. Also as I've said many times, I'm lazy. If the tool will do it for me, why would I code around it?

We'll have to agree to disagree on this one.
 

Users who are viewing this thread

Back
Top Bottom