SQL - 标准化包含多项选择的 table
SQL - Normalizing a table containing multiple choices
我正在尝试创建一个包含 table 的数据库,其中包含多项选择题的答案。
我的问题是,为每个问题创建一个专栏是否正常?
我的意思是,如果我有 100 个问题,我是否为每个问题创建一个专栏?
最好的方法是什么?
谢谢
你可以参考这个link
Table: 用户
user_id 自动整数
regtime 日期时间
用户名 varchar
useremail varchar
userpass varchar
Table:问题
question_id 自动整数
问题varchar
is_active 枚举(0,1)
Table: Question_choices
choice_id 自动整数
question_id整数
is_correct_choice 枚举(0,1)
选择varchar
Table: User_question_answer
user_id整数
question_id整数
choice_id整数
is_correct 枚举(0,1)
answer_time 日期时间
为每个问题创建一个行是正常(在普通意义上和数据库规范化意义上)。在另一个 table 中,为每个答案创建一个 行 。
阅读有关数据库规范化的内容。
您的 question
table 的列可能包含这样的值
question_id question_text
1 What color is the sky?
2 What number comes after 1?
和您的 answer
table 的列可能包含这样的值
answer_id question_id correct answer_text
1 1 0 Green
2 1 1 Blue
3 1 0 Orange
4 2 0 -2
5 2 1 2
6 2 0 42
7 2 0 π
使用这样的设计,您可以根据需要添加任意数量的问题和答案,而无需更改 table 的结构。
您可以检索正确答案,例如,对于像这样的每个问题
SELECT question_text, answer_text
FROM question
JOIN answer ON question.question_id = answer.question_id
WHERE answer.correct = 1
我正在尝试创建一个包含 table 的数据库,其中包含多项选择题的答案。 我的问题是,为每个问题创建一个专栏是否正常?
我的意思是,如果我有 100 个问题,我是否为每个问题创建一个专栏?
最好的方法是什么?
谢谢
你可以参考这个link Table: 用户
user_id 自动整数
regtime 日期时间
用户名 varchar
useremail varchar
userpass varchar
Table:问题
question_id 自动整数
问题varchar
is_active 枚举(0,1)
Table: Question_choices
choice_id 自动整数
question_id整数
is_correct_choice 枚举(0,1)
选择varchar
Table: User_question_answer
user_id整数
question_id整数
choice_id整数
is_correct 枚举(0,1)
answer_time 日期时间
为每个问题创建一个行是正常(在普通意义上和数据库规范化意义上)。在另一个 table 中,为每个答案创建一个 行 。
阅读有关数据库规范化的内容。
您的 question
table 的列可能包含这样的值
question_id question_text
1 What color is the sky?
2 What number comes after 1?
和您的 answer
table 的列可能包含这样的值
answer_id question_id correct answer_text
1 1 0 Green
2 1 1 Blue
3 1 0 Orange
4 2 0 -2
5 2 1 2
6 2 0 42
7 2 0 π
使用这样的设计,您可以根据需要添加任意数量的问题和答案,而无需更改 table 的结构。
您可以检索正确答案,例如,对于像这样的每个问题
SELECT question_text, answer_text
FROM question
JOIN answer ON question.question_id = answer.question_id
WHERE answer.correct = 1