SQL design for survey with answers of different data types

前端 未结 4 592
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-06 06:32

I am working on an online survey. Most questions have a scale of 1-5 for an answer. If we need to add a question to the survey, I use a simple web form, which does an INSERT

相关标签:
4条回答
  • 2021-01-06 06:40

    You want to create a QuestionType table that corresponds to a QuestionType class. Allow the persisted Answer filled in by your users to be free-form text, and leave it up to the QuestionType to determine what the answer means.

    So- if it's true/false, the Answer could be 'T' or 'F'.

    If it's multiple choice, the Answer could be the index of the selected choice.

    If it's a text box the users fills in, save the text they enter.

    0 讨论(0)
  • 2021-01-06 06:50

    You could also use dataType specified tables, so a table for integers, dates, strings ect. From there, 1 table for answers, that link the question (table) to the proper dataType table + primary key.

    To have one overview of all questions with answers, you could create a view on top of that, just casting all dataType's to text within the view.

    0 讨论(0)
  • 2021-01-06 06:54

    that I'm posing is "I'd like to store data of an arbitrary type without doing any coding..." Is this hopeless?

    Yes, it pretty much is. There is no "good" solution to the problem you're posing. The "best" is as Dave Swersky and Larry Lustig described it:

    A Question table, which stores the question, possible answers (if it's multiple choice) and a question type

    An Answer table, which stores the answer to a question (FK to Question table), serialized as text. Varchar(4000) or TEXT datatype, preferably the former unless absolutely necessary.

    It's up to your application logic to determine what the value means based on the type specified for the question.

    0 讨论(0)
  • 2021-01-06 06:58

    Use a column that specifies the type of answer, but store the answer as text. Your application or front end can use the answer_type column to determine what to display to the end user (a test box, radio buttons, a date picker) and how to validate it before sending it back to the database.

    0 讨论(0)
提交回复
热议问题