views:

168

answers:

6

hello what is the best way to create a database for Questions and Answers Script

the user can submit a question with a set of choices among which the right answer exists

the database must save the question, all the choices and the right answer.

the best way that comes to my mind is to make 2 tables:

Questions Table:

  • ID
  • Question
  • Right_Answer_ID

Choices Table:

  • ID
  • Choice
  • Question_ID

I'm using PHP and MYSQL for that so if i use this way it will be a little hard to insert information into my database, because i have to insert the Question and insert all choices and take the right choice ID modify the Right_Answer_ID field to that ID, it's a long process and i'm sure that there is a better way to achieve that, please HELP.

Thanks

+4  A: 

What about this?

Question

  • ID
  • Question

Choice

  • ID
  • Question_ID
  • Choice
  • Is_Right_Answer
Jeff
Nice suggestion! In addition to removing the circular INSERT conundrum this paves the way towards a more generic modeling of the domain, one where there may be more than one correct answer, (or, if you turn "Is_Right_Answer" to a numeric value, one where answers may be deemed "correct" within a sliding scale.)
mjv
Good point about the numeric sliding scale! With this model, I was thinking that it'd be easier to develop the logic, in addition to being able to have more than one correct answer. But the "correctness" scale didn't occur to me.
Jeff
+1  A: 

Reminds me of Catch-22 by Joseph Heller...

First edition book cover

Unlike with the book, there is a true loophole:
The easy way out is to produce your own IDs, rather than relying on auto-incremented and other SQL-supplied IDs.

This said, and aside from being a reminder that application-generated keys and identifiers are sometimes preferable to their system-supplied counterparts, it is also a good opportunity to reflect on the database design.
For example Jeff's answer, which suggests moving the "correct response" info from the "Right_Answer_ID" column in Questions table to an "IsCorrect" column in the Responses table, not only addresses the INSERT circular reference problem, but also introduces a more versatile data model: one where we may have multiple correct responses for a given question (or possibly, by changing "IsCorrect" by a numeric value of sorts, one where Responses may be "correct" on a sliding scale)

mjv
A: 

This looks like the best approach if the number of choices is variable - it's properly normalized and easy to query. An extra for loop to insert the choices won't break your back. You could even construct a single query for all the choices and execute it once.

Max Shawabkeh
A: 

Your design is a good starter, but what about questions with more than one correct answer, even if that will not be the case right now, but applications tend to evolve. That will lead to Jeff's answer, as well as the problem posed by mjv for saving the values.

So when only saving "ID" and "question" in the questions table, then you can use that question_id to save the choices, and you do not need to get the id of the correct answer back when saving choices and update the questions table.

Residuum
A: 

if the number of choices is either variable or can change in future, then the design you have proposed is the right way to go. if the number of choices is not going to change, then I would say it would suffice to have a single table with id, question, choice1, choice2, ...., choicen, correct_choice as the fields in it.

Aadith
Hum... The assumption that the number of replies will be [forever] fixed often fails. Regardless, this type of approach then requires a mapping of sorts between the numbered column names with the corresponding entities in the User Interface; while such mapping can be put in a loop (by generating the names in `"choice" + i` fashion), such is so much more rigid than the list-oriented approach. For example how would one go about showing the possible responses in a random order, or by systematically excluding two of the incorrect answers ?
mjv
A: 

If you have two tables you will have to have two insert queries. You can avoid the last update query by following the design below:

Questions Table:

* ID
* Question
* Right_Answer_Index

Choices Table:

* Index
* Question_ID
* Choice

If you need to have a primary key on the choices table you can combine Index with Question_ID. This way, in the html form you use for authoring new question and answers, you can have an extra input to indicate the index number of the correct choice and thus when you are inserting your question you have all the information you need (before the correct choice record has been added).

Majid
You will have to make sure that the choices_index is unique, before saving the question. How would you do that in your scenario?
Residuum
By index I mean an enumeration, so if you have four choices the choice_indexes will be 1, 2, 3, and 4. This way we will have many records with the choice_index set to 1 for instance and that is obviously not unique. But each of them will have a different Question_ID, and we can use the combination of 'Question_ID,Index' as the primary key to uniquely identify a record in Choices table
Majid