MySQL5.7 JSON類型使用介紹
JSON是一種輕量級的資料交換格式,採用了獨立於語言的文字格式設定,類似XML,但是比XML簡單,易讀並且易編寫。對機器來說易於解析和產生,並且會減少網路頻寬的傳輸。
JSON的格式非常簡單:名稱/索引值。之前MySQL版本裡面要實現這樣的儲存,要麼用VARCHAR要麼用TEXT大文本。 MySQL5.7發布後,專門設計了JSON資料類型以及關於這種類型的檢索以及其他函數解析。 我們先看看MySQL老版本的JSON存取。
樣本表結構:
CREATE TABLE json_test(
id INT,
person_desc TEXT
)ENGINE INNODB;
我們來插入一條記錄:
INSERT INTO json_test VALUES (1,'{
"programmers": [{
"firstName": "Brett",
"lastName": "McLaughlin",
"email": "aaaa"
}, {
"firstName": "Jason",
"lastName": "Hunter",
"email": "bbbb"
}, {
"firstName": "Elliotte",
"lastName": "Harold",
"email": "cccc"
}],
"authors": [{
"firstName": "Isaac",
"lastName": "Asimov",
"genre": "sciencefiction"
}, {
"firstName": "Tad",
"lastName": "Williams",
"genre": "fantasy"
}, {
"firstName": "Frank",
"lastName": "Peretti",
"genre": "christianfiction"
}],
"musicians": [{
"firstName": "Eric",
"lastName": "Clapton",
"instrument": "guitar"
}, {
"firstName": "Sergei",
"lastName": "Rachmaninoff",
"instrument": "piano"
}]
}');
那一般我們遇到這樣來儲存JSON格式的話,只能把這條記錄取出來交個應用程式,有應用程式來解析。
現在到了MySQL5.7,我們重新修改下表結構:
ALTER TABLE json_test MODIFY person_desc json;
先看看插入的這行JSON資料有哪些KEY:
mysql> SELECT id,json_keys(person_desc) as "keys" FROM json_test\G
*************************** 1. row ***************************
id: 1
keys: ["authors", "musicians", "programmers"]
1 row in set (0.00 sec)
我們可以看到,裡面有三個KEY,分別為authors,musicians,programmers。那現在找一個KEY把對應的值拿出來:
mysql> SELECT json_extract(AUTHORS,'$.lastName[0]') AS 'name', AUTHORS FROM
-> (
-> SELECT id,json_extract(person_desc,'$.authors[0][0]') AS "authors" FROM json_test
-> UNION ALL
-> SELECT id,json_extract(person_desc,'$.authors[1][0]') AS "authors" FROM json_test
-> UNION ALL
-> SELECT id,json_extract(person_desc,'$.authors[2][0]') AS "authors" FROM json_test
-> ) AS T1
-> ORDER BY NAME DESC\G
*************************** 1. row ***************************
name: "Williams"
AUTHORS: {"genre": "fantasy", "lastName": "Williams", "firstName": "Tad"}
*************************** 2. row ***************************
name: "Peretti"
AUTHORS: {"genre": "christianfiction", "lastName": "Peretti", "firstName": "Frank"}
*************************** 3. row ***************************
name: "Asimov"
AUTHORS: {"genre": "sciencefiction", "lastName": "Asimov", "firstName": "Isaac"}
3 rows in set (0.00 sec)
現在來把詳細的值羅列出來:
mysql> SELECT
-> json_extract(AUTHORS,'$.firstName[0]') AS "firstname",
-> json_extract(AUTHORS,'$.lastName[0]') AS "lastname",
-> json_extract(AUTHORS,'$.genre[0]') AS "genre"
-> FROM
-> (
-> SELECT id,json_extract(person_desc,'$.authors[0]') AS "authors" FROM json
_test
-> ) AS T\G
*************************** 1. row ***************************
firstname: "Isaac"
lastname: "Asimov"
genre: "sciencefiction"
1 row in set (0.00 sec)
我們進一步來示範把authors 這個KEY對應的所有對象刪掉。
mysql> UPDATE json_test
-> SET person_desc = json_remove(person_desc,'$.authors')\G
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
尋找下對應的KEY,發現已經被刪除掉了。
mysql> SELECT json_contains_path(person_desc,'all','$.authors') as authors_exist
s FROM json_test\G
*************************** 1. row ***************************
authors_exists: 0
1 row in set (0.00 sec)
總結下, 雖然MySQL5.7 開始支援JSON資料類型,但是我建議如果要使用的話,最好是把這樣的值取出來,然後在應用程式段來計算,畢竟資料庫是用來處理簡單資料的。
本文永久更新連結地址: