How to store multiple options in a single table?

后端 未结 2 602
猫巷女王i
猫巷女王i 2020-11-21 23:29

I want to design an application for result computation.

First, I need to know how to store record in a MySQL database in such a way that students can have as many co

2条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 00:20

    For completeness sake, not in a matter that this is a general recommended solution:

    MySQL as of version 5.7.8 provides the the JSON datatype, which allows to store and retrieve objects and arrays in the JSON format.

    This way, you can store entire objects and arrays into a field, as an array would look like:

     ['subject_1', 'subject_2', 'subject_3']
    

    Especially beginners don't know this, and they reinvent the wheel by yet another comma-separated string implementation or using language-dependent serialization/deserialization approaches.

    At least JSON is very commonly used and easily parsed as a data exchange format.

    There are valid use cases for using storing arrays and objects inside a MySQL field, e.g. for speed optimization or when you have unknown or dynamic properties that you still want to save in a DB.

    Yet as a rule of thumb, if you rely on storing objects and arrays into MySQL, then either your database design is most likely broken or you should rather use a document-based database like MongoDB.

提交回复
热议问题