I'm currently designing a database for a small bookmarking application (using MySQL) and I'd like to do something clever with the tag system. Although it's not an issue initially, I want to implement something similar to Stack Overflow's tag synonyms, where each tag can have multiple sub-tags that map to it. This would allow tag searches for 'hi' to return bookmarks tagged with 'hello' for example.
I'm familiar with building a many-to-many tag system in which you have three tables: 'tags', 'posts' and 'posts_tags' and I'd like to make synonyms fit in with this.
My initial thoughts were that each tag could have a 'parent' field which contains the ID of the tag it maps to. However, this could cause a lot of orphaned tags and would be a nightmare to manage; I'm looking for speed and elegance.
If anybody has any ideas/guidance it'd be much appreciated! Thanks
You could use a child to parent table. For example,
Using childTagId as the pk of the ChildToParentTags table limits a tag to 0 or 1 parent, but allows a parent to have multiple children.
Query for post by tag: