Reputation: 2220
I've searched the forum, and there are several posts about a questionnaire design, but I want to discuss a specific topic. Moreover, this is a quite general design question.
I want to store the questionnaire responses into a database. Some responses are free text (e.g. varchar) and some others are multiple choice (e.g. FK). How do you model that difference?
I've seen 2 approaches - (forget details).
Approach A: use 2 tables. One for text-responses and other for multiple choice responses.
Table: text_response
some_stuff ----[*]
text ----------[varchar]
Table: mc_response
some_stuff ----[*]
optionID -----[FK to selected option]
*-not relevant to the disccussion... ResponseID, QuestionID, SurveyID, whatever
Approach B: "merge" both tables and use only one of the columns depending on the response type.
Table response
some_stuff ----[*]
optionID -----[FK to selected option]
text ----------[varchar]
Which one should I choose (if any)? To keep the discussion general and useful for anybody I would like pros and cons for the general purpose, but if it helps some context I'll use it for a highly dynamic questionnaire system (users can create their own polls and queries). I don't care much about performance, and I do care about code simplicity and maintenance.
Upvotes: 2
Views: 183
Reputation: 55886
Go for approach B. Approach A does not make sense. Here is why:
Two table representing one logical object Response
is a bad idea. Not intuitive.
Your application logic will be complicated. It's complicated to do this -- "get all responses of a questionnaire id = X", you will need to query two tables.
You will have to get questionType
to decide what table to query to for the answer.
Suppose you wanted a mixed bag response
like first three choices in response is multiple choice, and people may fill in their own opinion if the three choices does not match to what they have to answer. Are you going to split Response
of the same question in two tables?
Upvotes: 2
Reputation: 1380
I would chose approach B
Upvotes: 1