9

Is it acceptable to store JSON data in a MySQL table row? I need to store arrays in a mysql database. The problem is, i don't know how many columns i will need for each user. So I thought to store JSON in one row named array for exemple. Is this the best way?

Edit:

Also, I am using text as table column type.

user1947561
  • 1,117
  • 2
  • 8
  • 13

4 Answers4

12

Yes, it's a very good idea to use mysql as a key-value store, in fact facebook does for some uses.

CREATE TABLE `json` (
    `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
    `data` blob NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

The above table-structure will get you far. It's very easy to shard or cluster.

Edit: The point here is to use PHP, Ruby etc to handle the json/data. You do SELECT ..., do your edits, then INSERT ... ON DUPLICATE KEY UPDATE ....

Gustav
  • 2,902
  • 1
  • 25
  • 31
2

Storing more than one piece of data in a relational database field is generally wrong. It is possible to think of cases where it would be acceptable, but not for storing an entire user.

If a using a relational database structure is not sufficient you may want to look at a NoSQL database inestead of MySQL.

jonixj
  • 397
  • 3
  • 11
0

It's not a problem. mySQL can already handle most of the brackets and funky characters in their text field.

apollosoftware.org
  • 12,161
  • 4
  • 48
  • 69
0

I think it is not. What about four months later you decide add a new attribute to your entity or remove some attributes from your entity? How will you parse your old json contents? If you dont know how many columns you will need in your table, you should think in a different way and maybe create a dynamic structure like using user_column table

sedat sevgili
  • 156
  • 1
  • 6