简体   繁体   中英

What is the best way to store a list of key values in one mysql column

I need to store data that can be variable, eg (data1='test1',data2='test2').

Is json the best way to do this ? or there is a better way?

Creating a table to contain the value, can't be done since the keys are dynamic.

create a table where columns would be your keys and rows would contain values

And never use your database to store data like json. that's disgusting

I would do it in JSON, yes. Just be sure to have a column data size that's long enough to accomodate your JSON strings.

You might want to have a look at MongoDB and other non-structured databases one day though, they're the more elegant way to do that.

JSON is a format for storing transfering values into/within javascript.

MySQL is a relational database and you should structure your data accordingly - ie it should be normalised. From the information you've provided it is impossible to say whether the right relational model for your data would across multiple columns or across multiple rows .

You can use serialize() to store your array in a single column. Use unserialize() when fetching the data from your table.

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