mysql的replace into类似于oracle的merge sql语句
2016-01-13 13:52
459 查看
mysql的replace into命令
replace into的用法,真的很好用,是insert into的增强版。在向表中插入数据时,我们经常会遇到这样的情况:1、首先判断数据是否存在;2、如果不存在,则插入;3、如果存在,则更新。在SQL Server中可以这样处理:
if not exists (select 1 from t where id = 1)? insert into t(id, update_time) values(1, getdate()) else update t set update_time = getdate() where id = 1
那么 MySQL 中如何实现这样的逻辑呢?MySQL 中有更简单的方法: replace into
replace into t(id, update_time) values(1, now()); 或 replace into t(id, update_time) select 1, now();
replace into 跟 insert 功能类似,不同点在于:replace into 首先尝试插入数据到表中, 1. 如果发现表中已经有此行数据(根据主键或者唯一索引判断)则先删除此行数据,然后插入新的数据。 2. 否则,直接插入新数据。
要注意的是:插入数据的表必须有主键或者是唯一索引!否则的话,replace into 会直接插入数据,这将导致表中出现重复的数据。
MySQL replace into 有三种形式:
1. replace into tbl_name(col_name, ...) values(...) 2. replace into tbl_name(col_name, ...) select ... 3. replace into tbl_name set col_name=value, ...
第一种形式类似于insert into的用法,
第二种replace select的用法也类似于insert select,这种用法并不一定要求列名匹配,事实上,MYSQL甚至不关心select返回的列名,它需要的是列的位置。例如,replace into tb1( name, title, mood) select rname, rtitle, rmood from tb2;?这个例子使用replace into从?tb2中将所有数据导入tb1中。
第三种replace set用法类似于update set用法,使用一个例如“SET col_name = col_name + 1”的赋值,则对位于右侧的列名称的引用会被作为DEFAULT(col_name)处理。因此,该赋值相当于SET col_name = DEFAULT(col_name) + 1。
前两种形式用的多些。其中 “into” 关键字可以省略,不过最好加上 “into”,这样意思更加直观。另外,对于那些没有给予值的列,MySQL 将自动为这些列赋上默认值。
Oracle的MERGE INTO命令
通过这个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子句来去除一些不需要的行
我们通过实例来一一看看如上的新特性
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
这里,如果匹配就更新,不存在就不管了。
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%'
这里注意比较一下,他们返回的结果行数,是有着差异的。
在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
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 结束事务。
相关文章推荐
- MySQL中的integer 数据类型
- MySQL存储过程
- 基于 Red Hat 的发行版 Oracle Linux 正式发布Oracle Linux 7.1
- mysql中int、bigint、smallint 和 tinyint的区别与长度
- mysql load data 导出、导入 csv
- source命令执行SQL脚本文件
- MySQL创建用户及权限控制
- MySQL管理数据表
- linux下mysql添加用户
- mysql procedure
- mysql触发器
- Oracle Containers for J2EE远程安全漏洞(CVE-2014-0413)
- Oracle 10g R2不能使用EM的问题
- MySQL 备份和恢复策略
- 表空间操作
- PreparedStatement中in子句的处理
- mac下安装mysql(转载)
- mysql 修改编码 Linux/Mac/Unix/通用(杜绝修改后无法启动的情况!)