SQL design for survey with answers of different data types

前端 未结 4 615
佛祖请我去吃肉
佛祖请我去吃肉 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: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.

提交回复
热议问题