简体   繁体   English

在数据库中存储多项选择测验 - 决定模式

[英]Storing a multiple choice quiz in a database - deciding the schema

I am trying to implement a multiple choice quiz and will want to store all my questions and answers in a SQLite database.我正在尝试实施多项选择测验,并希望将我所有的问题和答案存储在 SQLite 数据库中。 I will have many questions, and for each question there will 2 or more possible answers to display.我会有很多问题,每个问题都会显示 2 个或更多可能的答案。

My question is, how should I store the questions and answers in a database?我的问题是,我应该如何将问题和答案存储在数据库中? I have two ideas for a schema (primary key in bold)我有两个关于模式的想法(粗体的主键)

  1. as (many to many)作为(多对多)

questions ( questionID :int, questionString:String, correctAnswerID:int)问题( questionID :int,questionString:String,correctAnswerID:int)

answers ( answerID :int, answerString:String)答案( answerID :int,answerString:String)

questions_and_answers ( questionID , answerID ) questions_and_answers ( questionID , answerID )

2. 2.

questions ( questionID :int, questionString:String, correctAnswerID:int)问题( questionID :int,questionString:String,correctAnswerID:int)

answers ( answerID :int, answerString:String, questionID:int foreign key )答案( answerID :int,answerString:String,questionID:int外键

I'm not sure which one is better, or if there is another way?我不确定哪个更好,或者是否有另一种方法?

Maybe questions_and_answers would get very large and cause long retrieval times and memory problems?也许questions_and_answers会变得非常大并导致检索时间长和 memory 问题? Then again, I assume question_and_answers would be indexed on the primary keys.再一次,我假设question_and_answers将在主键上建立索引。 In the second schema, answers would be indexed on answerID and not questionID ?在第二个模式中, answers将在answerID而不是questionID上建立索引? meaning the search times would go up as the whole table would have to be searched?这意味着搜索时间会增加 go,因为必须搜索整个表?

There may be ~10,000 - 20,000 answers.可能有大约 10,000 - 20,000 个答案。 (the quiz may be run on a mobile device and questions will need to be shown "instantly") (测验可能在移动设备上运行,问题需要“即时”显示)

Note: I don't expect there to much overlap of answers between questions.注意:我不希望问题之间的答案有太多重叠。 I wouldn't think the amount of overlap would mean less data being stored, considering the extra space required by the questions_and_answers table考虑到questions_and_answers表所需的额外空间,我不认为重叠量意味着存储的数据更少

You're second schema is the better one, because it models the actual domain: each question has a set of answers.你的第二个模式更好,因为它模拟了实际的领域:每个问题都有一组答案。 Even if you can "compress" the data by storing duplicate answers once, it does not match the actual domain .即使您可以通过将重复答案存储一次来“压缩”数据,它也与实际域不匹配

Down the road you'll want to edit answers.在路上你会想要编辑答案。 With schema 1, that means first searching if that answer already exists.对于模式 1,这意味着首先搜索该答案是否已经存在。 If it does exist, you then would have to check if any questions still rely on the old answer.如果它确实存在,那么您将不得不检查是否有任何问题仍然依赖于旧答案。 If it did not exist, you would still have to check if any other questions relied on that answer, and then either edit that answer in place or create a new answer.如果它不存在,您仍然需要检查是否有任何其他问题依赖于该答案,然后就地编辑该答案或创建新答案。

Schema 1 just makes life really hard.模式 1 只会让生活变得非常艰难。

To answer your index questions, you would need to add an index on questionId.要回答您的索引问题,您需要在 questionId 上添加索引。 Once you have that index, looking up answers for a question should scale.有了该索引后,查找问题的答案应该可以扩展。

Now, on a completely different note, why use a database for this?现在,换一种说法,为什么要为此使用数据库? Consider storing them as simple documents in a standard format like json. Anytime you query a question, you will almost always want the answers, and vice versa.考虑将它们存储为标准格式的简单文档,如 json。任何时候你查询一个问题,你几乎总是想要答案,反之亦然。 Instead of executing multiple queries, you can load the entire document in one step.您可以一步加载整个文档,而不是执行多个查询。

If you then find you need more advanced storage (queries, redundancy, etc) you can move to a document database like MongoDB or CouchDB.如果您随后发现需要更高级的存储(查询、冗余等),您可以转移到文档数据库,如 MongoDB 或 CouchDB。

It seems deadlock (circular loop) as questionID column is referred as foreign key in answers table and correctAnswerID column is referred as foreign key in questions table.似乎死锁(循环),因为questionID列在答案表中被称为外键,而correctAnswerID列在问题表中被称为外键。

It's better to create a bit type column in answers table to marked the correct answer and remove correctAnswerID column.最好在答案表中创建一个位类型的列来标记正确答案并删除correctAnswerID列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM