ImZyzzBrah
ImZyzzBrah

Reputation: 142

Should i create 1 or 2 datatables for this? (related to keeping track of voting on questions and answers)

This is for my web project at university.

I am making a community Q&A site (like this for example) and these are the details related to my question:

Users can ask questions.
Users can answer questions.
User can upvote/downvote a question.
User can upvote/downvote an answer.
User can only vote ONCE on any given question/answer. (What i want to enforce)

Question:

Should I use two data tables?
- One data table for question votes
- One data table for answer votes

OR

Use ONE data table and add a column to determine whether the ID relates to a question or relates to an answer.

One of the benefits of using two data-tables from what i can think of is determining the foreign key of the ID, we can map each ID of question/answer to it's primary key in it's question/answer table.

Appreciate your help.

Upvotes: 2

Views: 44

Answers (1)

James Wiley
James Wiley

Reputation: 489

I would keep them separate. There are ways you could keep them in the same table and keep foreign keys to the question or answer, but it can get messy.

If you think about how the votes are related, the question votes and answer votes are not really related at all(distant cousins at best), and they don't need to know about each other. Question votes only need to know which question they belong to, and answer votes only need to know which answer they belong to. Answers need to know which question they belong to, but that is as deep as their connection needs to go.

I can't think of any real benefit to combining all votes into a single table.

Upvotes: 1

Related Questions