SQL存储不同类型的问题结构建议?

时间:2018-02-01 08:50:21

标签: database-design

我正在尝试找到一种合适的方式来存储不同类型的问题以及每种问题的答案。更具体地说,我想要存储的问题类型是:

1)多选类型

2)是/否类型

3)填补空白类型

4)匹配元素类型(匹配从A列到B列元素的元素)

到目前为止,我的想法是做一些事情:

CREATE TABLE Question (
    QuestionID int NOT NULL,
    QuestionText varchar(255) NOT NULL,
    QuestionType varchar(255),
    CorrectAnswer varchar(255),
    PRIMARY KEY (QuestionID)
);

CREATE TABLE Answer (
    AnswerID int NOT NULL,
    AnswerText varchar(255) NOT NULL,
    PRIMARY KEY (AnswerID)
);

CREATE TABLE QuestionToAnswer (
    ID int NOT NULL,
    QuestionID int FOREIGN KEY REFERENCES Question(QuestionID),
    AnswerID int FOREIGN KEY REFERENCES Answer(AnswerID),
    PRIMARY KEY (ID)
);

虽然,上面的实现不能涵盖第四个问题类型。

欢迎任何建议/帮助!

2 个答案:

答案 0 :(得分:0)

这是一种方法,允许每个问题的多个源元素的模板和每个源元素的多个目标元素

1引入一个QuestionType表并将问题指向此处。这将确保您只使用有效值,删除冗余,并使您能够使用QuestionType表进行逻辑控制。

2为User添加一个表,并在Answer表中提供指向User的外键。这将使您能够识别用户。

3为了满足您的第四个要求,您将需要以下内容:

-- provides a list of possible alternatives from which to match
CREATE TABLE AnswerElement(
    AnswerElementID int NOT NULL,
    ElementName varchar(255) NOT NULL,
    PRIMARY KEY (AnswerElementID)
);

--provides the question elements that need to be matched
CREATE TABLE QuestionElement(
    QuestionElementID int NOT NULL,
    QuestionID int NOT NULL FOREIGN KEY REFERENCES Question(QuestionID),
    ElementToMatchID int NOT NULL FOREIGN KEY REFERENCES AnswerElement(AnswerElementID),
    PRIMARY KEY (QuestionElementID)


--provides the candidate answer matches. 
CREATE TABLE ElementMatchAnswer(
    ElementMatchAnswerID int NOT NULL,
    QuestionID int NOT NULL FOREIGN KEY REFERENCES Question(QuestionID),
    UserID int NOT NULL FOREIGN KEY REFERENCES User(UserID), 
    ElementtoMatchID int NOT NULL FOREIGN KEY REFERENCES AnswerElement(AnswerElementID),
    MatchingElementID int NOT NULL FOREIGN KEY REFERENCES AnswerElement(AnswerElementID),
    PRIMARY KEY ElementMatchAnswerID

--provides the correct answers
CREATE TABLE QuestionElementCorrectAnswer(
    QuestionElementCorrectAnswerID int NOT NULL,
    QuestionID int NOT NULL FOREIGN KEY REFERENCES Question(QuestionID),
    ElementToMatchID int NOT NULL FOREIGN KEY REFERENCES AnswerElement(AnswerElementID),
    MatchingElementID int NOT NULL FOREIGN KEY REFERENCES     AnswerElement(AnswerElementID),
    PRIMARY KEY (QuestionElementCorrectAnswerID)

答案 1 :(得分:0)

这就是你可以开始的方式......你不需要AnswerQuestionToAnswer的表格,因为你创建的每个问题都在SubQuestion中插入记录或者称之为PossibleAnswers或者其他什么,你怎么能更容易理解它。

CREATE TABLE Question (
    QuestionID int NOT NULL,
    QuestionText varchar(255) NOT NULL, -- here is question like 
                                        -- 'Do you drink coffee in evenings?', then 
                                        -- in SubQuestion table youll have rows 
                                        -- for 'Yes' / 'No'
    QuestionTypeID  INT NOT NULL FOREIGN KEY REFERENCES QuestionTypes(QuestionTypeID),
    PRIMARY KEY (QuestionID)
);

CREATE TABLE QuestionTypes (
    QuestionTypeID INT NOT NULL,        -- I would take QuestionType table separated
    QuestionDescription varchar(255)
);

CREATE TABLE SubQuestion (
    SubQuestionID int NOT NULL,         -- This table actually keeps possible answers 
                                        --  on your questions
    QuestionID int NOT NULL,
    QuestionText varchar(255) NOT NULL, -- Here you keep possible answers Yes / No, Maybe       
    CorrectAnswer varchar(255),         -- This should be bit 1 or 0 ... easier to 
                                        -- figure out which is correct answer
    PRIMARY KEY (SubQuestionID, QuestionID)
);

由于有人需要回答这些问题,因此您可以更轻松地使用列创建表UserAnswers

`UserId, QuestionId, AnsweredId -- which represent SubQuestionId`

希望我明白了......