问题
What is the best database schema for polls? Is one-to-many relationship good for this? I'm thinking about having two tables:
poll_questions
int id
varchar body
datetime created_at
datetime updated_at
poll_answers
int id
varchar body
int votes default 0
int question_id (foreign key to poll_questions.id)
datetime created_at
datetime updated_at
Then there would also be third table for tracking who voted for an answer so users are able to vote only once:
poll_voting_history
int id
int question_id (foreign key to poll_questions.id)
int answer_id (foreign key to poll_answers.id)
int user_id (foreign key to the id in the users table)
datetime created_at
datetime updated_at
What are your thoughts? Am I thinking about it right?
回答1:
The schema looks good, and yes, you'd need to track the user votes as well.
回答2:
Note: the "votes" column of the poll_answers table isn't necessary. Votes can be determined by querying the Poll_voting_history table.
回答3:
I think that question_id in poll_voting_history is not necessary as well, since each answer points to a question, so based on a answer_id you can get the question_id that it belongs to.
回答4:
I would like to add the status of poll_questions and poll_answers as well, just in case if we need to activate or disable the current poll question during given date and time.
来源:https://stackoverflow.com/questions/2264750/poll-database-schema