自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL JSON數(shù)據(jù)類(lèi)型如何操作?這里告訴你~

數(shù)據(jù)庫(kù) MySQL
mysql 自 5.7.8 版本開(kāi)始,就支持了 json 結(jié)構(gòu)的數(shù)據(jù)存儲(chǔ)和查詢(xún),這表明了 mysql 也在不斷的學(xué)習(xí)和增加 nosql 數(shù)據(jù)庫(kù)的優(yōu)點(diǎn)。但 mysql 畢竟是關(guān)系型數(shù)據(jù)庫(kù),在處理 json 這種非結(jié)構(gòu)化的數(shù)據(jù)時(shí),還是比較別扭的。

 

概述

mysql 自 5.7.8 版本開(kāi)始,就支持了 json 結(jié)構(gòu)的數(shù)據(jù)存儲(chǔ)和查詢(xún),這表明了 mysql 也在不斷的學(xué)習(xí)和增加 nosql 數(shù)據(jù)庫(kù)的優(yōu)點(diǎn)。但 mysql 畢竟是關(guān)系型數(shù)據(jù)庫(kù),在處理 json 這種非結(jié)構(gòu)化的數(shù)據(jù)時(shí),還是比較別扭的。

創(chuàng)建一個(gè) JSON 字段的表

首先先創(chuàng)建一個(gè)表,這個(gè)表包含一個(gè) json 格式的字段:

  1. CREATE TABLE table_name ( 
  2.     id INT NOT NULL AUTO_INCREMENT,  
  3.     json_col JSON, 
  4.     PRIMARY KEY(id) 
  5. ); 

上面的語(yǔ)句,主要注意 json_col 這個(gè)字段,指定的數(shù)據(jù)類(lèi)型是 JSON。

插入一條簡(jiǎn)單的 JSON 數(shù)據(jù)

  1. INSERT INTO 
  2.     table_name (json_col)  
  3. VALUES 
  4.     ( 
  5. '{"City": "Galle", "Description": "Best damn city in the world"}' 
  6. ); 

上面這個(gè) SQL 語(yǔ)句,主要注意 VALUES 后面的部分,由于 json 格式的數(shù)據(jù)里,需要有雙引號(hào)來(lái)標(biāo)識(shí)字符串,所以,VALUES 后面的內(nèi)容需要用單引號(hào)包裹。

插入一條復(fù)雜的 JSON 數(shù)據(jù) 

  1. INSERT INTO table(col)  
  2. VALUES
  3. '{"opening":"Sicilian","variations":["pelikan","dragon","najdorf"]}' 
  4. ); 

這地方,我們插入了一個(gè) json 數(shù)組。主要還是注意單引號(hào)和雙引號(hào)的問(wèn)題。

修改 JSON 數(shù)據(jù)

之前的例子中,我們插入了幾條 JSON 數(shù)據(jù),但是如果我們想修改 JSON 數(shù)據(jù)里的某個(gè)內(nèi)容,怎么實(shí)現(xiàn)了?比如我們向 variations 數(shù)組里增加一個(gè)元素,可以這樣:

  1. UPDATE myjson SET dict=JSON_ARRAY_APPEND(dict,'$.variations','scheveningen'WHERE id = 2; 

這個(gè) SQL 語(yǔ)句中,$ 符合代表 JSON 字段,通過(guò). 號(hào)索引到 variations 字段,然后通過(guò) JSON_ARRAY_APPEND 函數(shù)增加一個(gè)元素?,F(xiàn)在我們執(zhí)行查詢(xún)語(yǔ)句:

  1. SELECT * FROM myjson 

得到的結(jié)果是: 

  1. +----+-----------------------------------------------------------------------------------------+ 
  2. | id | dict                                                                                    | 
  3. +---+-----------------------------------------------------------------------------------------+ 
  4. |    | { "opening" :  "Sicilian" ,  "variations" : [ "pelikan" ,  "dragon" ,  "najdorf" ,  "scheveningen" ]} | 
  5. +----+-----------------------------------------------------------------------------------------+ 
  6.  row  in   set  ( 0.00  sec) 

關(guān)于 MySQL 中,JSON 數(shù)據(jù)的獲取方法,參照官方鏈接 JSON Path Syntax

創(chuàng)建索引

MySQL 的 JSON 格式數(shù)據(jù)不能直接創(chuàng)建索引,但是可以變通一下,把要搜索的數(shù)據(jù)單獨(dú)拎出來(lái),單獨(dú)一個(gè)數(shù)據(jù)列,然后在這個(gè)字段上鍵一個(gè)索引。下面是官方的例子: 

  1. mysql> CREATE TABLE jemp ( 
  2.     ->     c JSON, 
  3.     ->     g INT GENERATED ALWAYS AS (c-> "$.id" ), 
  4.     ->     INDEX i (g) 
  5.     -> ); 
  6. Query  OK,   rows affected ( 0.28  sec) 
  7. mysql> INSERT INTO jemp (c) VALUES 
  8.      >   ( '{"id": "1", "name": "Fred"}' ), ( '{"id": "2", "name": "Wilma"}' ), 
  9.      >   ( '{"id": "3", "name": "Barney"}'), ('{"id": "4", "name": "Betty"}' ); 
  10. Query  OK,   rows affected ( 0.04  sec) 
  11. Records :  Duplicates:  Warnings :  
  12. mysql> SELECT c->> "$.name"  AS name 
  13.      >     FROM jemp WHERE g >  
  14. +--------+ 
  15. name   | 
  16. +--------+ 
  17. |  Barney  | 
  18. |  Betty   | 
  19. +--------+ 
  20.  rows  in set 0.00  sec) 
  21. mysql> EXPLAIN SELECT c->> "$.name"  AS name 
  22.      >    FROM jemp WHERE g >  \G 
  23. ***************************  1.  row *************************** 
  24.            id:  
  25.   select_type: SIMPLE 
  26.         table: jemp 
  27.    partitions: NULL 
  28.          type: range 
  29. possible_keys: i 
  30.           key: i 
  31.       key_len:  
  32.            
  33. ref NULL 
  34.          rows:  
  35.      filtered: 100.00          
  36. Extra:  Using where
  37.   row  in set ,   warning ( 0.00  sec) 
  38. mysql> SHOW WARNINGS\G 
  39. ***************************  1 row *************************** 
  40.    
  41. Level :  Note 
  42.     
  43. Code :  1003 
  44. Message :  /* select#1 */ select  json_unquote(json_extract( `test` `jemp` `c` '$.name')) 
  45. AS  `namefrom `test` `jemp`where  ( `test` `jemp` `g`  >  
  46. 1 row  in set ( 0.00  sec) 

這個(gè)例子很簡(jiǎn)單,就是把 JSON 字段里的 id 字段,單獨(dú)拎出來(lái)成字段 g,然后在字段 g 上做索引,查詢(xún)條件也是在字段 g 上。

字符串轉(zhuǎn) JSON 格式

把 json 格式的字符串轉(zhuǎn)換成 MySQL 的 JSON 類(lèi)型:

  1. SELECT CAST('[1,2,3]' as JSON) ; 
  2.  
  3. SELECT CAST('{"opening":"Sicilian","variations":["pelikan","dragon","najdorf"]}' as JSON);  

所有 MYSQL JSON 函數(shù)

  1. Name  Description 
  2. JSON_APPEND()  Append data to JSON document 
  3. JSON_ARRAY()  Create JSON array 
  4. JSON_ARRAY_APPEND()  Append data to JSON document 
  5. JSON_ARRAY_INSERT()  Insert into JSON array-> Return value from JSON column after evaluating path; equivalent to JSON_EXTRACT(). 
  6. JSON_CONTAINS()  Whether JSON document contains specific object at path 
  7. JSON_CONTAINS_PATH()  Whether JSON document contains any data at path 
  8. JSON_DEPTH()  Maximum depth of JSON document 
  9. JSON_EXTRACT()  Return data from JSON document->> Return value from JSON column after evaluating path and unquoting the result; equivalent to JSON_UNQUOTE(JSON_EXTRACT()). 
  10. JSON_INSERT()  Insert data into JSON document 
  11. JSON_KEYS()  Array of keys from JSON document 
  12. JSON_LENGTH()  Number of elements in JSON document 
  13. JSON_MERGE()  Merge JSON documents, preserving duplicate keys. Deprecated synonym for JSON_MERGE_PRESERVE() 
  14. JSON_MERGE_PRESERVE()  Merge JSON documents, preserving duplicate keys 
  15. JSON_OBJECT()  Create JSON object 
  16. JSON_QUOTE()  Quote JSON document 
  17. JSON_REMOVE()  Remove data from JSON document 
  18. JSON_REPLACE()  Replace values in JSON document 
  19. JSON_SEARCH()  Path to value within JSON document 
  20. JSON_SET()  Insert data into JSON document 
  21. JSON_TYPE()  Type of JSON value 
  22. JSON_UNQUOTE()  Unquote JSON value 
  23. JSON_VALID()  Whether JSON value is valid 
責(zé)任編輯:龐桂玉 來(lái)源: ITPUB
相關(guān)推薦

2011-05-26 13:54:04

Json

2017-07-10 13:38:07

MySQL數(shù)據(jù)類(lèi)型整數(shù)類(lèi)型

2010-10-15 13:28:34

MySql數(shù)據(jù)類(lèi)型

2019-11-12 08:53:32

PG數(shù)據(jù)數(shù)據(jù)庫(kù)

2010-06-13 18:00:56

MySQL數(shù)據(jù)類(lèi)型

2010-06-10 10:06:01

MySQL數(shù)據(jù)類(lèi)型

2020-09-18 10:18:08

MySQL數(shù)據(jù)插入數(shù)據(jù)庫(kù)

2017-08-25 09:18:04

2010-10-08 14:04:44

MySQL數(shù)值數(shù)據(jù)類(lèi)型

2010-01-07 16:55:06

JSON字符串

2009-09-01 16:35:55

C#操作String數(shù)

2010-09-15 14:02:07

關(guān)鍵數(shù)據(jù)備份

2016-11-01 14:37:15

老齡化智慧養(yǎng)老大數(shù)據(jù)

2018-04-04 12:54:51

航空大數(shù)據(jù)航班延誤

2024-03-25 08:18:31

2017-10-24 14:05:16

MySQLSchema數(shù)據(jù)類(lèi)型

2019-08-12 11:40:48

數(shù)據(jù)庫(kù)SQLite3數(shù)據(jù)類(lèi)型

2016-08-18 14:13:55

JavaScript基本數(shù)據(jù)引用數(shù)據(jù)

2014-01-05 17:08:09

PostgreSQL數(shù)據(jù)類(lèi)型

2010-05-31 10:35:12

MySQL數(shù)據(jù)類(lèi)型
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)