百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术教程 > 正文

oracle中merge into语句详解(oracle merge into语法)

mhr18 2024-09-27 12:21 21 浏览 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'

————————————————

相关推荐

甲骨文签署多项大型云协议,其一未来可贡献超300亿美元年收入

IT之家7月1日消息,根据甲骨文Oracle当地时间6月30日向美国证券交易委员会(SEC)递交的FORM8-K文件,该企业在始于2025年6月1日的202...

甲骨文获TEMU巨额合同,后者大部分基础设施将迁移至Oracle云

IT之家6月23日消息,Oracle甲骨文创始人、董事长兼首席技术官LarryEllison(拉里埃里森)在本月早些时候的2025财年第四财季和全财年财报电话会议上表示,Oracle...

Spring Boot 自定义数据源设置,这些坑你踩过吗?

你在使用SpringBoot进行后端开发的过程中,是不是也遇到过这样的问题:项目上线后,数据库连接总是不稳定,偶尔还会出现数据读取缓慢的情况,严重影响了用户体验。经过排查,发现很大一部分原因竟然...

一个开箱即用的代码生成器(一个开箱即用的代码生成器是什么)

今天给大家推荐一个好用的代码生成器,名为renren-generator,该项目附带前端页面,可以很方便的选择我们所需要生成代码的表。首先我们通过git工具克隆下来代码(地址见文末),导入idea。...

低代码建模平台-数据挖掘平台(低代码平台的实现方式)

现在来看一下数据连接。·这里是管理数据连接的空间,点击这里可以新增一个数据连接。·输入连接名称,然后输入url,是通过gdbc的方式去连接的数据库,目前是支持mysql、oracle以及国产数据库达梦...

navicat 17.2.7连接oracle数据库提示加载oracle库失败

系统:macOS15.5navicat版本:navicatpremiumlite17.2.7连接oracle测试报错:加载oracle库失败【解决办法】:放达里面找到程序,显示简介里面勾选“使...

开源“Windows”ReactOS更新:支持全屏应用

IT之家6月17日消息,ReactOS团队昨日(6月16日)在X平台发布系列推文,公布了该系统的最新进展,包括升级Explorer组件,支持全屏应用,从Wine项目引入了...

SSL 推出采用全模拟内置混音技术的模拟调音台Oracle

英国调音台传奇品牌SolidStateLogic宣布推出Oracle——一款采用全模拟内置混音技术的调音台,在紧凑的AWS尺寸机箱内集成了大型调音台的功能。该调音台提供24输入和...

47道网络工程师常见面试题,看看有没有你不会的!

你们好,我的网工朋友。网络工程师面试的时候,都会被问到什么?这个问题其实很泛,一般来说,你肯定要先看明白岗位需求写的是什么。基本上都是围绕公司需要的业务去问的。但不可否认的是,那些最基础的概念,多少也...

汉得信息:发布EBS系统安装启用JWS的高效解决方案

e公司讯,从汉得信息获悉,近日,微软官方宣布InternetExplorer桌面应用程序将于2022年6月15日正式停用。目前大部分客户都是使用IE浏览器打开EBS的Form界面,IE停用后,只能使...

36.9K star ! 推荐一个酷炫低代码开发平台!功能太强!

前言最近在逛github,看看能不能搜罗到一些对自己有帮助的开源软件。不经意间看到一个高star的java开源项目:jeecg-boot。进入在线演示版一看,感叹实在是太牛了!此开源项目不管是给来学习...

Linux新手入门系列:Linux下jdk安装配置

本系列文章是把作者刚接触和学习Linux时候的实操记录分享出来,内容主要包括Linux入门的一些理论概念知识、Web程序、mysql数据库的简单安装部署,希望能够帮到一些初学者,少走一些弯路。注意:L...

手把手教你在嵌入式设备中使用SQLite3

摘要:数据库是用来存储和管理数据的专用软件,使得管理数据更加安全,方便和高效。数据库对数据的管理的基本单位是表(table),在嵌入式linux中有时候它也需要用到数据库,听起来好难,其实就是几个函数...

JAVA语言基础(java语言基础知识)

一、计算机的基本概念什么是计算机?计算机(Computer)全称:电子计算机,俗称电脑。是一种能够按照程序运行、自动高速处理海量数据的现代化智能电子设备。由硬件和软件组成、没有安装过任何软件的计算机称...

再见 Navicat!一款开源的 Web 数据库管理工具!

大家好,我是Java陈序员。在日常的开发工作中,常常需要与各种数据库打交道。而为了提高工作效率,常常会使用一些可视化工具进行操作数据库。今天,给大家介绍一款开源的数据库管理工具,无需下载安装软件,基...

取消回复欢迎 发表评论: