设为首页 收藏本站
查看: 895|回复: 0

[经验分享] oracle merge-oracle的merger的用法

[复制链接]

尚未签到

发表于 2018-9-25 10:51:17 | 显示全部楼层 |阅读模式
  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的用法
12345merge intoproducts p using newproducts np on(p.product_id = np.product_id)whenmatched thenupdatesetp.product_name = np.product_namewhennotmatched theninsertvalues(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换成
12345merge intoproducts p using (select* fromnewproducts) np on(p.product_id = np.product_id)whenmatched thenupdatesetp.product_name = np.product_namewhennotmatched theninsertvalues(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
123merge intoproducts p using newproducts np on(p.product_id = np.product_id)whenmatched thenupdatesetp.product_name = np.product_name  这里,如果匹配就更新,不存在就不管了。
  2. UPDATE和INSERT子句可以加WHERE子句
  这也是一个功能性的改进,能够符合我们更多的需求,这个where的作用很明显是一个过滤的条件,是我们加入一些额外的条件,对只对满足where条件的进行更新和insert
123merge intoproducts p using (select* fromnewproducts) np on(p.product_id = np.product_id)whenmatched thenupdatesetp.product_name = np.product_name wherenp.product_name like'OL%'  这里表示只是对product_name开头是'OL'的匹配上的进行update,如果开头不是'OL'的就是匹配了也不做什么事情,insert里也可以加入where
  比如
12345merge intoproducts p using (select* fromnewproducts) np on(p.product_id = np.product_id)whenmatched thenupdatesetp.product_name = np.product_name wherenp.product_name like'OL%'whennotmatched theninsertvalues(np.product_id, np.product_name, np.category) wherenp.product_name like'OL%'  这里注意比较一下,他们返回的结果行数,是有着差异的。
  3. 在ON条件中使用常量过滤谓词来insert所有的行到目标表中,不需要连接源表和目标表
12345merge intoproducts p using (select* fromnewproducts) np on(1=0)whenmatched thenupdatesetp.product_name = np.product_namewhennotmatched theninsertvalues(np.product_id, np.product_name, np.category)  个人觉得这个功能没有太大的意义,我们的insert into本身就支持这样的功能,没有必要使用merge
  4. UPDATE子句后面可以跟DELETE子句来去除一些不需要的行
  delete只能和update配合,从而达到删除满足where条件的子句的纪录
12345merge intoproducts p using (select* fromnewproducts) np on(p.product_id = np.product_id)whenmatched thenupdatesetp.product_name = np.product_name deletewherep.product_id = np.product_id wherenp.product_name like'OL%'whennotmatched theninsertvalues(np.product_id, np.product_name, np.category)  这里我们达到的目的就是 会把匹配的记录的prodcut_name更新到product里,并且把product_name开头为OL的删除掉。
  merge into也是一个dml语句,和其他的dml语句一样需要通过rollback和commit 结束事务。
  Merge是一个非常强大的功能,而且是我们需求里经常会用到的一个有用的功能,所以我们一定要好好的学习到。


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-601503-1-1.html 上篇帖子: oracle的优化 下篇帖子: Oracle Dimension-oracle之路
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表