Hi folks,
I'm trying to make sure some data is auto-deleted when there's no more references using cascade deletes. I'll explain with a fake database based on Stack Overflow.
I have a Post
table. Each post has zero to many Tags.
So it should look like:
Post <-> PostTags <-> Tags
eg.
Post 1 has tags 'A', 'B', 'C' Post 2 has tags 'C', 'D'.
now, what i'm doing is that when i delete all the tags for post 2 (eg. DELETE FROM PostTags WHERE PostId = 2
), I would like tag 'D' to also be deleted because no one else if referencing it. I thought cascade deletes would handle this, but of course it's only if u cascade down from Tag->PostTags
or Post->PostTags
.
I'm not sure how to handle this.
I'm afraid that people will suggest using a trigger :( (extra complexity to the system).
Thoughts?
Note: DB is MS Sql2008.