简体   繁体   中英

Database design with undetermined data

Recently I have been planning a system that allows a user to customize and add to a web interface. The app could be compared to a quiz creating system. The problem I'm having is how to design a schema that will allow for "variable" numbers of additions to be made to the application.

The first option that I looked into was just creating an object for the additions and then serializing it and putting it in its own column. The content wouldn't be edited often so writing would be minimal, reads however would be very often. (caching could be used to cut down)

The other option was using something other than mysql or postgresql such as cassandra. I've never used other databases before but would be interested in learning how to use them if they would improve the design of the system.

Any input on the subject would be appreciated.

Thank you.

* edit 29/3/14

Some information on the data being changed. For my idea above of using a serialized object, you could say that in the table I would store the name of the quiz, the number of points the quiz is worth and then a column called quiz data that would store the serialized object containing the information on the questions. So overall the object could look like this:

Questions(Array):{
    [1](Object):Question{
        Field-type(int):1
        Field-title(string):"Whats your gender?"
        Options(Array):{"Female", "Male"}
        }
    [2](Object):Question{
        Field-type(int):2
        Field-title(string):"Whats your name?"
        }
}

The structure could vary of course but generally i would be storing integers to determin the type of field in the quiz and then a field to hold the label for the field and the options (if there are any) for that field.

In this scenario I would advise looking at MongoDB .

However if you want to work with MySQL you can think about the entity-attribute-value model in your design. The EAV model allows you to design for entries that contain a variable number of attributes.

edit

Following your update on the datatypes you would like to store, you could map your design as follows:

+-------------------------------------+
| QuizQuestions                       |
+----+---------+----------------------+
| id | type_id | question_txt         |
+----+---------+----------------------+
| 1  |    1    | What's your gender?  |
| 2  |    2    | What's your name?    |
+----+---------+----------------------+

+-----------------------------------+
| QuestionTypes                     |
+----+--------------+---------------+
| id | attribute_id | description   |
+----+--------------+---------------+
| 1  |      1       | Single select |
| 2  |      2       | Free text     |
+----+--------------+---------------+

+----------------------------+
| QuestionValues             |
+----+--------------+--------+
| id | question_id  | value  |
+----+--------------+--------+
| 1  |      1       | Male   |
| 2  |      1       | Female |
+----+--------------+--------+

+-------------------------------+
| QuestionResponses             |
+----+--------------+-----------+
| id | question_id  | response  |
+----+--------------+-----------+
| 1  |      1       | 1         |
| 2  |      2       | Fred      |
+----+--------------+-----------+

This would then allow you to dynamically add various different questions (QuizQuestions), of different types (QuestionTypes), and then restrict them with different options (QuestionValues) and store those responses (QuestionResponses).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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