database design for quiz with different languages

我是研究僧i 提交于 2019-12-13 02:22:47

问题


I'm trying to make a quiz that supports multiple languages. I have made the database schema for the quiz and user results but not sure how to implement support for different languages in a good way.

This what it looks like right now (feel free to point out design mistakes):

* User:
   - id          PK
   - name
   - email
   - password

* Quiz
   - id          PK
   - slug
   - title

* Question
   - id          PK
   - quiz_id     FK
   - question
   - image
   - message

* Option
   - id           PK
   - question_id  FK
   - option
   - is_right

Relation between user and quiz to store results:

* Result
   - id           PK
   - user_id      FK
   - quiz_id      FK

* Result Details
   - id           PK
   - result_id    FK
   - question_id  FK
   - option_id    FK (The option user choose)
   - is_right

Same database design but clear connections between tables:

My first thought was to make a parent language table to the quiz table (language table has many quizzes):

* Language:
   - id          PK
   - language_title

* Quiz
   - id           PK
   - language_id  FK
   - slug
   - title

But this would only create a language category and the administrator would have to create a whole new quiz for a different language and not just add new question text and option text.

How do I design the quiz database for multiple languages so only the columns with text (title, message, question and option) has to get a new record instead of having to create a whole new quiz?


回答1:


I would propose this general route:

1) Create a table (translation?) with fields language_id,string_id,value. Each string_id corresponds to a specific message regardless of language. Thus, all string_ids x all language_ids = all possible messages for different translations. Thus, (language_id,string_id) is the PK, and value is the thing your quiztaker should see.

2) Replace each text field on your tables with a string_id.

3) When your presenting environment must show a string_id, it will SELECT it from the table mentioned in 1) WHERE language_id=the one chosen by your quiz-taker




回答2:


First of all, you should review your choice of keys. Your current design (atomic surrogate keys everywhere) allows for inconsistency all over the place. Unless you have additional, unmentioned multi-table constraints, you could e.g. have one Result_detail belonging to Question A from Quiz B, but with an Option belonging to Question C from Quiz D as well as a Result belonging to Quiz E.

And unless you have additional, unmentioned keys, your design allows for practical duplicates. You could e.g. have two Result_details that are identical except for the id, which would seem to mean that the user in question answered the same question twice in the same Result. It may be a feature to let a user takes the same quiz multiple times, but you probably don't want multiple equal (or different, for that matter) answers to the same question within the same quiz result. This is actually a pretty good example of how uncritical introduction of surrogate keys in some cases completely misses the point of keys.

Result_details.Is_right appears redundant. Can you have a Result_detail that is right even if its Option is not?

There are other problems as well, but to get to your main question: If you have a fixed number of languages (particularly if all questions and options have strings in all languages), you can just add Question and Option columns for each language. If anyone yells about first normal form, ignore them (or ask them to formally define 1NF).

If you have an open-ended number of languages, move the Question and Option columns into separate tables, along with a Language column and foreign keys to the Question and Option tables, respectively. Use e.g. ISO 639 for the values of the Language column; that way you won't necessarily need a Language table.




回答3:


Database design should follow from a more general information design model derived from a conceptual information model, preferably in the form of UML Class Diagrams (because of their expressivity). The following is a conceptual information model for your problem:

Such a model still has to be enriched with suitable standard identifier attributes and data types for obtaining an information design model. By eliminating the associations and compositions (replacing them with reference properties), we obtain the following OO class model, which can be used as the basis for coding Java/C#/PHP/etc. classes:

Notice that we have added support for multilingual quizzes in this OO class model by adding an IsoLanguageCode enumeration and a TextItem class with a two-part primary key consisting of a text item ID and a language code such that quizzes, questions and answer options use a text item ID for referencing the text items used as their title, question text and answer text. Notice also that the Quiz class has a derived property \availableLanguages that can be computed with the help of a query retrieving all languages for which text items for all questions of a quiz, and all their answer options, are available.

An SQL database design model can be derived from such an OO class model by replacing the reference properties with corresponding foreign key attributes:



来源:https://stackoverflow.com/questions/51022843/database-design-for-quiz-with-different-languages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!