KunlunBase对MySQL私有DML语法的支持

前言

为了让MySQL的应用更为便捷地迁移到KunlunBase,我们做了很多兼容MySQL的工作。

本篇章主要介绍KunlunBase现在已经支持的MySQL常用的私有DML语法,以及这些语法与原生MySQL的差异。

一、兼容MySQL的insert ignore语法

功能: 忽略违背唯一约束的新元组。

示例:

postgres -> create table t1(a int primary key, b int not null unique);
CREATE TABLE
# 违背唯一约束,不进行插入
postgres -> insert ignore into t1(a,b) values (4,4);
INSERT 0 1
postgres -> insert ignore into t1(a,b) values (4,4);
INSERT 0 0

和原生MySQL的差异:

  • 只忽略唯一性约束,如果违背其他约束(例如分区约束、非null约束),则报错。

例如:

postgres -> insert ignore into t1(a,b) values (4,NULL);
ERROR:  null value in column "b" violates not-null constraint
DETAIL:  Failing row contains (4, null)

二、兼容MySQL的INSERT…ONDUPLICATE KEY UPDATE…语法

功能: 插入数据;如果违背了某个唯一约束,则转变为更新操作,对其中一个冲突的元组进行更新。

示例:

postgres -> create table t1 (a int primary key, b int not null unique);
CREATE TABLE
postgres -> insert into t1 values(3,3), (4,4);
INSERT 0 2


# 插入的数据和已有的两个元组都冲突,但只更新了其中一个元组(3,3)
postgres -> insert into t1 values(3,4) on duplicate key update b=2;
INSERT 0 2
postgres -> select * from t1;
 a | b
---+---
 3 | 2
 4 | 4

和原生MySQL的差异:

  • 暂不支持在ON DUPLICATE KEY UPDATE子句中使用VALUES()函数来引用新值,可以使用excluded虚拟表来代替。

例如:

postgres -> INSERT INTO t1 VALUES(3,0) ON DUPLICATE KEY UPDATE b = excluded.b;
INSERT 0 2
postgres -> select * from t1;
 a | b
---+---
 3 | 0
 4 | 4
(2rows)
postgres -> INSERT INTO t1 VALUES(3,0) ON DUPLICATE KEY UPDATE b=VALUES(b);
ERROR:  syntax error at or near "VALUES"
  • 往临时表批量写入多个新元组时,如果新元组之间存在唯一性冲突,则会报错(根本原因是临时表存在于计算节点,使用的不是innodb引擎)。

例如:

postgres -> create temp table t1(a int primary key, b int not null unique);
CREATE TABLE
postgres -> INSERT INTO t1 VALUES(5,5), (5,6) ON DUPLICATE KEY UPDATE b = excluded.b;
ERROR:  ON CONFLICT DO UPDATE command cannot affect row a secondtime
HINT:  Ensure that norows proposed for insertion within the same command have duplicate constrained values.
postgres ->
  • 临时表返回的影响行数的差异。即使更新前后的值相同,临时表返回的影响行数仍然大于0。

例如:

postgres -> create temp table t1(a int primary key, b int not null unique);
CREATE TABLE
postgres -> INSERT INTO t1 VALUES(5,5) ON DUPLICATE KEY UPDATE b=excluded.b;
INSERT 0 1
postgres -> INSERT INTO t1 VALUES(5,5) ON DUPLICATE KEY UPDATE b=excluded.b;
INSERT 0 1

三、兼容mysql的replace into语法

功能: 插入元组;如果存在冲突的旧元组,则删除所有与之冲突的旧元组。

示例:

postgres -> create table t1(a int primary key, b int not null unique);
CREATE TABLE
postgres -> insert into t1 values(3,3),(4,4);
INSERT 0 2

postgres -> replace into t1 values(3,4);
INSERT 0 3
postgres -> select * from t1;
 a | b
---+---
 3 | 4
(1row)

和原生MySQL的差异:

  • 往临时表批量写入多个新元组时,如果新元组之间存在唯一性冲突,则会报错(根本原因是临时表存在于计算节点,使用的不是innodb引擎)。

例如:

postgres -> create table t1(a int primary key, b int not null unique);
CREATE TABLE
postgres -> replace into t1 values(1,1),(1,2);
INSERT 0 3

postgres -> create temp table t2(a int primary key,b int not null unique);
CREATE TABLE
postgres -> replace into t2 values(1,1),(1,2);
ERROR:  REPLACEINTO command cannot affect row a secondtime
HINT:  Ensure that norows proposed for insertion within the same command have duplicate constrained values.

四、兼容MySQL的update/delete…order by…limit… 语法

功能: 指定更新/删除的元组的顺序和数量。

示例:

postgres -> create table t1 (a int primary key, b int);
CREATE TABLE
postgres -> insert into t1 select generate_series(1,100),generate_series(1,100);
INSERT 0 100

# 对非分区表的有序更新
postgres -> update t1 set b=b+1 order by a desc limit 4 returning*;
  a  |  b
-----+-----
 100 | 101
  99 | 100
  98 |  99
  97 |  98
(4rows)

UPDATE 4

postgres -> drop table t1;
DROP TABLE
postgres -> CREATE TABLE t1 (A INT PRIMARY KEY,B INT) PARTITION BY RANGE(a);
CREATE TABLE
postgres -> CREATE TABLE t1p1 PARTITION OF t1 FOR VALUES FROM (0) TO (100);
CREATE TABLE
postgres -> CREATE TABLE t1p2 PARTITION OF t1 FOR VALUES FROM (100) TO (200);
CREATE TABLE
postgres -> insert into t1 select generate_series(0,199);
INSERT 0 200

# 指定分区表删除的总量
postgres -> delete from t1  limit 4 returning *;
 a | b
---+---
 0 |
 1 |
 2 |
 3 |
(4rows)

DELETE 4

和原生MySQL的差异:

  • 暂不支持指定分区表的更新/删除的顺序(注意:临时表的分区表已经支持)。 当然,实际使用中需要严格规定更新/删除顺序的场景是极少的,这一限制并不会对KunlunBase的用户造成困扰。

例如:

postgres -> CREATE TABLE t1 (A INT PRIMARY KEY, B INT) PARTITION BY RANGE(a);
CREATE TABLE
postgres -> CREATE TABLE t1p1 PARTITION OF t1 FOR VALUESFROM (0) TO (100);
CREATE TABLE
postgres -> CREATE TABLE t1p2 PARTITION OF t1 FORVALUESFROM (100) TO (200);
CREATE TABLE

# 不能指定分区表删除顺序
postgres -> delete from t1 order by a limit 4 returning *;
ERROR:  Kunlun-db: Cannot push down plan
postgres ->

END

昆仑数据库是一个HTAP NewSQL分布式数据库管理系统,可以满足用户对海量关系数据的存储管理和利用的全方位需求。
应用开发者和DBA的使用昆仑数据库的体验与单机MySQL和单机PostgreSQL几乎完全相同,因为首先昆仑数据库支持PostgreSQL和MySQL双协议,支持标准SQL:2011的
DML 语法和功能以及PostgreSQL和MySQL对标准
SQL的扩展。同时,昆仑数据库集群支持水平弹性扩容,数据自动拆分,分布式事务处理和分布式查询处理,健壮的容错容灾能力,完善直观的监测分析告警能力,集群数据备份和恢复等
常用的DBA 数据管理和操作。所有这些功能无需任何应用系统侧的编码工作,也无需DBA人工介入,不停服不影响业务正常运行。
昆仑数据库具备全面的OLAP
数据分析能力,通过了TPC-H和TPC-DS标准测试集,可以实时分析最新的业务数据,帮助用户发掘出数据的价值。昆仑数据库支持公有云和私有云环境的部署,可以与docker,k8s等云基础设施无缝协作,可以轻松搭建云数据库服务。
请访问http://www.kunlunbase.com获取更多信息并且下载昆仑数据库软件、文档和资料。
KunlunBase项目已开源
【GitHub:】 https://github.com/zettadb
【Gitee:】https://gitee.com/zettadb

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值