I am implementing my own blogging system to better understand the object oriented side of PHP, mySQL, and jQuery AJAX. I have a many-to-many relationship between Post_T and Tag_T, thus including an associative entity PostTag_T. When creating a post, I added functions that would check if the tag already exists, and either add the new tag and mapping, or just add the mapping to the existing tag.
The mishap that I noticed is that when deleting a Post, the tag row in Tag_T still exists while the mapping in PostTag_T is removed (set to ON DELETE CASCADE). How can I go about deleting the tag, if it isn't being used by any other post? I have the post ID while deleting the Post.
Useful Logic
Using the post id from deletion, delete from tag_t where in posttag_t the pID = post id AND no other posts (pID) in posttag_t is using the same tag id.
delete from tag_t where tID = (select tID from posttag_t where pID='post id') AND..
For your sanity, here is a visual representation
Created Post row in Post_T:
Mapping between post and tag in PostTag_T:
Created Tag row in Tag_T:
When Deleting: using AJAX POST with data that calls a php file in url (guessing this is where I might need to add a query that searches tables..)