Storing a multiple choice quiz in a database - deciding the schema

后端 未结 2 1396
天涯浪人
天涯浪人 2021-02-01 23:35

I am trying to implement a multiple choice quiz and will want to store all my questions and answers in a SQLite database. I will have many questions, and for each question there

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-02 00:26

    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. 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.

    To answer your index questions, you would need to add an index on 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. 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.

提交回复
热议问题