/    Sign up×
Community /Pin to ProfileBookmark

Store an array in a table column

Dear users,

I want to create a new table in mysql containing a field which can contain an array (or a json).
I want to perform query to add an element to that array, remove or update ones. In select mode I want to get an array or a json to read all the field content.

How can I do?

Thanks

to post a comment

7 Comments(s)

Copy linkTweet thisAlerts:
@rootApr 23.2018 — You can use PHP to insert data in to a database with the mysqli_ suite of commands.

It will help if you have an actual example of your data, then someone can tell you what sort of table structure will be appropriate.
Copy linkTweet thisAlerts:
@NogDogApr 24.2018 — While it has a bit of a "code smell" to me that you want to do that, probably the way I'd do it (if I had to) is to serialize() the array, then store the result in a text field. To modify it, you'd have to first query the table to get the string into PHP, where you would unserialize() it, make your changes in the array, then serialize() and update the DB with the new serialized string.
Copy linkTweet thisAlerts:
@redagrauthorApr 24.2018 — I know how to use php, I use it.

I want to know if there is a json type of mysql field or similar to store a json, which can have limited or unlimited size.

And if possible, a sql query to add, remove or update elements in json field.

I heard some time ago this was possible, but I don't know if it is true.

Thanks
Copy linkTweet thisAlerts:
@NogDogApr 24.2018 — Never used it, but here's my first result when I googled for "mysql json type": https://dev.mysql.com/doc/refman/8.0/en/json.html

I've used the PostgreSQL jsonb type a bit, but have never tried to update it once stored.
Copy linkTweet thisAlerts:
@redagrauthorApr 24.2018 — I read the docs and I saw this is available from 8.0 version, but I have 5.7 version.

Is there a way in mysql 5.7 to store multiple data in a field value?

Example: the table is users, the column is cars, and an user can have more than one car, how can I store in a single field all the cars owned by ad user (one or more)?

Thanks
Copy linkTweet thisAlerts:
@NogDogApr 24.2018 — The normalized way would be with a separate table with a many-to-one foreign key to the users table, using appropriate joins when retrieving data (maybe using group_concat() to put any associated cars into one delimited string?).

If you want to go with non-normalized data and put them all in one field, then I think you're stuck with doing any editing in the application code and then updating the DB with the revised string (JSON, serialized, whatever) -- which also makes any queries against that data inefficient at best.
Copy linkTweet thisAlerts:
@redagrauthorApr 25.2018 — I tried to use a JSON field in mysql 5.7 and it works correctly. Now I want to know the size of that field, how much json I can put in.

Thanks
×

Success!

Help @redagr spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...