I\'m making an online quiz with php and mysql and need a bit of help deciding how to design the database for optimal insert of questions/answers and to select questions for
Go with option 1 where you are having one row for each question/options/answer.
Option 2 does not make any sense. Every time you want to add/delete a question you'll be modifying the database schema!! And you'll have just one row always !!
It'd be better to store the possible answers in a seperate table. This allows you to have any amount of answers per question instead of just 4. It also allows questions to have a different number of answers. If you have more than one quiz, you may also want a Quizes Table.
Quizes:
id
name
Questions:
id
quiz
prompt
Answers:
id
question
prompt
QuizResult (someone taking a quiz)
id
quiz
// other information about the quiz taker, possibly including the time
Now the correct answer thing gets a lot more tricky. I prefer the higher implementations here:
Each question has a value and each answer has value
A system I recently worked with you could assign a point value for each question and each answer. Incorrect answers often got 0, correct answers got the full amount. You could also have partially-correct answers using this method. This is the method I would go with.
You could go and say every question is worth 10 points or you could assign different weights to different questions:
Questions:
id
quiz
prompt
value (you can make this question worth more or less)
Answers:
question
prompt
value (you can make this answer worth more or less)
Store the correct answer in the Answers Table
A more simple (but less robust) solution is to simply say which answer is correct in the Answers table.
Answers:
question
prompt
is_correct
Store the correct answer in the Questions Table
I wouldn't recommend it. When you create a question, it won't have a correct answer until you insert one. This means at least 3 queries to correctly make a question. If you use foreign key dependencies, this will quickly get annoying.
Go for your first option. It is the most normalised option, but that isn't necessarily a clinching argument. But the virtues of the normalised design are manifold: