oracle更新语法:
1.通常语法
update tab set col = .... [where ...] =后能够有子查询,可是必须对于tab的每一列返回惟一一行与之对应,where是须要更新的表,部分更新必须加,不然相关子查询的更新会把没有匹配的更新为null,如
update tab a set a.col=(select b.col from b where a.id=b.id) where exists (select 1 from b where a.id=b.id) 相似地写了多遍
2.改进语法merge
merge into tab
using (表|视图|子查询等) --子查询须要加括号 on (条件)
when match then
do update
when no match then
do insert
insert语法和update语法有所不一样,详细参考文档,10g还支持update,insert的有条件更新和插入,支持update的delete where,支持只有update或insert的
不能修改using里的关联列,一样,必须每一行有惟一与之对应的
上面两种语法若是找不到惟一对应的,须要改进语句,好比加rownum=1
3.update inline view的用法
update (select ...........关联查询) set 目标=源
如 update(select a.name,b.name from a,b where a.id=b.id) set a.name=b.name;
须要unique建保证惟一对应,好比上面的必需要b.id有惟一键,也就是preserved key,好比惟一索引什么的均可以,11g以前能够用hint: bypass_ujvc,这样不须要惟一键,可是可能有问题,一对多会更新屡次,11g这个hint失效
delete (select ....) 也能够,有不少要求,能够看sql文档,insert (select ...)限制更多
第3种方法来源于可更新的视图
oracle更新基本有3种sql写法,后面两种每每优化中会使用到,特别第一种的更新关联子查询中源表不走索引,那么更新不少,至关于 nested loop,确定慢,并且还有个where过滤,屡次访问源表
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
sql
MERGE语句是Oracle9i新增的语法,用来合并UPDATE和INSERT语句。经过MERGE语句,根据一张表或子查询的链接条件对另一张表进行查询,链接条件匹配上的进行UPDATE,没法匹配的执行INSERT。这个语法仅须要一次全表扫描就完成了所有工做,执行效率要高于INSERT+UPDATE。express
*/oracle
/*語法:oop
MERGE [INTO [schema .] table [t_alias]优化
USING [schema .] { table | view | subquery } [t_alias]索引
ON ( condition )文档
WHEN MATCHED THEN merge_update_clauseit
WHEN NOT MATCHED THEN merge_insert_clause;io
*/table
语法:
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 ! ]
/*
咱们仍是以《sql中的case应用》中的表为例。在建立另两个表fzq1和fzq2
*/
--所有男生记录
create table fzq1 as select * from fzq where sex=1;
--所有女生记录
create table fzq2 as select * from fzq where sex=0;
/*涉及到两个表关联的例子*/
--更新表fzq1使得id相同的记录中chengji字段+1,而且更新name字段。
--若是id不相同,则插入到表fzq1中.
--将fzq1表中男生记录的成绩+1,女生插入到表fzq1中
merge into fzq1 aa --fzq1表是须要更新的表
using fzq bb -- 关联表
on (aa.id=bb.id) --关联条件
when matched then --匹配关联条件,做更新处理
update set
aa.chengji=bb.chengji+1,
aa.name=bb.name --此处只是说明能够同时更新多个字段。
when not matched then --不匹配关联条件,做插入处理。若是只是做更新,下面的语句能够省略。
insert values( bb.id, bb.name, bb.sex,bb.kecheng,bb.chengji);
--能够自行查询fzq1表。
/*涉及到多个表关联的例子,咱们以三个表为例,只是做更新处理,不作插入处理。固然也能够只作插入处理*/
--将fzq1表中女生记录的成绩+1,没有直接去sex字段。而是fzq和fzq2关联。
merge into fzq1 aa --fzq1表是须要更新的表
using (select fzq.id,fzq.chengji
from fzq join fzq2
on fzq.id=fzq2.id) bb -- 数据集
on (aa.id=bb.id) --关联条件
when matched then --匹配关联条件,做更新处理
update set
aa.chengji=bb.chengji+1
--能够自行查询fzq1表。
/*不能作的事情*/
merge into fzq1 aa
using fzq bb
on (aa.id=bb.id)
when matched then
update set
aa.id=bb.id+1
/*系统提示:
ORA-38104: Columns referenced in the ON Clause cannot be updated: "AA"."ID"
咱们不能更新on (aa.id=bb.id)关联条件中的字段*/
update fzq1
set id=(select id+1 from fzq where fzq.id=fzq1.id)
where id in
(select id from fzq)