JSON support. MySQL5.7.8開始原生支持JSON數(shù)據(jù)類型,不再以字符串形式存儲而是以二進(jìn)制格式存儲,允許快速讀取文檔元素。JSON列在插入或更新時會自動進(jìn)行驗(yàn)證叫搁,文檔格式不正確會報錯。除了可用常用比較操作符進(jìn)行比較外還引入一系列函數(shù)用于處理JSON類型。
MySQL 5.7 使用原生JSON類型的例子
一渴逻、創(chuàng)建表
CREATE TABLE `json` (
`id` int(11) DEFAULT NULL,
`content` json DEFAULT NULL
)
二疾党、插入數(shù)據(jù)
mysql> insert into json values(3,'{"name":"測試"}');
Query OK, 1 row affected (0.06 sec)
三、查詢數(shù)據(jù)
mysql> select * from json;
+------+---------------------+
| id | content |
+------+---------------------+
| 1 | {"title": "標(biāo)題"} |
| 2 | {"title": "新聞"} |
| 3 | {"name": "測試"} |
+------+---------------------+
3 rows in set (0.00 sec)
mysql> select * from json where content->'$.title' = '標(biāo)題';
+------+---------------------+
| id | content |
+------+---------------------+
| 1 | {"title": "標(biāo)題"} |
+------+---------------------+
1 row in set (0.00 sec)
mysql> select * from json where content->'$.title' like '%新%';
+------+---------------------+
| id | content |
+------+---------------------+
| 2 | {"title": "新聞"} |
+------+---------------------+
1 row in set (0.00 sec)
很明顯惨奕,Mysql新的數(shù)據(jù)類型給我們帶來了更多應(yīng)用上的便利雪位。