Mysql – Db design for a quiz with variable number of answer choices, and each worth different score in a different category

database-designMySQLschema

I'm working on a question/answer project and having a hard time coming up with a suitable database structure.

Each question can have from 1 – "n" answers, and each answer is associated with one of 6 predefined categories.

For example:

Q1) Which magazine would you prefer to read?

  • Science (+1 for Science category)
  • Wired (+1 for Science, +1 for Tech)
  • Vogue (+1 for Fashion)
  • Time (+1 for Information, +1 for Social)
  • Fortune (+1 for Business)
  • People (+1 for Social)

Q2) Have you taken an art class?

  • Yes (+1 for Artistic)
  • No (+0 for Artistic)

Q3) What's the best part of a movie?

  • Action (+1 for Science)
  • Story (+1 for Social)
  • Music (+1 for Artistic)
  • Acting (+1 for Artistic)

I know I should have one table for Questions, and a separate table for Answers linked by question_ID, but I'm not sure how to deal with a variable number of answer choices that are different for each question, and their corresponding categories.

Best Answer

Consider a data model like this:

ERD

You have a list of quesitons in the QUESTION table. Each question can have two or more possible options for answers in the OPTION table. For each option there are one or more category score changes which are noted in OPTION_CATEGORY. When a USER takes the quiz their answers (i.e. choices of options) are recorded in ANSWER.

Note that with this design you can change someone's score in multiple categories with a single answer (such as in your example of reading WIRED magazine) and you also have the option of changing someone's score in a category by an increment other than 1 (as in your example of not having taken an art class). You could even change someones score by more than 1, for example in your question 1 you could score +10 in the science category for prefering to read Philosophical Transactions of the Royal Society!