网站首页 > 教程文章 正文
由于工作中使用,研究了merge into 语句是insert 与update语句的结合,可以同时实现update和insert的功能。
一、merge into语句的语法。
MERGE INTO schema. table alias
USING { schema. table | views | query} alias
ON {(condition) }
WHEN MATCHED THEN
UPDATE SET {clause}
WHEN NOT MATCHED THEN
INSERT VALUES {clause};
--解析
INTO 子句
用于指定你所update或者Insert目的表。
USING 子句
用于指定你要update或者Insert的记录的来源,它可能是一个表,视图,子查询。
ON Clause
用于目的表和源表(视图,子查询)的关联,如果匹配(或存在),则更新,否则插入。
merge_update_clause
用于写update语句
merge_insert_clause
用于写insert语句
二、merge 语句的各种用法练习
创建表及插入记录
create table t_B_info_aa
( id varchar2(20),
name varchar2(50),
type varchar2(30),
price number
);
insert into t_B_info_aa values('01','冰箱','家电',2000);
insert into t_B_info_aa values('02','洗衣机','家电',1500);
insert into t_B_info_aa values('03','热水器','家电',1000);
insert into t_B_info_aa values('04','净水机','家电',1450);
insert into t_B_info_aa values('05','燃气灶','家电',800);
insert into t_B_info_aa values('06','太阳能','家电',1200);
insert into t_B_info_aa values('07','西红柿','食品',1.5);
insert into t_B_info_aa values('08','黄瓜','食品',3);
insert into t_B_info_aa values('09','菠菜','食品',4);
insert into t_B_info_aa values('10','香菇','食品',9);
insert into t_B_info_aa values('11','韭菜','食品',2);
insert into t_B_info_aa values('12','白菜','食品',1.2);
insert into t_B_info_aa values('13','芹菜','食品',2.1);
create table t_B_info_bb
( id varchar2(20),
type varchar2(50),
price number
);
insert into t_B_info_bb values('01','家电',2000);
insert into t_B_info_bb values('02','家电',1000);
1) update和insert同时使用
merge into t_B_info_bb b
using t_B_info_aa a --如果是子查询要用括号括起来
on (a.id = b.id and a.type = b.type) --关联条件要用括号括起来
when matched then
update set b.price = a.price --update 后面直接跟set语句
when not matched then
insert (id, type, price) values (a.id, a.type, a.price) --insert 后面不加into
---这条语句根据t_B_info_aa 更新了t_B_info_bb中的一条记录,插入了11条记录
2)只插入不更新
--处理表中数据,使表中的一条数据发生变化,删掉一部分数据。用来验证只插入不更新的功能
update t_B_info_bb b set b.price=1000 where b.id='02';
delete from t_B_info_bb b where b.type='食品';
--只是去掉了when matched then update语句
merge into t_B_info_bb b
using t_B_info_aa a
on (a.id = b.id and a.type = b.type)
when not matched then
insert (id, type, price) values (a.id, a.type, a.price)
3)只更新不插入
--处理表中数据,删掉一部分数据,用来验证只更新不插入
delete from t_B_info_bb b where b.type='食品';
--只更新不插入,只是去掉了when not matched then insert语句
merge into t_B_info_bb b
using t_B_info_aa a
on (a.id = b.id and a.type = b.type)
when matched then
update set b.price = a.price
三、加入限制条件的操作
变更表中数据,便于练习
update t_B_info_bb b
set b.price = 1000
where b.id='02'
delete from t_b_info_bb b where b.id not in ('01','02') and b.type='家电'
update t_B_info_bb b
set b.price = 8
where b.id='10'
delete from t_b_info_bb b where b.id in ('11','12','13') and b.type='食品'
表中数据
执行merge语句:脚本一和脚本二执行结果相同
脚本一: merge into t_B_info_bb b
using t_B_info_aa a
on (a.id = b.id and a.type = b.type)
when matched then
update set b.price = a.price where a.type = '家电'
when not matched then
insert
(id, type, price)
values
(a.id, a.type, a.price) where a.type = '家电';
----------------------
脚本二: merge into t_B_info_bb b
using (select a.id, a.type, a.price
from t_B_info_aa a
where a.type = '家电') a
on (a.id = b.id and a.type = b.type)
when matched then
update set b.price = a.price
when not matched then
insert (id, type, price) values (a.id, a.type, a.price);
结果:
上面两个语句是只对类型是家电的语句进行插入和更新
而脚本三是只对家电进行更新,其余的全部进行插入
merge into t_B_info_bb b
using t_B_info_aa a
on (a.id = b.id and a.type = b.type)
when matched then
update set b.price = a.price
where a.type = '家电'
when not matched then
insert
(id, type, price)
values
(a.id, a.type, a.price);
四、加删除操作
update子句后面可以跟delete子句来去掉一些不需要的行
delete只能和update配合,从而达到删除满足where条件的子句的记录
例句:
merge into t_B_info_bb b
using t_B_info_aa a
on (a.id=b.id and a.type = b.type)
when matched then
update set b.price=a.price
delete where (a.type='食品')
when not matched then
insert values (a.id, a.type, a.price) where a.id = '10'
————————————————
猜你喜欢
- 2025-04-06 超级详细的zabbix环境搭建和测试(看我的教程,大神你也可以)
- 2025-04-06 SQL——UPDATE:更新数据(更新sql语句)
- 2025-04-06 什么是 SQL 事务,如何创建 SQL 事务
- 2025-04-06 Win11学院:如何修复Windows Update服务丢失问题
- 2025-04-06 TiDB 专用语法,优化器注释和表属性
- 2025-04-06 20000 字干货笔记,一天搞定 MySQL
- 2025-04-06 数据库:Mysql中“select ... for update”排他锁分析
- 2025-04-06 【Mybatis实战第4天】三步搞定 Mybatis更新(update)操作
- 2025-04-06 select...for update到底是加了行锁,还是表锁?
- 2025-04-06 超详细的Oracle19c修改数据库用户名教程
- 05-08云虚拟电脑与操作云电脑:相同还是不同?
- 05-08【三.丰.云】免费虚拟主机和免费云服务器,真的不错
- 05-08三丰云免费虚拟主机和云服务器评测
- 05-08阿贝云:免费的虚拟主机和云服务器,让我爱上云计算
- 05-08云服务器与虚拟主机的区别解析(云服务器与虚拟主机的区别解析图)
- 05-08(可以搭建游戏无盘)推荐一个免费云服务器,免费虚拟主机
- 05-08【阿贝云】免费云主机,免费虚拟主机
- 05-08免费云服务器和虚拟主机(免费云服务器和虚拟主机的区别)
- 最近发表
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- md5 sha1 (32)
- mybatis plus page (35)
- semaphore 使用详解 (32)
- update from 语句 (32)
- vue @scroll (38)
- 堆栈区别 (33)
- 在线子域名爆破 (32)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)