Reputation:
I want to implement a tagging system on a website I'm working on. I have a theoretical question about this. How would I store those tags in my database?
If for example I use a table with two columns, tags and postId I could have a certain tag an incredible amount of times if it is popular! So I'm not sure if this is the best way.
If I want to add tags to posts, and I have 3 posts with the tag 'cool' I'd have something like this:
tag-----id
cool----23
cool----12
cool----39
Are there better ways to achieve this?
Upvotes: 2
Views: 216
Reputation: 12599
You want a post table, a tags table and a join table:
CREATE TABLE posts (id, title, body)
CREATE TABLE tags (id, tag)
CREATE TABLE posts_tags (tag_id, post_id)
This is fine. Make sure you add indexes on the tag, tag_id and post_id columns.
Upvotes: 6