Reputation: 155
I‘m thinking about a good DB design for this use case:
topics
articles
with foreign key topic_id
Now I want to maintain the order of articles for each topic. In which Table should I store this Information: topics, articles or a third one?
Any help is greatly appreciated!
Upvotes: 0
Views: 372
Reputation: 8621
If the number of articles - topics links are variable, you need a link table.
So:
Table Articles
idArticles PK, int, auto-increment, not null
Title varchar, not null
Table Topics
idTopics PK, int, auto-increment, not null
Title varchar, not null
Table Topics-have-Articles
idTopics PK, FK to Topics:idTopics, not null
idArticles PK, FK to Articles:idArticles, not null
Order int, not null
Notes:
Potential problems with the order field:
Upvotes: 1
Reputation: 5397
So if in the future you want to have many topics for one article and, i supose that one topic may have many articles, you will need a third table. If you are going to have only one topic for an article, you should insert it in the table article, one column for topic_id.
This will be a many-to-many relation so you need a third table with the id of the topic and the and the id of the article. The primary key will be (topic_id,article_id) so you only have one combination of article-topic. In this table both, topic_id and article_id are foreign keys.
Upvotes: 1