How to delete related sub tables when item is deleted?

I’m trying to set up a database and the related actions but am not sure how to work something. My basic layout is

(user sub table) Collection
(collection sub table) Item

Basically, users can make different collections and each collection can hold items within it.

How can I make it so that when a user deletes a collection it will delete all the items in that collection?

Similarly, if a user deletes their account, I want to delete all of their collections and all of the items in their collections.

I’m not sure how cascade on delete works and if it would do what I need or if I have to do something like fetch the id of all the items in the collection and then use a database delete on those items when I delete the collection.

I’m assuming there is an easy solution I’m just not sure the right way to do it. Thanks for any help.

Community Page
Last updated: