I was wondering if using some sort of serialized data whether it be json encoded or serialized etc. was better than using multiple columns in a mysql database.
Example:
If I have a “users” table, some columns would usually be: id, fullname, username, email, password etc.
But what if I used json data instead of having all these columns. Could I use just a 2 columns in my db table (id, data). So I could do something like this in php:
json_encode(array('username' => $_POST['username'], 'password' => md5($_POST['password'])));
And then insert it into a table with a column of “data” which would hold the json encoded array.
Later on in the script, I could just retrieve the ‘data’ column and use json_decode($stuff_from_db, true)
I’m sorry if this is a dumb question, I’m not really very knowledgeable about mysql and how it scales etc. But I would like to know if this would be a good or bad idea.
Thanks
No, it wouldn’t be a good idea and that would take the Relational out of RDBMS. You wouldn’t be able to quickly query anything out of the table without having to parse the results of each row first. On a small scale this would be okay, but then MySQL wouldn’t be the database to use if this is what you had in mind for storing data.
Imagine trying to get the following out of your database if you stored it using serialization or encoding with JSON: