你Merge了没有----Merge into的使用详解

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有以下一些改进:
一、UPDATE或INSERT子句是可选的
二、UPDATE和INSERT子句能够加WHERE子句
三、在ON条件中使用常量过滤谓词来insert全部的行到目标表中,不须要链接源表和目标表
四、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是一个很是强大的功能,并且是咱们需求里常常会用到的一个有用的功能,因此咱们必定要好好的学习到。sql

相关文章
相关标签/搜索