具有不同数据类型答案的调查的 SQL 设计

发布于 2024-08-12 07:50:55 字数 1403 浏览 9 评论 0原文

我正在开展一项在线调查。大多数问题的答案等级为 1-5。如果我们需要向调查添加问题,我会使用一个简单的 Web 表单,该表单会插入到相应的表中,瞧!调查提出了新问题——没有新代码或对数据库结构进行更改。

我们被要求添加可以有不同数据类型答案的调查问题。规范是让调查“可配置”,以便在将来的任何时候,当有人说“我们需要一项新的调查来询问{文本答案问题}、{1-5 问题}、{真假问题} ,{以日期作为答案的问题}”,我们可以在不更改数据库结构的情况下做到这一点。

我试图想出存储这些答案的最佳方法,但我想出的每种方法似乎都有些老套。

有些问题可能有是/否或真/假答案,有些可能有整数答案(“过去一个月您使用过技术支持多少次?”),另一个答案可能有日期、字符串、倍数具有单个值的选择、具有多个值的多项选择等。或者有时,特定的答案值可能会提示一个子问题(“什么令您失望......?”)

简单的解决方案是将每个问题作为一个调查中的一栏、其答案作为调查中的一栏,以及是否将其作为调查中的一栏提问。这对我来说感觉就像是一团乱——这是一张大桌子;不是很“相关”。

集思广益,我能想到的“最佳”方法是为每种答案类型使用不同的表,但这感觉容易受到数据完整性问题的影响。换句话说,我会

CREATE TABLE `Questions` (...);
CREATE TABLE `TrueFalseAnswers` (...);
ALTER TABLE `TrueFalseAnswers`
  ADD CONSTRAINT `TrueFalseAnswers_ibfk_1` FOREIGN KEY (`question_id`)
REFERENCES `Questions` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
CREATE TABLE `TextAnswers` (...);
ALTER TABLE `TextAnswers`
  ADD CONSTRAINT `TextAnswers_ibfk_1` FOREIGN KEY (`question_id`)
REFERENCES `Questions` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

等等。

上述的一个问题是我不能保证对于 DDL 中的任何问题至少存在一个且只有一个答案。

另一种解决方案可能是在问题表中为答案设置一个二进制或字符串列,并将所有答案编码为某种字符串或二进制格式,并将它们存储在答案列中。这为我的每一个问题提供了至少一个且仅有一个答案,但随后我就无法访问 SQL 查询中的聚合功能。我觉得这不是一个非常“相关”的解决方案。

因此,我认为上述想法存在问题。有没有“最好”的方法来解决这个问题?

现在我已经花时间来表达问题和我的想法了,看来我提出的广泛问题是“我想存储任意类型的数据而不进行任何编码......”是这样的吗?绝望?

我正在使用 MySQL,因此我无法访问其他 RDBMS 可能访问的内容。

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 into the appropriate table, and voila! surveys are asking the new question -- no new code or change to the database structure.

We are being asked to add survey questions that can have answers of different data types. The spec is to have the survey 'configurable', so that at any point in the future, when someone says, "We need a new survey that asks {text answer question}, {1-5 question}, {true false question}, {question with a date as an answer}", we can do that without changing the database structure .

I'm trying to think of the best way to store those answers, but every method I come up with seems somewhat hackish.

Some questions may have a yes/no or true/false answer, some may have an integer answer ( "How many times in the past month have you used tech support?"), another answer may have a date, a string, a multiple choice with a single value, a multiple choice with multiple values, etc. Or sometimes, a particular answer value may prompt a sub-question ( "What disappointed you about...?" )

The simple solution is to have each question as a column in the survey, its answer as a column in the survey, and whether to ask it as a column in the survey. This feels like a mess to me -- it's one big table; not very 'relational'.

Brainstorming, the 'best' method I could come up with is to have a different table for each answer type, but that feels susceptible to data integrity issues. In other words, I would have

CREATE TABLE `Questions` (...);
CREATE TABLE `TrueFalseAnswers` (...);
ALTER TABLE `TrueFalseAnswers`
  ADD CONSTRAINT `TrueFalseAnswers_ibfk_1` FOREIGN KEY (`question_id`)
REFERENCES `Questions` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
CREATE TABLE `TextAnswers` (...);
ALTER TABLE `TextAnswers`
  ADD CONSTRAINT `TextAnswers_ibfk_1` FOREIGN KEY (`question_id`)
REFERENCES `Questions` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

etc.

One problem with the above is that I can't guarantee that at least one and only one answer exists for any question in the DDL alone.

Another solution might be to have a binary or string column for the answer in the Questions table, and encode all answers into some string or binary format, and store them in the answer column. That gives me at least one and only one answer for every question, but then I lose access to aggregate features in SQL queries. This strikes me as not a very 'relational' solution.

So, I see problems with the ideas outlined above. Is there a 'best' way to solve this problem?

Now that I've taken the time to verbalize the problem and my thoughts, it seems that the broad problem that I'm posing is "I'd like to store data of an arbitrary type without doing any coding..." Is this hopeless?

I am using MySQL so I don't have access to thing that other RDBMSes might.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

使用指定答案类型的列,但将答案存储为文本。您的应用程序或前端可以使用answer_type 列来确定向最终用户显示的内容(测试框、单选按钮、日期选择器)以及如何在将其发送回数据库之前对其进行验证。

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.

潇烟暮雨 2024-08-19 07:50:55

您想要创建一个与 QuestionType 类相对应的 QuestionType 表。允许用户填写的保留答案为自由格式文本,并由 QuestionType 来确定答案的含义。

因此,如果正确/错误,答案可能是“T”或“F”。

如果是多项选择,答案可以是所选选项的索引。

如果是用户填写的文本框,请保存他们输入的文本。

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.

萌无敌 2024-08-19 07:50:55

我提出的是“我想存储任意类型的数据而不进行任何编码......”这是绝望的吗?

是的,几乎是这样。对于您提出的问题,没有“好的”解决方案。 “最好的”正如 Dave Swersky 和 ​​Larry Lustig 所描述的那样:

问题表,存储问题、可能的答案(如果是多项选择)和问题类型

答案表,存储问题的答案(FK 到问题)表),序列化为文本。 Varchar(4000) 或 TEXT 数据类型,除非绝对必要,否则最好是前者。

由您的应用程序逻辑根据为问题指定的类型来确定该值的含义。

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.

装迷糊 2024-08-19 07:50:55

您还可以使用 dataType 指定的表,例如整数、日期、字符串等的表。
从那里开始,有 1 个答案表,将问题(表)链接到正确的数据类型表 + 主键。

要概览所有问题及其答案,您可以在其之上创建一个视图,只需将所有数据类型转换为视图中的文本即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文