I have a database to store the news of my website. The news entity type is associated with two different ones; images (which from the point of view of the app users are grouped in a gallery) and categories.
To represent this situation this, I have some tables like this:
image
id | src
category
id | name
news
id | title | text | author | ...
It is important to mention that each news may have multiple images and belong to multiple categories. So, to create the relationship between images, categories and news, I'll use another table, but my question is:
Should I have (a) two tables (one for image rows and other for category rows) or (b) just one with multiple columns? For example:
(a) Two distinct tables, one called news_relationship_category
, with the column headings
id | id_news | id_category
and another one, named news_relationship_gallery
set up as follows
id | id_news | id_gallery
(b) One single table, entitled news_relationships
with the headings
id_news | id_category | id_gallery
and INSERT INTO it rows with some columns that accept NULL marks, for instance:
10 | NULL | 3
---+------+------
10 | 27 | NULL
---+------+------
These are simple examples, but imagine if I have an e-commerce table, with products involved in multiple relationships. What is the best way to approach this case?
id
. Useid_news
,id_gallery
andid_category
all the time. It's far more clear. Yournew_relation_category
does not need an id for itself: it's just(id_news, id_category)
. – joanolo Feb 2 '17 at 23:30SELECTs
you will be doing -- they may provide your answer ! – Rick James Feb 3 '17 at 1:19