The other day I created a database table in MySQL, and first I made it so that there are two columns, the first column holding a unique id, and the second holding a category name.
First I let the id column be the primary key, and associated entries in another database table with this value. But then I thought, maybe it's unnecessary to have this id column, when I can just associate the category name with entries in another table.
Let me make it a little more clear. Background setting: We have a database with 3 tables.
Table1 stores data about news articles.
Table2 stores all different categories an article can belong to (and an article can belong to more than one category)
Table3 is a lookup table which says which articles are associated with which category.
In Table1 I think it's natural to keep one column for the unique id of every article. But what about Table2? In Table3 I want to store the article id and a category id or name in every row, like this:
option 1:
article_id --------------- category_id
1 --------------- 2
1 --------------- 3
2 --------------- 2
or if I choose to only store the category names in Table2 and use them as the primary key:
article_id --------------- category_name
1 --------------- world news
2 --------------- world news
2 --------------- politics
So the real question here becomes, which way would be the more efficient? Does it take longer for the database to find an entry in a primary key column consisting of a unique string of characters than a unique number id?
Apologies in advance if the dilemma is not clear enough. Hope it gets through though. And by the way, the tables are just examples, nothing I'll actually use.
Bookmarks