oracle中merge into语句详解
ccwgpt 2025-01-05 16:16 96 浏览 0 评论
由于工作中使用,研究了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'
————————————————
相关推荐
- 一个基于.Net Core遵循Clean Architecture原则开源架构
-
今天给大家推荐一个遵循CleanArchitecture原则开源架构。项目简介这是基于Asp.netCore6开发的,遵循CleanArchitecture原则,可以高效、快速地构建基于Ra...
- AI写代码翻车无数次,我发现只要提前做好这3步,bug立减80%
-
写十万行全是bug之后终于找到方法了开发"提示词管理助手"新版本那会儿,我差点被bug整崩溃。刚开始两周,全靠AI改代码架构,结果十万行程序漏洞百出。本来以为AI说没问题就稳了,结果...
- OneCode低代码平台的事件驱动设计:架构解析与实践
-
引言:低代码平台的事件驱动范式在现代软件开发中,事件驱动架构(EDA)已成为构建灵活、松耦合系统的核心范式。OneCode低代码平台通过创新性的注解驱动设计,将事件驱动理念深度融入平台架构,实现了业务...
- 国内大厂AI插件评测:根据UI图生成Vue前端代码
-
在IDEA中安装大厂的AI插件,打开ruoyi增强项目:yudao-ui-admin-vue31.CodeBuddy插件登录腾讯的CodeBuddy后,大模型选择deepseek-v3,输入提示语:...
- AI+低代码技术揭秘(二):核心架构
-
本文档介绍了为VTJ低代码平台提供支持的基本架构组件,包括Engine编排层、Provider服务系统、数据模型和代码生成管道。有关UI组件库和widget系统的信息,请参阅UI...
- GitDiagram用AI把代码库变成可视化架构图
-
这是一个名为gitdiagram的开源工具,可将GitHub仓库实时转换为交互式架构图,帮助开发者快速理解代码结构。核心功能一键可视化:替换GitHubURL中的"hub...
- 30天自制操作系统:第六天:代码架构整理与中断处理
-
1.拆开bootpack.c文件。根据设计模式将对应的功能封装成独立的文件。2.初始化pic:pic(可编程中断控制器):在设计上,cpu单独只能处理一个中断。而pic是将8个中断信号集合成一个中断...
- AI写代码越帮越忙?2025年研究揭露惊人真相
-
近年来,AI工具如雨后春笋般涌现,许多人开始幻想程序员的未来就是“对着AI说几句话”,就能轻松写出完美的代码。然而,2025年的一项最新研究却颠覆了这一期待,揭示了一个令人意外的结果。研究邀请了16位...
- 一键理解开源项目:两个自动生成GitHub代码架构图与说明书工具
-
一、GitDiagram可以一键生成github代码仓库的架构图如果想要可视化github开源项目:https://github.com/luler/reflex_ai_fast,也可以直接把域名替换...
- 5分钟掌握 c# 网络通讯架构及代码示例
-
以下是C#网络通讯架构的核心要点及代码示例,按协议类型分类整理:一、TCP协议(可靠连接)1.同步通信//服务器端usingSystem.Net.Sockets;usingTcpListene...
- 从复杂到优雅:用建造者和责任链重塑代码架构
-
引用设计模式是软件开发中的重要工具,它为解决常见问题提供了标准化的解决方案,提高了代码的可维护性和可扩展性,提升了开发效率,促进了团队协作,提高了软件质量,并帮助开发者更好地适应需求变化。通过学习和应...
- 低代码开发当道,我还需要学习LangChain这些框架吗?| IT杂谈
-
专注LLM深度应用,关注我不迷路前两天有位兄弟问了个问题:当然我很能理解这位朋友的担忧:期望效率最大化,时间用在刀刃上,“不要重新发明轮子”嘛。铺天盖地的AI信息轰炸与概念炒作,很容易让人浮躁与迷茫。...
- 框架设计并不是简单粗暴地写代码,而是要先弄清逻辑
-
3.框架设计3.框架设计本节我们要开发一个UI框架,底层以白鹭引擎为例。框架设计的第一步并不是直接撸代码,而是先想清楚设计思想,抽象。一个一个的UI窗口是独立的吗?不是的,...
- 大佬用 Avalonia 框架开发的 C# 代码 IDE
-
AvalonStudioAvalonStudio是一个开源的跨平台的开发编辑器(IDE),AvalonStudio的目标是成为一个功能齐全,并且可以让开发者快速使用的IDE,提高开发的生产力。A...
- 轻量级框架Lagent 仅需20行代码即可构建自己的智能代理
-
站长之家(ChinaZ.com)8月30日消息:Lagent是一个专注于基于LLM模型的代理开发的轻量级框架。它的设计旨在简化和提高这种模型下代理的开发效率。LLM模型是一种强大的工具,可以...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- 框架图 (58)
- flask框架 (53)
- quartz框架 (51)
- abp框架 (47)
- springmvc框架 (49)
- 分布式事务框架 (65)
- scrapy框架 (56)
- shiro框架 (61)
- 定时任务框架 (56)
- java日志框架 (61)
- mfc框架 (52)
- abb框架断路器 (48)
- beego框架 (52)
- java框架spring (58)
- grpc框架 (65)
- tornado框架 (48)
- 前端框架bootstrap (54)
- orm框架有哪些 (51)
- 知识框架图 (52)
- ppt框架 (55)
- 框架图模板 (59)
- 内联框架 (52)
- cad怎么画框架 (58)
- ssm框架实现登录注册 (49)
- oracle字符串长度 (48)