Home >Database >Mysql Tutorial >mysql how to store json
How can I efficiently store JSON data in a MySQL database?
For efficient storage of JSON data in MySQL, you can use the JSON data type introduced in MySQL 5.7.23. This data type allows you to store JSON documents as a single value in a column, preserving the hierarchical structure and data types of the JSON object. To store JSON data using the JSON data type, simply create a column with the JSON data type, for example:
<code>CREATE TABLE json_table ( id INT NOT NULL, json_data JSON );</code>
You can then insert JSON data into the column using the JSON_VALUE() function, for example:
<code>INSERT INTO json_table (id, json_data) VALUES (1, JSON_VALUE('{"name": "John Doe", "age": 30}'));</code>
What are the best practices for designing a MySQL schema for JSON storage?
When designing a MySQL schema for JSON storage, consider the following best practices:
How can I retrieve and query JSON data stored in a MySQL database?
To retrieve and query JSON data stored in a MySQL database, you can use the JSON_VALUE() and JSON_QUERY() functions.
For example, to retrieve the name from the JSON document stored in the json_data column of the json_table table, you can use the following query:
<code>SELECT JSON_VALUE(json_data, '$.name') FROM json_table WHERE id = 1;</code>
The above is the detailed content of mysql how to store json. For more information, please follow other related articles on the PHP Chinese website!