MariaDB 10.0.X中,動態列支援 JSON 格式來擷取資料。

來源:互聯網
上載者:User

標籤:json   mongodb   動態列   mariadb   

MariaDB 10.0.X中,動態列(Dynamic Columns),可以支援 JSON 格式來擷取資料。


為了相容傳統SQL文法,MariaDB 10和MySQL5.7支援原生JSON格式,即關係型資料庫和文檔型NoSQL資料庫集於一身


使用說明:

###表結構

create table assets (  item_name varchar(32) primary key, -- A common attribute for all items  dynamic_cols  blob  -- Dynamic columns will be stored here);


###插入JSON格式資料

mysql> INSERT INTO assets VALUES     ->   (‘MariaDB T-shirt‘, COLUMN_CREATE(‘color‘, ‘blue‘, ‘size‘, ‘XL‘));Query OK, 1 row affected (0.02 sec)mysql> INSERT INTO assets VALUES    ->   (‘Thinkpad Laptop‘, COLUMN_CREATE(‘color‘, ‘black‘, ‘price‘, 500));Query OK, 1 row affected (0.01 sec)


###擷取Key(鍵)color的Value(值):

mysql> SELECT item_name, COLUMN_GET(dynamic_cols, ‘color‘ as char) AS color FROM assets;+-----------------+-------+| item_name       | color |+-----------------+-------+| MariaDB T-shirt | blue  || Thinkpad Laptop | black |+-----------------+-------+2 rows in set (0.00 sec)


###擷取全部Key(鍵)

mysql> SELECT item_name, column_list(dynamic_cols) FROM assets;+-----------------+---------------------------+| item_name       | column_list(dynamic_cols) |+-----------------+---------------------------+| MariaDB T-shirt | `size`,`color`            || Thinkpad Laptop | `color`,`price`           |+-----------------+---------------------------+2 rows in set (0.00 sec)


###擷取全部Key-Value

mysql> SELECT item_name, COLUMN_JSON(dynamic_cols) FROM assets;+-----------------+-------------------------------+| item_name       | COLUMN_JSON(dynamic_cols)     |+-----------------+-------------------------------+| MariaDB T-shirt | {"size":"XL","color":"blue"}  || Thinkpad Laptop | {"color":"black","price":500} |+-----------------+-------------------------------+2 rows in set (0.01 sec)


###刪除一個Key-Value:

mysql> UPDATE assets SET dynamic_cols=COLUMN_DELETE(dynamic_cols, "price")     -> WHERE COLUMN_GET(dynamic_cols, ‘color‘ as char)=‘black‘;Query OK, 1 row affected (0.00 sec)Rows matched: 1  Changed: 1  Warnings: 0mysql> SELECT item_name, COLUMN_JSON(dynamic_cols) FROM assets;+-----------------+------------------------------+| item_name       | COLUMN_JSON(dynamic_cols)    |+-----------------+------------------------------+| MariaDB T-shirt | {"size":"XL","color":"blue"} || Thinkpad Laptop | {"color":"black"}            |+-----------------+------------------------------+2 rows in set (0.00 sec)


###增加一個Key-Value:

mysql> UPDATE assets SET dynamic_cols=COLUMN_ADD(dynamic_cols, ‘warranty‘, ‘3 years‘)    -> WHERE item_name=‘Thinkpad Laptop‘;Query OK, 1 row affected (0.01 sec)Rows matched: 1  Changed: 1  Warnings: 0mysql> SELECT item_name, COLUMN_JSON(dynamic_cols) FROM assets;                            +-----------------+----------------------------------------+| item_name       | COLUMN_JSON(dynamic_cols)              |+-----------------+----------------------------------------+| MariaDB T-shirt | {"size":"XL","color":"blue"}           || Thinkpad Laptop | {"color":"black","warranty":"3 years"} |+-----------------+----------------------------------------+2 rows in set (0.00 sec)


###更改一個Key-Value:

mysql> UPDATE assets SET dynamic_cols=COLUMN_ADD(dynamic_cols,‘color‘, ‘white‘) WHERE COLUMN_GET(dynamic_cols, ‘color‘ as char)=‘black‘;Query OK, 1 row affected (0.01 sec)Rows matched: 1  Changed: 1  Warnings: 0mysql> SELECT item_name, COLUMN_JSON(dynamic_cols) FROM assets;+-----------------+----------------------------------------+| item_name       | COLUMN_JSON(dynamic_cols)              |+-----------------+----------------------------------------+| MariaDB T-shirt | {"size":"XL","color":"blue"}           || Thinkpad Laptop | {"color":"white","warranty":"3 years"} |+-----------------+----------------------------------------+2 rows in set (0.00 sec)



本文出自 “賀春暘的技術專欄” 部落格,請務必保留此出處http://hcymysql.blog.51cto.com/5223301/1694181

MariaDB 10.0.X中,動態列支援 JSON 格式來擷取資料。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.