2
I have a structure of companies and categories organized in 3 tables:
empresa
-> Registration of all registered companies.categoria
-> Registration of all registered categories.rel_categoria
-> Relationship betweenempresa
andcategorias
.
Where I store the data as follows in the table rel_categoria
:
id (primary_key) | id_empresa | id_categoria
And when I need to get the categories of a company, I make the related selection. So far everything ok.
But I came up with the following question: Why use an extra table to save these records and I can store them in a column in the table empresa
?
Example in the table empresa
:
id | nome_empresa | categorias | [..etc..]
1 | 'Lojinha' | 1, 7, 14, 16 | ...
The only reason that comes to mind would be in case of a count. Ex.: How many companies use the category X
?
Is there any other reason? Any structural questions? Performance? etc.. I manage the bank through the MySql
and PHP
.
I may be talking nonsense but in ORM frameworks like Hibernate for Java and Eloquent for PHP, they use this pattern. I believe it is easier to return for example the category(object) of a company.
– Rafael
@Raphael when you say 'this pattern', refers to the related table pattern?
– celsomtrindade