Part1:JSON格式的支持

MySQL5.7版本终于支持了原生的JSON格式,即将关系型数据库和文档型NO_SQL数据库集于一身。本文接下来将对这特性分别就MySQL5.7和MariaDB10.1各自实现的方法异同进行介绍和演示。

Part2:创建相应表结构

[root@HE3 ~]# mysql -V

mysql Ver 14.14 Distrib 5.7.15, for linux-glibc2.5 (x86_64) using EditLine wrapper

mysql> create database helei;

Query OK, 1 row affected (0.00 sec)

mysql> use helei;

Database changed

mysql> create table helei (id int(10) unsigned NOT NULL,context json default null,primary key(id));

Query OK, 0 rows affected (0.02 sec)

mysql> show create table helei G

*************************** 1. row ***************************

Table: helei

Create Table: CREATE TABLE `helei` (

`id` int(10) unsigned NOT NULL,

`context` json DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8

1 row in set (0.02 sec)

Part3:构造数据&测试mysql> desc helei;

+---------+------------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+---------+------------------+------+-----+---------+-------+

| id | int(10) unsigned | NO | PRI | NULL | |

| context | json | YES | | NULL | |

+---------+------------------+------+-----+---------+-------+

2 rows in set (0.00 sec)

mysql> insert into helei values(1,'{"name":"贺磊","age":100}'),(2,'{"name":"陈加持","age":30}'),(3,'{"name":"于浩","age":28}');

Query OK, 3 rows affected (0.00 sec)

Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from helei;

+----+----------------------------------+

| id | context |

+----+----------------------------------+

| 1 | {"age": 100, "name": "贺磊"} |

| 2 | {"age": 30, "name": "陈加持"} |

| 3 | {"age": 28, "name": "于浩"} |

+----+----------------------------------+

3 rows in set (0.00 sec)

mysql> select id,JSON_EXTRACT(context,'$.name') name,JSON_EXTRACT(context,'$.age') age from helei;

+----+-------------+------+

| id | name | age |

+----+-------------+------+

| 1 | "贺磊" | 100 |

| 2 | "陈加持" | 30 |

| 3 | "于浩" | 28 |

+----+-------------+------+

3 rows in set (0.00 sec)

获取Key-Value

mysql> select id,json_keys(context) from helei;

+----+--------------------+

| id | json_keys(context) |

+----+--------------------+

| 1 | ["age", "name"] |

| 2 | ["age", "name"] |

| 3 | ["age", "name"] |

+----+--------------------+

3 rows in set (0.00 sec)

获取全部Key

mysql> update helei set context=JSON_INSERT(context,'$.name',"贺磊",'$.address','beijing')where id=1;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from helei;

+----+------------------------------------------------------+

| id | context |

+----+------------------------------------------------------+

| 1 | {"age": 100, "name": "贺磊", "address": "beijing"} |

| 2 | {"age": 30, "name": "陈加持"} |

| 3 | {"age": 28, "name": "于浩"} |

+----+------------------------------------------------------+

3 rows in set (0.00 sec)

增加Key-Value

mysql> update helei set context=JSON_SET(context,'$.name',"高穷帅")where id=1;

Query OK, 1 row affected (0.01 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from helei;

+----+---------------------------------------------------------+

| id | context |

+----+---------------------------------------------------------+

| 1 | {"age": 100, "name": "高穷帅", "address": "beijing"} |

| 2 | {"age": 30, "name": "陈加持"} |

| 3 | {"age": 28, "name": "于浩"} |

+----+---------------------------------------------------------+

3 rows in set (0.00 sec)

变更key-value

mysql> update helei set context=JSON_REMOVE(context,'$.name') where id=1;

Query OK, 1 row affected (0.01 sec)

Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from helei;

+----+------------------------------------+

| id | context |

+----+------------------------------------+

| 1 | {"age": 100, "address": "beijing"} |

| 2 | {"age": 30, "name": "陈加持"} |

| 3 | {"age": 28, "name": "于浩"} |

+----+------------------------------------+

3 rows in set (0.00 sec)

删除Key-Value

JSON格式存储BLOB的测试

Part1:Dynamic Columns处理方式的异同

①MySQL5.7的动态列JSON格式存储mysql> insert into helei_blob values(1,'{"name":"贺磊","age":100}'),(2,'{"name":"陈加持","age":30}'),(3,'{"name":"于浩","age":28}');

Query OK, 3 rows affected (0.01 sec)

Records: 3 Duplicates: 0 Warnings: 0

mysql> select * from helei_blob;

+----+-------------------------------+

| id | blob_col |

+----+-------------------------------+

| 1 | {"name":"贺磊","age":100} |

| 2 | {"name":"陈加持","age":30} |

| 3 | {"name":"于浩","age":28} |

+----+-------------------------------+

3 rows in set (0.00 sec)

②MariaDB的动态列JSON格式存储MariaDB [helei]> create table helei (id int(10) unsigned NOT NULL,context json default null,primary key(id));

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'json default null,primary key(id))' at line 1

可以看到MariaDB并不能直接存储JSON类型。

MariaDB [helei]> show create table helei_blobG;

*************************** 1. row ***************************

Table: helei_blob

Create Table: CREATE TABLE `helei_blob` (

`id` int(10) unsigned NOT NULL AUTO_INCREMENT,

`blob_col` blob,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8

1 row in set (0.00 sec)

ERROR: No query specified

MariaDB [helei]> insert into helei_blob values(5,column_create('color','blue','size','XL'));

Query OK, 1 row affected (0.01 sec)

MariaDB [helei]> select * from helei_blob;

+----+--------------------------------+

| id | blob_col |

+----+--------------------------------+

| 1 | {"name":"贺磊","age":100} |

| 2 | {"name":"陈加持","age":30} |

| 3 | {"name":"于浩","age":28} |

| 5 | 3 sizecolor!XL!blue |

+----+--------------------------------+

4 rows in set (0.00 sec)

直接查询是乱码需用以下函数查询

MariaDB [helei]> select id,column_json(blob_col) from helei_blob where id =5;

+----+------------------------------+

| id | column_json(blob_col) |

+----+------------------------------+

| 5 | {"size":"XL","color":"blue"} |

+----+------------------------------+

1 row in set (0.00 sec)

MariaDB [helei]> select id,column_list(blob_col) from helei_blob where id =5;

+----+-----------------------+

| id | column_list(blob_col) |

+----+-----------------------+

| 5 | `size`,`color` |

+----+-----------------------+

1 row in set (0.00 sec)

获取全部Key

MariaDB [helei]> select id,column_get(blob_col,'color' as char) as color from helei_blob where id =5;

+----+-------+

| id | color |

+----+-------+

| 5 | blue |

+----+-------+

1 row in set (0.00 sec)

获取Key-Value

MariaDB [helei]> update helei_blob set blob_col=column_add(blob_col,'sex','man') where id=5;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

MariaDB [helei]> select id,column_json(blob_col) from helei_blob where id=5;

+----+------------------------------------------+

| id | column_json(blob_col) |

+----+------------------------------------------+

| 5 | {"sex":"man","size":"XL","color":"blue"} |

+----+------------------------------------------+

1 row in set (0.00 sec)

增加Key-Value

MariaDB [helei]> update helei_blob set blob_col=column_add(blob_col,'color','black') where id=5;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 0

MariaDB [helei]> select id,column_json(blob_col) from helei_blob where id=5;

+----+-------------------------------------------+

| id | column_json(blob_col) |

+----+-------------------------------------------+

| 5 | {"sex":"man","size":"XL","color":"black"} |

+----+-------------------------------------------+

1 row in set (0.00 sec)

更改Key-Value

MariaDB [helei]> update helei_blob set blob_col=column_delete(blob_col,'sex','man') where id=5;

Query OK, 1 row affected (0.01 sec)

Rows matched: 1 Changed: 1 Warnings: 0

MariaDB [helei]> select id,column_json(blob_col) from helei_blob where id=5;

+----+-------------------------------+

| id | column_json(blob_col) |

+----+-------------------------------+

| 5 | {"size":"XL","color":"black"} |

+----+-------------------------------+

1 row in set (0.00 sec)

删除Key-Value

——总结——

虽然MySQL5.7和MariaDB10.0/10.1版本对于JSON的支持是比较完整的,不过MongoDB的Sharding功能更加好用,个人更倾向于MongoDB。由于笔者的水平有限,编写时间也很仓促,文中难免会出现一些错误或者不准确的地方,不妥之处恳请读者批评指正。

mysql json官方文档,10分钟了解MySQL5.7对原生JSON的支持与用法相关推荐

  1. 状态提升(精读React官方文档—10)

    为什么需要状态提升? 有时候,多个组件需要共享状态,此时需要将共享状态提升到最近的共同父组件中去. 首先创建一个判断水是否沸腾的组件BoilingVerdict celsius 温度作为一个 prop ...

  2. Delphi官方文档

    Delphi官方文档 http://docwiki.embarcadero.com/RADStudio/Tokyo/en/JSON#JSON_Frameworks

  3. Unity 优化翻译官方文档(二) ------ 平台特定覆盖的纹理压缩格式

    官方文档 : https://docs.unity3d.com/Manual/class-TextureImporterOverride.html 虽然Unity支持许多常见的图像格式作为导入纹理的源 ...

  4. mysql5.5配置向导官方文档【中文翻译版】

    这是mysql的官方文档的翻译版本,浏览器翻译的,基本上能看懂.图片看着更加方便.就是看着发虚,可以直接点击图片看大图. MySQL5.5配置向导中文说明

  5. 爆肝1个多月,严选10大热门静态博客框架,官方文档、教学视频、经典案例、热门插件一应俱全,强烈建议收藏

    [文章编号 t001] 凌晨 12 点,我有一位朋友,在电话里,气愤地说他的博客文章又被平台删掉了. 我很好奇为什么,原来是他在某平台发布了一篇技术类原创文章,读者都比较喜欢.后来,他把这篇文章转发到 ...

  6. MySQL MHA高可用架构官方文档全文翻译

    目录 MHA项目官方github地址 关于MHA 概述 主故障切换的难点 现有的解决方案和问题 MHA的架构 MHA的优势 使用案例 其他高可用解决方案和问题 纯手动解决 单主单从 一个主节点.一个备 ...

  7. 《maven官方文档》5分钟开始Maven

    原文地址 前提 你必须明白如何在电脑上安装软件.如果你不知道如何做,请向你学校.办公室里等的人请教下,或者付费给他人让他们解释给你.Maven邮件组不是寻求这个建议的最好地方. 安装 Maven是个J ...

  8. mysql如何降级_降级MySQL(参考MySQL官方文档)

    降级MySQL(参考MySQL官方文档) 介绍降级MySQL安装的步骤. 降级比升级更不常见.降级通常是由于生产系统上发生兼容性或性能问题而执行,并且在测试系统的初始升级验证期间没有发现. 与升级过程 ...

  9. 基于vue的微信小程序开发5分钟上手教程(官方文档转)

    使用手册 mpvue 继承自 Vue.js,其技术规范和语法特点与 Vue.js 保持一致. 注:其实就是官方文档,只是习惯看博文学习才直接copy过来的,详见官方文档 本文档适用于有一定 Vue.j ...

最新文章

  1. 第六课.NLP文本分类任务
  2. ASP.NET清除页面缓存的方法
  3. 数字电路时钟问题——Jitter与Skew区别
  4. 记录一次【模仿真实环境】的内网漫游
  5. 成功解决Fit Failed Warning: Estimator fit failed. The score on this train-test partition for these param
  6. DateTime和字符串转换问题
  7. 编译AVX代码,升级Redhat 5.5 GCC至4.7.1
  8. python json转xml_Python中xml和json格式相互转换操作示例
  9. java看视频可以学会吗,看it教程视频自学Java编程可以学会吗?
  10. python dash html.table_阅读 Python dash 代码的时候有个问题, 那个包的调用有问题?
  11. html按钮调用php函数,如何在html按钮上执行php函数点击
  12. java reader类 实例_java字符流-java writer-java reader-嗨客网
  13. 用游戏编辑器制作MOD脱颖而出
  14. html图片白色背景怎么去掉,怎么把PPT图片的白色背景去掉 PPT去除图片背景颜色技巧...
  15. 你知道台湾Android开发面試題是什么样的吗(附答案解析)
  16. Direct3D 11 Devices 之 Using Direct3D 11 feature data to supplement Direct3D feature levels
  17. AI绘画绘图流量主小程序开发
  18. Python | 人脸识别系统 — 用户操作
  19. 【1945. 字符串转化后的各位数字之和】
  20. PDF转word怎么转换

热门文章

  1. RabbitMQ 高级特性(吐血猝死整理篇)
  2. 全国青少年软件编程等级考试标准 1-4级
  3. 2021年低压电工考试技巧及低压电工复审模拟考试
  4. tiptap - 基于 vue 的优雅流畅的开源富文本编辑器
  5. 新版gsp计算机培训课件,新版gsp《药品经营质量管理规范》释义v0708ppt课件.ppt
  6. 五金模具设计制造完整的流程方式跟方法
  7. 简述:为什么硅胶按键要使用镭雕工艺?
  8. SU插件情报局 | CleanUp3 清理大师
  9. [置顶] 【稀饭】react native 实战系列教程之热更新原理分析与实现
  10. 【Linux】CentOS7 无法打开终端