sql - Table design for questionaire -


we working on application ask users various questions, these questions can have branches, don't run in straight order. example, if below structure of questions , can flow to:

enter image description here

question 2 has branch if user answers yes move 3 , end @ 7, otherwise work our way down 4,5,6,7. have come table design wanted feedback if design work or if there better way structure this.

there question table have id (key) , text columns. tree represented as:

questionid | previousid | nextid | branch condition 1                0            2      null 2                1            3      yes 2                1            4      no 3                2            7      null 4                2            5      null 5                4            6      null 7                3            0      null 7                6            0      null 

the way work, once question answered system check current question's branch condition is, if null move nextid. otherwise find answer user selected , go associated nextid. expect y/n questions branch.

the previousid i've kept now, in case users ability go , retake question, want move forward.

if it's yes/no questions may have different next question, suggest adding 2 columns questions table specify next question id when answer yes (or default, when it's not yes/no question) , next question id when answer no. keeping previous answer id seems me redundant, since can keep in memory entire questions path in application itself.

however, if sometime in future going have multiple choice questions, , next question depend on answer (i.e category , sub categories), suggest add answers table well, , keep column next question id both in questions table , in answers table.
if next question id not dependent on answer, keep in questions table. if dependent on answer, put null in next question id column of questions table, , keep next question id in answers table. provide maximum flexibility , simple enough data structure.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -