I have a survey web application. The survey can have a multiple choice question. An answer to a multiple choice question can be dependent on other questions’ answer.
Example:
Question 1 has choices: HP, Acer, Samsung, Lenovo
Question 2 has choices: Android, Ubuntu, iOS, Windows
Question 3 has choices: Ubuntu, OS X, Windows
Question 4 has choices: Adidas, Nike, Puma
Say Question 4 depends on the combination of answers from Questions 1, 2 and 3.
Example 1:
If a person answers: Question 1 = “HP”, Question 2 = “Ubuntu”,
Question 3 = “OS X”; Question 4 is automatically set to “Puma”
Example 2:
If a person answers: Question 1 = “Acer”, Question 2 = “Ubuntu”,
Question 3 = “Ubuntu”; Question 4 is automatically set to “Adidas”
*Both examples have the same logic.
Generally, some survey questions’ answer can be dependent on some other survey questions’ answer.
How do you design/model a database for that purpose?
This is the initial table relationship I’ve created (feel free to modify it):
Users: user_id
Questions: question_id
Choices: choice_id, question_id
Answers: answer_id, user_id, question_id
Additional information:
The admin user interface process I’m thinking of doing is:
1. The admin creates several independent questions (questions which have answers independent of other questions' answer)
2. The admin creates a dependent question, selects one or many questions which he created earlier, selects a combination of answers from those question (just like in examples 1 and 2 above) and then sets an answer for the dependent question based on those combination of answers.
... The admin proceeds creating several more questions.
Edit:
Thanks for your idea @MahmoudGamal. I created something that is based on your design:
Combinations table
ID
question_id # the dependent question's id
choice_id # the automatic answer based on the combination of other answers
Answer Combinations table
ID
combination_id
question_id # the question that is depended upon by the dependent question
choice_id # the choice that will be used for the combination
So I can have several combinations for one question. Example:
If I want Question 4 to accept several combinations. One combination has different answer.
Answer Combinations table
ID combination_id question_id choice_id
1 1 1 1
2 1 2 2
3 1 3 3
4 2 1 2
5 2 2 2
6 2 3 1
And the Combinations table would have
Combinations table
ID question_id choice_id
1 4 4
2 4 3
Looks pretty neat to me. What do you think?
PS: Forgive me but I’m new to Stack Overflow and I’m still finding my way around.
So here is the scenario that I could understood from your question:
The admin specify a relation between the previous questions’ answers and another question, based on this conditions this question will have an automatically answer based on this relation, and this logic relation is specified manually. So for this, you will need two more tables:
PreviousQuestionsAnswers:QuestionId,PreviousQuestionId,PreviousAnswerIDQuestionPreAnswer:QuestionId,AnswerIdFor instance:
Then these two tables will have something like:
PreviousQuestionsAnswers:QuestionPreAnswer:Note that: These data are preentered by the admin, so that, from the front end application after the OP that supposed to take that survey, you will match the answeres he entered in the previous questions that supposed to be entered in the
Answersthat you already have, with the pre defined conditions in thePreviousQuestionsAnswerstable, if so then set the question with the choiceQuestionPreAnswer.