Merge into使用详解( 同时执行inserts和updates操作 )
Merge是一个非常有用的功能,类似于MySQL里的insert into on duplicate key.
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是一个非常强大的功能,而且是我们需求里经常会用到的一个有用的功能,所以我们一定要好好的学习到。
Merge into使用详解( 同时执行inserts和updates操作 )的更多相关文章
- Oracle Merge Into 用法详解
原文:http://blog.csdn.net/EdgenHuang/article/details/3587912 Oracle9i引入了MERGE命令,你能够在一个SQL语句中对一个表同时执行in ...
- JS006. 详解自执行函数原理与数据类型的快速转换 (声明语句、表达式、运算符剖析)
今天的主角: Operator Description 一元正值符 " + "(MDN) 一元运算符, 如果操作数在之前不是number,试图将其转换为number. 圆括号运算符 ...
- SQLServer中merge函数用法详解
http://www.jb51.net/article/75302.htm Merge关键字是一个神奇的DML关键字.它在SQL Server 2008被引入,它能将Insert,Update,Del ...
- UEFI启动视频详解:启动分析+N项操作实例
============================================================= ※※※※最给力的视频解说※※※※ 2011hiboy全部共享资料:立刻去 ...
- makefile详解 嵌套执行make,定义命令包
嵌套执行make 在一些大的工程中,我们会把我们不同模块或是不同功能的源文件放在不同的目录中,我们可以在每个目录中都书写一个该目录的Makefile,这有利于让我们的Makefile变得更加地简洁,而 ...
- javascript模块化编程-详解立即执行函数表达式IIFE
一.IIFE解释 全拼Imdiately Invoked Function Expression,立即执行的函数表达式. 像如下的代码所示,就是一个匿名立即执行函数: (function(windo ...
- 详解PHP执行定时任务的实现思路
PHP本身是没有定时功能的,PHP也不能多线程.PHP的定时任务功能必须通过和其他工具结合才能实现,例如WordPress内置了wp-cron的功能,很厉害. 一.Linux服务器上使用CronTab ...
- svn merge和branch 详解
1.本地Repository的创建 repository的创建很简单,假设我要在D:\TortoiseSVN\TestRepository目录中创建repository,只需右键TestReposit ...
- 16、cgminer学习之:popen函数和system函数详解(执行系统命令)
1.popen函数我们先用man指令查一下popen函数: 函数说明: (1)popen()会调用fork()产生子进程,然后从子进程中调用/bin/sh -c来执行参数command的指令. (2) ...
随机推荐
- Visual Studio 连接 SQL Server 的connectionStringz和
近期C#和数据结构的课程设计多次用到了C#中连接SQL Server数据库的问题,当中涉及到数据库文件的附加和连接问题. 当中最烦人的就是 SqlConnection(String connStr) ...
- 【Python基础】之异常
一.常见异常 try: open('abc.txt','r') except FileNotFoundError: print('异常啦!') 输出结果: ======= 异常啦! 我们通过 open ...
- mongo的时间类型,erlang中对其的处理
需求:要想在一个调度中,从mongo中查出大于一个时间戳的所有的数据总和. 这个需求很简单,一个是scheduler,还有另一个就是查出来大于某个时间戳的总和,比如大于每天0点时间点的和. 需要注意的 ...
- 知识复习(LDT+TSS+GATE+INTERRUPT)
[1]README 1.0)由于实现进程的切换任务,其功能涉及到 LDT + TSS +GATE + INTERRUPT:下面我们对这些内容进行复习: 1.1) source code from or ...
- T-SQL高级查询语句(父子查询)
T-SQL高级查询语句 高级查询 1.连接查询,对结果集列的扩展 select * from info select * from info,nation #形成笛卡尔积 select * from ...
- 【BZOJ2510】弱题 期望DP+循环矩阵乘法
[BZOJ2510]弱题 Description 有M个球,一开始每个球均有一个初始标号,标号范围为1-N且为整数,标号为i的球有ai个,并保证Σai = M. 每次操作等概率取出一个球(即取出每个球 ...
- AWS:1.相关概念、创建云主机的过程
概念 EC2是弹性的云计算 云主机 也即虚拟机,由分配的CPU.内存.网络和磁盘等资源组成 好处:维护成本低(主机替换).环境升级成本低 AMI:映像 创建云主机的蓝图,指定初始状态1 预装什么操作系 ...
- mysql insert返回主键
使用mybatis的话,很方便. 使用useGeneratedKeys和keyProperty,keyProperty是插入的java对象的属性名,不是表的字段名. 这样,在插入该条记录之后,生成的主 ...
- new() 和 make() 的区别
https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/07.2.md
- X86汇编 BT
位操作指令位操作指令包括位测试和位扫描指令,可以直接对一个二进制位进行测试,设置和扫描. 1位测试和设置指令 格式:BT DEST,SRC BTC DEST,SRC BTR DEST,SRC BTS ...