Mysql 预设/自定义选择题的数据库设计
因此,我有一个HTML表单,用户可以通过选中三个预设答案中的一个或编写自己的自定义答案来回答单选问题Mysql 预设/自定义选择题的数据库设计,mysql,sql,database,forms,database-design,Mysql,Sql,Database,Forms,Database Design,因此,我有一个HTML表单,用户可以通过选中三个预设答案中的一个或编写自己的自定义答案来回答单选问题 虽然这显然是一种多对多关系,但我仍然无法找到一种适当的方法来设计数据库表,以便处理这两种类型(文本和布尔)。我目前的快速而肮脏的解决方案是在连接表中为自定义答案硬编码一个特定的选项id。我正在为此寻找更好的模式。有以下表格: 表1:问题 QuestionID (ID) QuestionText (Text) QuestionResponseId (ID) QuestionRe
虽然这显然是一种多对多关系,但我仍然无法找到一种适当的方法来设计数据库表,以便处理这两种类型(文本和布尔)。我目前的快速而肮脏的解决方案是在连接表中为自定义答案硬编码一个特定的选项id。我正在为此寻找更好的模式。有以下表格: 表1:
问题
QuestionID (ID)
QuestionText (Text)
QuestionResponseId (ID)
QuestionResponseTypeId (References Question Response Type)
QuestionResponseDetailsId (References Question Response Details) - This should be used for text only values (Custom Answers)
QuestionResponse (Boolean)
QuestionResponseTypeId (Id)
Description (Text) -- Dictates if the answer is a boolean or a text field
QuestionResponseDetailsId (Id)
Description (Text) - Holds the text answer to the questions
表2:问题回答
QuestionID (ID)
QuestionText (Text)
QuestionResponseId (ID)
QuestionResponseTypeId (References Question Response Type)
QuestionResponseDetailsId (References Question Response Details) - This should be used for text only values (Custom Answers)
QuestionResponse (Boolean)
QuestionResponseTypeId (Id)
Description (Text) -- Dictates if the answer is a boolean or a text field
QuestionResponseDetailsId (Id)
Description (Text) - Holds the text answer to the questions
表3:问题回答类型
QuestionID (ID)
QuestionText (Text)
QuestionResponseId (ID)
QuestionResponseTypeId (References Question Response Type)
QuestionResponseDetailsId (References Question Response Details) - This should be used for text only values (Custom Answers)
QuestionResponse (Boolean)
QuestionResponseTypeId (Id)
Description (Text) -- Dictates if the answer is a boolean or a text field
QuestionResponseDetailsId (Id)
Description (Text) - Holds the text answer to the questions
表4:问题回答详细信息
QuestionID (ID)
QuestionText (Text)
QuestionResponseId (ID)
QuestionResponseTypeId (References Question Response Type)
QuestionResponseDetailsId (References Question Response Details) - This should be used for text only values (Custom Answers)
QuestionResponse (Boolean)
QuestionResponseTypeId (Id)
Description (Text) -- Dictates if the answer is a boolean or a text field
QuestionResponseDetailsId (Id)
Description (Text) - Holds the text answer to the questions
填充下表后,您将拥有一个包含问题的结构,即问题的回答(文本或布尔值)
然后,您可以对此进行筛选,以仅查看基于文本的答案,例如:
SELECT * FROM QuestionResponse
INNER JOIN QuestionResponseDetails ON QuestionResponse.QuestionResponseDetailsId = QuestionResponseDetails.QuestionResponseDetailsId
WHERE QuestionResponse.QuestionResponseTypeId = 1
其中1是基于文本的答案,2是基于布尔值的答案(来自问题回答类型表)如果我是你,我会这样做:
Table Answers:
question_id INT(11)
answer_id INT(11)
preset_answer TINYINT(1) //1, 2, 3 for three answers
custom_answer VARCHAR(255)