Question

I have a simple survey database where a linear set of questions are asked. The database is set up below.

Survey Table
id: PK
(other details)

Question Table
id: PK
survey_id: FK
(other details)

Response Table
id: PK
question_id: FK
(other details)

I have been asked to modify the survey to allow N repeating question groups. For example.

  1. What is your name? What is your address?
  2. Do you play a sport?
  3. What sport do you play?
  4. What position do you play?
  5. Do you play another sport? If yes go to 3 else go to 6
  6. (Additional Questions)

What would be the best way to design a survey database with repeating groups?

I have thought about using a question rule table to direct the line of questions. For example.

Question Rule Table
id: PK
question_id: FK
response_validation_rule (possibly a regex pattern)
next_question_id

Would this be the best way to implement the repeating groups question? I am also guessing it will be difficult to normalize the data as well. Any suggestions will be greatly appreciated.

Was it helpful?

Solution

A self many-to-many relation in Question Table may helps.
The many-to-many relation will relate a question to other questions.

To achieve the criteria to fetch related questions:

If yes go to 3

Could be done by adding a FK from answer table to many-to-many table.

Something like this: enter image description here
I just suggest not to use surrogate identifier column in Related-Question table.

  • The composite PK will be {main question id, related question id,answer id}
  • Check constraint will be {main question id <> related question id}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top