早在 2003 年 sql 标准就支持了 MERGE 语句,它可以基于源表或者查询结果更新目标表中的数据。MERGE 可以在单个语句中实现 INSERT、UPDATE 以及 DELETE 操作。
目前只有 Oracle 和 sql Server 支持 MERGE 语句,不过即将到来的 Postgresql 15 已经确认会增加 MERGE 语句。在此之前,我们可以使用 INSERT … ON CONFLICT DO …. 语句代替该功能。
Postgresql 官方文档中已经可以查看 MERGE 语句的使用说明,我们这里给出一个简单的示例案例:
create table test (
id int8 generated always as identity,
username text not null unique,
touch_count int4 not null default 0,
primary key (id)
);
merge into test t
using (values ('super')) as i(un)
on t.username = i.un
when matched then
update set touch_count = touch_count + 1
when not matched then
insert (username, touch_count) values (i.un, 1);
MERGE 1
select * from test;
id | username | touch_count
----+----------+-------------
1 | super | 1
(1 row)
如果我们再次执行上面的 MERGE 语句:
merge into test t
using (values ('super')) as i(un)
on t.username = i.un
when matched then
update set touch_count = touch_count + 1
when not matched then
insert (username, 1);
MERGE 1
select * from test;
id | username | touch_count
----+----------+-------------
1 | super | 2
(1 row)
MEREG 语句中的 WHEN 子句可以使用复合条件,而且我们可以指定多个 WHEN 子句。例如,假设我们希望用户最多被操作 3 次,超过次数则会被删除:
merge into test t
using (values ('super')) as i(un)
on t.username = i.un
when matched and touch_count < 3 then
update set touch_count = touch_count + 1
when matched then
delete
when not matched then
insert (username, 1);
MERGE 1
select * from test;
id | username | touch_count
----+----------+-------------
1 | super | 3
(1 row)
merge into test t
using (values ('super')) as i(un)
on t.username = i.un
when matched and touch_count < 3 then
update set touch_count = touch_count + 1
when matched then
delete
when not matched then
insert (username, 1);
MERGE 1
select * from test;
id | username | touch_count
----+----------+-------------
(0 rows)
其中第一个 MERGE 语句将 super 用户的 touch_count 增加为 3。第二个 MERGE 语句发现 touch_count 已经到达 3 次,所以删除了该用户。
MERGE 语句支持普通表、分区表以及继承层次结构,包括列级和行级安全增强,以及行级触发器、语句级触发器和触发器中的过渡表。
MERGE 针对 OLTP 进行了优化,支持参数化语句,对于大规模 ETL、ELT 也很有用。不过,MERGE 并不是为了替代已有的 INSERT、UPDATE 或者 DELETE 语句,因为它存在一些额外的开销。
MERGE 可以用于 PL/pgsql 存储过程。
MERGE 不支持使用可更新视图或者外部表作为目标表,目前也不支持 RETURNING 子句,这些限制可以通过其他方式解决。MERGE 也不支持重写规则。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。