Should I create a table by category, or a single table?

1

I want to set up a classified site and I have a question: how can I create categories in BD? Do I create an ad table for each category? Or do I create a Categorias table with them inside? What is the best way?

    
asked by anonymous 01.08.2014 / 18:21

1 answer

4

If category is a well-closed concept, the ideal is not to create a table for each category.

A good sanity exercise: imagine that you already have a thousand news on the site, and now every news or category needs to have one more column. Do you prefer to make changes - and still risk making mistakes - in just one table or in twenty?

There are two ways to deal with categories, in your case:

  • A category table, and the news table has a foreign key for it. This has two advantages: by renaming the category you automatically update all news of it, adding a new category just add a record to a table.

  • Category as a textual column in the news table. This can be seen by many people as denormalization. This simplifies schema of the bank and, depending on how it is done, can make searches faster. But to update a category, you need to update all news logs for that category.

If the category has more information than the name, I suggest you prefer the first form.

    
01.08.2014 / 23:26