Ryan Haywood
Ryan Haywood

Reputation: 549

Postgresql DB Design for Quiz Project

I am thinking about how to set up my Database for an upcoming project. The project is a one page internal company quiz app. Users answer the questions, enter their name and email, and if they get them all right they are entered into a "lottery" for a prize.

There are 4 sets of questions that get released every month. I want track all the answers to all the questions for all users so I can generate that data in the admin section. There are 5 questions for each month, and each question is multiple choice.

My first thought was to make a column for each question, and name them like so: M1Q1, M1Q2, M1Q2 and then for month two M2Q1 etc. That is 20 columns for each user, and seems unnecessary. Should I store an "array" of answers for each month? Or any other way?

There is a similar question here: MySql database design for a quiz But I dont need to store questions, I just need to save each users questions for all the months. I am using rails, so I have access to domain logic etc versus the above question.

Thanks.

Upvotes: 2

Views: 1257

Answers (1)

Mark Byers
Mark Byers

Reputation: 838236

You can use two tables. One has four columns:

userid month question answer
1      1     1        A
1      1     2        C
1      1     3        B
etc...

Primary key is (user, month, question).

The userid is a foreign key to a user table where information about the user is stored.

Upvotes: 1

Related Questions