Oracle中MERGE语句的使用
Oracle在9i引入了merge命令,
通过这个merge你能够在一个SQL语句中对一个表同时执行inserts和updates操作. 当然是update还是insert是依据于你的指定的条件判断的,Merge into可以实现用B表来更新A表数据,如果A表中没有,则把B表的数据插入A表. MERGE命令从一个或多个数据源中选择行来updating或inserting到一个或多个表
语法如下
MERGE INTO [your table-name] [rename your table here]
USING ( [write your query here] )[rename your query-sql and using just like a table]
ON ([conditional expression here] AND [...]...)
WHEN MATHED THEN [here you can execute some update sql or something else ]
WHEN NOT MATHED THEN [execute something else here ! ]
我们先看看一个简单的例子,来介绍一个merge into的用法
merge into products p using newproducts np on (p.product_id = np.product_id)
when matched then
update set p.product_name = np.product_name
when not matched then
insert values(np.product_id, np.product_name, np.category)
在这个例子里。前面的merger into products using newproducts 表示的用newproducts表来merge到products表,merge的匹配关系就是on后面的条件子句的内容,这里根据两个表的product_id来进行匹配,那么匹配上了我们的操作是就是when matched then的子句里的动作了,这里的动作是update set p.product_name = np.product_name, 很显然就是把newproduct里的内容,赋值到product的product_name里。如果没有匹配上则insert这样的一条语句进去。 大家看看这个merget inot的用法是不是一目了然了呀。这里merger的功能,好比比较,然后选择更新或者是插入,是一系列的组合拳,在做merge的时候,这样同样的情况下,merge的性能是优于同等功能的update/insert语句的。有人曾经分析merge是批量处理对性能贡献很大,个人觉得这个是没有考据的。
我们也可以在using后面使用视图或者子查询。比如我们把newproducts换成
merge into products p using (select * from newproducts) np on (p.product_id = np.product_id)
when matched then
update set p.product_name = np.product_name
when not matched then
insert values(np.product_id, np.product_name, np.category)
也是可以的。
在Oracle 10g中MERGE有如下一些改进:
1、UPDATE或INSERT子句是可选的
2、UPDATE和INSERT子句可以加WHERE子句
3、在ON条件中使用常量过滤谓词来insert所有的行到目标表中,不需要连接源表和目标表
4、UPDATE子句后面可以跟DELETE子句来去除一些不需要的行
我们通过实例来一一看看如上的新特性
1. UPDATE或INSERT子句是可选的
在9i里由于必须insert into和update都要存在,也就是不是update就是insert,不支持单一的操作,虽然还是可以曲线救国,呵呵 但是有些过于强势了。而10g里就是可选了,能符合我们更多的需求了
比如上面的句子
我们可以只存在update或者insert
merge into products p using newproducts np on (p.product_id = np.product_id)
when matched then
update set p.product_name = np.product_name
这里,如果匹配就更新,不存在就不管了。
2. UPDATE和INSERT子句可以加WHERE子句
这也是一个功能性的改进,能够符合我们更多的需求,这个where的作用很明显是一个过滤的条件,是我们加入一些额外的条件,对只对满足where条件的进行更新和insert
merge into products p using (select * from newproducts) np on (p.product_id = np.product_id)
when matched then
update set p.product_name = np.product_name where np.product_name like 'OL%'
这里表示只是对product_name开头是'OL'的匹配上的进行update,如果开头不是'OL'的就是匹配了也不做什么事情,insert里也可以加入where
比如
merge into products p using (select * from newproducts) np on (p.product_id = np.product_id)
when matched then
update set p.product_name = np.product_name where np.product_name like 'OL%'
when not matched then
insert values(np.product_id, np.product_name, np.category) where np.product_name like 'OL%'
这里注意比较一下,他们返回的结果行数,是有着差异的。
3. 在ON条件中使用常量过滤谓词来insert所有的行到目标表中,不需要连接源表和目标表
merge into products p using (select * from newproducts) np on (1=0)
when matched then
update set p.product_name = np.product_name
when not matched then
insert values(np.product_id, np.product_name, np.category)
个人觉得这个功能没有太大的意义,我们的insert into本身就支持这样的功能,没有必要使用merge
4. UPDATE子句后面可以跟DELETE子句来去除一些不需要的行
delete只能和update配合,从而达到删除满足where条件的子句的纪录
merge into products p using (select * from newproducts) np on (p.product_id = np.product_id)
when matched then
update set p.product_name = np.product_name delete where p.product_id = np.product_id where np.product_name like 'OL%'
when not matched then
insert values(np.product_id, np.product_name, np.category)
这里我们达到的目的就是 会把匹配的记录的prodcut_name更新到product里,并且把product_name开头为OL的删除掉。
merge into也是一个dml语句,和其他的dml语句一样需要通过rollback和commit 结束事务。
注意merge into的几点注意事项:
实例如下:
merge into product p
using newProduct np
on(p.product_id = np.product_id)
when matched then
update
set p.product_name = np.product_name where p.product_category = 'category3'
delete where p.product_category = 'category3'
when not matched then
insert
values(np.product_id,np.product_name,np.product_category) where np.product_category = 'category2';
一、在on子句中用于判断条件的列属性不能用在update语句中作为更新的列:product_id列不能用在update语句中来更新product_id 列。如果update语句换成
update
set p.product_name = np.product_name where p.product_category = 'category3',p.product_id = np.product_id就会报错,提示不能更新在on子句中引用的列product_id.
二、update 和insert语句中都可以使用where子句,并且where子句的限制条件即可以是限制到product表又可以限制到newProduct表
三、delete子句只能跟update语句一起使用不能添加到insert语句中,另外要想delete子句得到执行那么update中的set语句后面的更新必须要得到执行才能执行delete子句
Merge是一个非常强大的功能,而且是我们需求里经常会用到的一个有用的功能,所以我们一定要好好的学习。
原文:http://blog.csdn.net/inthirties/article/details/4731930
Oracle中MERGE语句的使用的更多相关文章
- Oracle 中MERGE语句的用法
原文章出处(http://blog.csdn.net/lichkui/article/details/4306299) MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语 ...
- Oracle中merge into的使用
http://blog.csdn.net/yuzhic/article/details/1896878 http://blog.csdn.net/macle2010/article/details/5 ...
- Oracle中merge into的使用 (转)
该命令使用一条语句从一个或者多个数据源中完成对表的更新和插入数据. ORACLE 9i 中,使用此命令必须同时指定UPDATE 和INSERT 关键词,ORACLE 10g 做了如下改动. 1.ins ...
- Oracle中merge into的使用 (转)
http://blog.csdn.net/yuzhic/article/details/1896878 http://blog.csdn.net/macle2010/article/details/5 ...
- Oracle中SQL语句分类
Oracle中SQL语句分类如下:1.DML语句 insert/delete/update/select/merge/explan plan/lock table2.DDL语句 create/atlt ...
- 如何理解T-SQL中Merge语句(二)
写在前面的话:上一篇写了如何理解T-SQL中Merge语句,基本把Merge语句要讲的给讲了,在文章的后面,抛出了几个结,当时没有想明白怎么去用文字表达,这一篇就来解答一下这几个结,又是一篇“天马行空 ...
- 如何理解T-SQL中Merge语句
写在前面的话:之前看过Merge语句,感觉没什么用,完全可以用其他的方式来替代,最近又看了看Merge语句,确实挺好用,可以少写很多代码,看起来也很紧凑,当然也有别的优点. ====正文开始===== ...
- oracle中sql语句的优化
oracle中sql语句的优化 一.执行顺序及优化细则 1.表名顺序优化 (1) 基础表放下面,当两表进行关联时数据量少的表的表名放右边表或视图: Student_info (30000条数据)D ...
- ORACLE中查询语句的执行顺及where部分条件执行顺序测试
Oracle中的一些查询语句及其执行顺序 原文地址:https://www.cnblogs.com/likeju/p/5039115.html 查询条件: 1)LIKE:模糊查询,需要借助两个通配符, ...
随机推荐
- linux tar使用
Linux tar指令简单使用 -c:创建包,-x:解压或解包(-c和-x可理解为互逆运算),-t:查看包 -f:后加处理文件,必须放在参数组合的最后一位(tar -cf a.tar 1.tx ...
- [linux] linux知识积累(不断更新中…)
一.vim知识 f(find)命令也可以用于移动,fx将找到光标后第一个为x的字符,3fd将找到第三个为d的字符. D 删除当前字符至行尾.D=d$ :split或new 打开一个新窗口,光标停在顶层 ...
- Java学习笔记--AWT事件处理
1.事件模型 在整个事件触发和相应的过程中,主要涉及一下3类对象 (1) 事件源 : 引起时间的GUI对象,如各类组件(Button,Label,TextField),容器组件(Frame,panel ...
- VLSI和ASIC的区别(转)
VLSI和ASIC是不同的两个概念 VLSI(Very Large Scale Integrate circuit)是指集成电路的规模,有时也指制造集成电路所使用的工艺,VLSI工艺一般都在1um以下 ...
- iPhone 和Android应用,特殊的链接:打电话,短信,email;
http://ice-k.iteye.com/blog/1426526 下面的这篇文章主要是说,网页中的链接如何写,可以激活电话的功能. 例如,页面中展示的是一个电话号码,当用户在手机浏览器里面点击这 ...
- cf446A DZY Loves Sequences
A. DZY Loves Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Cookie知识点小结
问题是什么?有哪些技术?如何解决? 1. Cookie 1)完成回话跟踪的一种机制:采用的是在客户端保存Http状态信息的方案 2)Cookie是在浏览器访问WEB服务器的某个资源时,由WEB服务器在 ...
- Kafka测试
准备工作 硬件:笔记本,windows10系统4核8G内存 软件:接口测试工具,以及kafka自带测试工具 影响测试结果配置分析 Borker num.network.thread=3 用于接收并处理 ...
- vs2010 正式版官方下载地址
北京时间2010年4月12日12:00,微软Visual Studio 2010 正式版提供官方下载,眼下有三个版本号,Professional/Premium/Ultimate. 注意原页面的链接R ...
- Android窗口管理服务WindowManagerService对输入法窗口(Input Method Window)的管理分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8526644 在Android系统中,输入法窗口 ...