Reputation: 207
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!
Upvotes: 3
Views: 1746
Reputation: 562891
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.
Upvotes: 6
Reputation: 2049
Numeric IDs are not required (You could have a table without a primary key), but they are useful:
You can specify the tag_name as unique, so two tag_name equals couldn't be inserted.
Upvotes: 2
Reputation: 142528
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
Upvotes: 2