to be more specific, I've created a news module with a tag system. As each tag is unique (you are not allowed, as an admin, to create 2 identical tags), is the id column still useful? I guess no, but I was wondering about the performances.
id | mews_title | date ... <-------------> news_id | tag_id <----------> id | tag_name
VS
id | mews_title | date ... <-------------> news_id | tag_tag_name <----------> tag_name
Thanks a lot!
The performance difference is insignificant.
Advantages to using a numeric id for the tags in your example would be:
These may not be important considerations for your case. So no, it's not required to use a numeric id.
I also wrote about this in a chapter titled "ID Required" in my book, SQL Antipatterns Volume 1: Avoiding the Pitfalls of Database Programming.
You don't even need a table of tags. Just a table with
news_id | tag_name
This is a many-to-many mapping between news_ids and tags. Since there is no tag table, the issue of duplicates vanishes. The same tag (tag_name) can be associated with many news articles, but that is what you want.
INSERT INTO Tags (news_id, tag_name) -- change this to INSERT IGNORE INTO Tags (news_id, tag_name) so you don't have to worry about getting the same news article tagged the same way twice. And have
PRIMARY KEY(news_id, tag_name), -- uniqueness and lookup tags for one article
INDEX(tag_name, news_id) -- For finding all articles for one tag
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With