sql - 带有单选或多选且没有正确/错误答案的问卷的模式设计
问题描述
首先,我在这里经历了数十个与我相关的问题,但找不到我要找的东西,因此再次问了一个看似重复的问题。
我需要一份针对所有用户的大约 30 个固定问题的问卷调查,
- 单选选项(文本或带图像的文本)
- 多选选项(文本或带图像的文本)
- 自由文本
- 日期字段
由于问题迎合用户的喜好,因此没有正确或错误的答案。我一直在研究 postgres 模式设计,看起来像这样。
--问题表--
id int
question varchar
description varchar
type varchar -> single select/multi select or free text with no options
createdAt timestamp
updatedAt timestamp
- 选项 -
questionId int -> foreign key to questions.id
type varchar -> text or text with image
text varchar
image varchar
createdAt timestamp
updatedAt timestamp
---答案---
questionId int -> foreign ref to questions.id
option int -> foreign ref to options.id
value varchar -> free text in case a question didn't have options
options ? -> array of option ids?
userId int -> foreign ref to users.id
createdAt timestamp
updatedAt timestamp
现在,我不确定在多选的情况下如何最好地将选项存储在答案表中。我是否保存一组选项 ID?但这会使我什至无法拥有选项表的外键引用,并且可能会使查询变得困难,因为我需要能够查询单个用户的所有问题的问答对,这个数组对我没有帮助填充他选择的选项。请帮我找出存储它的最佳方法。
解决方案
我的模型可能看起来像这样......丰富
--user table
user_id (primary key)
first_name
last_name
email
create_date_time
update_date_time
--survey master table, so you can reuse the model for additional surveys
survey_id (primary key)
survey_name
survey_description
create_date_time
update_date_time
--question table
question_id (primary key)
survey_id (foreign key)
question_text
description
question_type
create_date_time
update_date_time
--question answers table
answer_id (primary key)
question_id (foreign key)
answer_type
answer_text
answer_image
create_date_time
update_date_time
--user answers table
user_id (primary key and foreign key)
question_id (primary key and foreign key)
answer_id (primary key if >1 answer allowed, and foreign key)
user_answer_text
create_date_time
update_date_time
推荐阅读
- php - 小型模板系统,/if 结尾未检测到正确
- javascript - API 数组问题
- python - 在python中的二维列表中的位置查找重复项
- javascript - WordPress中类似的HTML元素冲突
- python - 散景悬停在具有不同数据源的两个表上
- java - 如何正确关闭嵌套的 ZipInputStreams?
- android - Firebase crashlytics - 混淆/反混淆
- javascript - Material UI 自动完成功能未按预期工作,不接受逗号
- android - FCM 通知不显示在后台弹出通知
- python - 如何根据熊猫数据框每两个样本的另一列分别为每一列绘制线图?