elibanon
elibanon

Reputation: 45

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, e.g. (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.

Upvotes: 0

Views: 382

Answers (4)

julien_c
julien_c

Reputation: 5092

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.

Upvotes: 1

symcbean
symcbean

Reputation: 48357

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

MySQL is a relational database and you should structure your data accordingly - i.e. 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.

Upvotes: 0

kgilden
kgilden

Reputation: 10356

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

Upvotes: -1

Your Common Sense
Your Common Sense

Reputation: 157889

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

Upvotes: 7

Related Questions