How to Craft and Embed JSON Objects into Your MySQL Database
As a novice to JSON and MySQL, encountering errors when attempting to create JSON objects and extract values for your MySQL tables can be frustrating. This guide will equip you with the knowledge you need to overcome these obstacles.
Creating JSON Objects
To establish a JSON object, define your MySQL table field as a JSON datatype:
<code class="sql">CREATE TABLE `person` ( `name` JSON DEFAULT NULL );</code>
Insert JSON Data
To insert JSON data into your table, you can either use an array approach:
<code class="sql">INSERT INTO `person` (`name`) VALUES ('["name1", "name2", "name3"]');</code>
Or specify key-value pairs:
<code class="sql">INSERT INTO person VALUES ('{"pid": 101, "name": "name1"}'); INSERT INTO person VALUES ('{"pid": 102, "name": "name2"}');</code>
Retrieve JSON Data
To select JSON data from your table, execute a query like this:
<code class="sql">SELECT * FROM `person` WHERE JSON_CONTAINS(name, '["name1"]');</code>
Key Considerations
Remember that JSON support is limited to MySQL versions 5.7 and higher using InnoDB as the storage engine.
The above is the detailed content of Here are a few question-style titles based on your article: * **How to Store and Retrieve JSON Objects in MySQL: A Beginner\'s Guide** * **MySQL JSON: A Simple Guide to Creating, Inserting, and Retri. For more information, please follow other related articles on the PHP Chinese website!