I've read some of Bill Karwin's answers about single table inheritance and think this approach would be good for the setup I am considering:
Playlist
--------
id AUTO_INCREMENT
title
TeamPlaylist
------------
id REFERENCES Playlist.id
teamId REFERENCES Team.id
UserPlaylist
------------
id REFERENCES Playlist.id
userId REFERENCES User.id
PlaylistVideo
-------------
id
playlistId REFERENCES Playlist.id
videoId REFERENCES Video.id
All the CASCADE
options are set to DELETE
which will work correctly for when a Playlist
is deleted, however, what happens if a User
or Team
is deleted?
ie. If a User
is deleted, the rows in UserPlaylist
will be deleted but the referenced rows in Playlist
and PlaylistVideo
will remain. I thought about enforcing this as a TRIGGER AFTER DELETE
but there is no way of knowing if the delete request came about because the Playlist
was deleted or if the User
was deleted.
What is the best way to enforce integrity in this situation?
Edit (Provided ERD)