且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在数据库中存储多个选择问题

更新时间:2023-01-22 12:15:34

这可能是一种结构方式:

This could be one way to structure:


问题

Id

Text



答案

Id

文字

QuestionId



CorrectAnswers

QuestionId

AnswerId


Question
Id
Text

Answers
Id
Text
QuestionId

CorrectAnswers
QuestionId
AnswerId





有一个单独表格的正确答案的理由是一个问题可以有多个正确的答案。如果不是这种情况,您可以省略第三个表,只需将名为 CorrectAnswerId 的字段添加到问题表。



如果有多个问题可以使用相同的答案,你也可以从中删除 QuestionId 答案表并将 IsCorrect 字段添加到 CorrectAnswers 表。这将减少数据库的大小。但是,如果您对一个问题进行了更改以回答,则会针对使用该问题的所有问题对其进行更改。



Reason for having a separate table for correct answer is that one question can have more than one correct answers. If that is not the case, you can omit third table and just add a field called CorrectAnswerId to Questions table.

If there is a chance that same answer can be used for multiple questions, you might as well remove QuestionId from Answers table and add IsCorrect field to CorrectAnswers table. This will reduce the size of your database. However, if you make change to answer for one question, it will change it for all the questions that are using it.