首页 > 数据库 > mysql教程 > MariaDB10.0.X中,动态列支持JSON格式来获取数据。

MariaDB10.0.X中,动态列支持JSON格式来获取数据。

WBOY
发布: 2016-06-07 14:56:09
原创
1639 人浏览过

MariaDB 10.0.X中,动态列(Dynamic Columns),可以支持 JSON 格式来获取数据。 为了兼容传统SQL语法,MariaDB 10和MySQL5.7支持原生JSON格式, 即关系型数据库和文档型NoSQL数据库集于一身 。 使用说明: ###表结构 createtableassets(item_namevarchar(32

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: 0

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"}            |
+-----------------+------------------------------+
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: 0

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","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: 0

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":"white","warranty":"3 years"} |
+-----------------+----------------------------------------+
2 rows in set (0.00 sec)
登录后复制



相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板