Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choosing a primary key for many-many relation

I'm trying to model a site similar to StackOverflow. It has a set of users and questions, and user vote on questions. Each user can only have one vote on each question.

What should the structure of my "VotesOnQuestions" table look like:

  1. Should I have an auto-generated "VoteID" column?
  2. How do I disallow, at the schema level, a user from having multiple votes on a question?
  3. Should I, instead of a VoteID column, just use (UserID, QuestionID) as a primary key?
like image 348
ripper234 Avatar asked Feb 27 '23 21:02

ripper234


1 Answers

Use the Primary Key UserID, QuestionID.

That will enforce your requirement, and optimze searches.

like image 183
Adriaan Stander Avatar answered Mar 08 '23 15:03

Adriaan Stander