update
ORACLE UPDATE 语句语法与性能分析的一点看法
为了方便起见,建立了以下简单模型,和构造了部分测试数据:
在某个业务受理子系统BSS中,
--客户资料表
create table customers
(
customer_id number(8) not null, -- 客户标示
city_name varchar2(10) not null, -- 所在城市
customer_type char(2) not null, -- 客户类型
...
)
create unique index PK_customers on customers (customer_id)
由于某些原因,客户所在城市这个信息并不什么准确,但是在
客户服务部的CRM子系统中,通过主动服务获取了部分客户20%的所在
城市等准确信息,于是你将该部分信息提取至一张临时表中:
create table tmp_cust_city
(
customer_id number(8) not null,
citye_name varchar2(10) not null,
customer_type char(2) not null
)
1) 最简单的形式
--经确认customers表中所有customer_id小于1000均为'北京'
--1000以内的均是公司走向全国之前的本城市的老客户:)
update customers
set city_name='北京'
where customer_id<1000
2) 两表(多表)关联update -- 仅在where字句中的连接
--这次提取的数据都是VIP,且包括新增的,所以顺便更新客户类别
update customers a -- 使用别名
set customer_type='01' --01 为vip,00为普通
where exists (select 1
from tmp_cust_city b
where b.customer_id=a.customer_id
)
3) 两表(多表)关联update -- 被修改值由另一个表运算而来
update customers a -- 使用别名
set city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)
where exists (select 1
from tmp_cust_city b
where b.customer_id=a.customer_id
)
-- update 超过2个值
update customers a -- 使用别名
set (city_name,customer_type)=(select b.city_name,b.customer_type
from tmp_cust_city b
where b.customer_id=a.customer_id)
where exists (select 1
from tmp_cust_city b
where b.customer_id=a.customer_id
)
注意在这个语句中,
=(select b.city_name,b.customer_type
from tmp_cust_city b
where b.customer_id=a.customer_id
)
与
(select 1
from tmp_cust_city b
where b.customer_id=a.customer_id
)
是两个独立的子查询,查看执行计划可知,对b表/索引扫描了2篇;
如果舍弃where条件,则默认对A表进行全表
更新,但由于(select b.city_name from tmp_cust_city b where where b.customer_id=a.customer_id)
有可能不能提供"足够多"值,因为tmp_cust_city只是一部分客户的信息,
所以报错(如果指定的列--city_name可以为NULL则另当别论):
01407, 00000, "cannot update (%s) to NULL"
// *Cause:
// *Action:
一个替代的方法可以采用:
update customers a -- 使用别名
set city_name=nvl((select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id),a.city_name)
或者
set city_name=nvl((select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id),'未知')
-- 当然这不符合业务逻辑了
4) 上述3)在一些情况下,因为B表的纪录只有A表的20-30%的纪录数,
考虑A表使用INDEX的情况,使用cursor也许会比关联update带来更好的性能:
set serveroutput on
declare
cursor city_cur is
select customer_id,city_name
from tmp_cust_city
order by customer_id;
begin
for my_cur in city_cur loop
update customers
set city_name=my_cur.city_name
where customer_id=my_cur.customer_id;
/** 此处也可以单条/分批次提交,避免锁表情况 **/
-- if mod(city_cur%rowcount,10000)=0 then
-- dbms_output.put_line('----');
-- commit;
-- end if;
end loop;
end;
5) 关联update的一个特例以及性能再探讨
在oracle的update语句语法中,除了可以update表之外,也可以是视图,所以有以下1个特例:
update (select a.city_name,b.city_name as new_name
from customers a,
tmp_cust_city b
where b.customer_id=a.customer_id
)
set city_name=new_name
这样能避免对B表或其索引的2次扫描,但前提是 A(customer_id) b(customer_id)必需是unique index
或primary key。否则报错:
01779, 00000, "cannot modify a column which maps to a non key-preserved table"
// *Cause: An attempt was made to insert or update columns of a join view which
// map to a non-key-preserved table.
// *Action: Modify the underlying base tables directly.
6)oracle另一个常见错误
回到3)情况,由于某些原因,tmp_cust_city customer_id 不是唯一index/primary key
update customers a -- 使用别名
set city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)
where exists (select 1
from tmp_cust_city b
where b.customer_id=a.customer_id
)
当对于一个给定的a.customer_id
(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)
返回多余1条的情况,则会报如下错误:
01427, 00000, "single-row subquery returns more than one row"
// *Cause:
// *Action:
一个比较简单近似于不负责任的做法是
update customers a -- 使用别名
set city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)
如何理解 01427 错误,在一个很复杂的多表连接update的语句,经常因考虑不周,出现这个错误,
仍已上述例子来描述,一个比较简便的方法就是将A表代入 值表达式 中,使用group by 和
having 字句查看重复的纪录
(select b.customer_id,b.city_name,count(*)
from tmp_cust_city b,customers a
where b.customer_id=a.customer_id
group by b.customer_id,b.city_name
having count(*)>=2
)
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=581488
×××××××××××××××××××××××××××
Oracle中的UPDATE FROM解决方法
来源:雪精灵社区 等级:III
今天又碰到了这个问题,在Oracle中没有update from这样的语法,但可以实现同样的功能。
表的更新操作中,在很多情况下需要在表达式中引用要更新的表以外的数据。象sql server提供了update的from 子句,可以将要更新的表与其它的数据源连接起来。虽然只能对一个表进行更新,但是通过将要更新的表与其它的数据源连接起来,就可以在update的表达式中引用要更新的表以外的其它数据。
例如:
UPDATE dbo.Table2
SET dbo.Table2.ColB = dbo.Table2.ColB + dbo.Table1.ColB
FROM dbo.Table2
INNER JOIN dbo.Table1
ON (dbo.Table2.ColA = dbo.Table1.ColA);
实际更新的操作是在要更新的表上进行的,而不是在from子句所形成的新的结果集上进行的。
Oracle没有update from语法,可以通过两种写法实现同样的功能:
1:子查询UPDATE A SET A.NAME=(SELECT B.NAME FROM B WHERE B.ID=A.ID),本查询要根据具体情况看看是否变通成如下
UPDATE A SET A.NAME=(SELECT B.NAME FROM B WHERE B.ID=A.ID) WHERE A.ID IN (SELECT ID FROM B);
2:利用视图来做
UPDATE (SELECT A.NAME ANAME,B.NAME BNAME FROM A,B WHERE A.ID=B.ID)
SET ANAME=BNAME;
例如:
UPDATE tablea a
SET a.fieldforupdate = (SELECT b.fieldsource FROM tableb b
WHERE a.keyfield = b.keyfield)
WHERE EXISTS (SELECT b.fieldsource FROM tableb b
WHERE a.keyfield = b.keyfield)
有三点需要注意:
1. 对于一个给定的a.keyfield的值,SELECT b.fieldsource FROM tableb b WHERE a.keyfield = b.keyfield 的值只能是一个唯一值,不能是多值。
2. 在绝大多数情况下,最后面的where EXISTS子句是重要的,否则将得到错误的结果。
3. 对于视图更新的限制:
如果视图基于多个表的连接,那么用户更新(update)视图记录的能力将受到限制。除非update只涉及一个表且视图列中包含了被更新的表的整个主键,否则不能更新视图的基表。
×××××××××××××××××××××××××××
[转载]Oracle Update Statements Version 10.2
Basic Update Statements
Update all records UPDATE
SET =
CREATE TABLE test AS
SELECT object_name, object_type
FROM all_objects;
SELECT DISTINCT object_name
FROM test;
UPDATE test
SET object_name = 'OOPS';
SELECT DISTINCT object_name
FROM test;
ROLLBACK;
Update a specific record UPDATE
SET =
WHERE =
SELECT DISTINCT object_name
FROM test;
UPDATE test
SET object_name = 'LOAD'
WHERE object_name = 'DUAL';
COMMIT;
SELECT DISTINCT object_name
FROM test
Update based on a single queried value UPDATE
SET = (
SELECT
FROM WHERE );
CREATE TABLE test AS
SELECT table_name
FROM all_tables;
ALTER TABLE test
ADD (lower_name VARCHAR2(30));
SELECT *
FROM test
WHERE table_name LIKE '%A%';
UPDATE test t
SET lower_name = (
SELECT DISTINCT lower(table_name)
FROM all_tables a
WHERE a.table_name = t.table_name
AND a.table_name LIKE '%A%');
COMMIT;
SELECT *
FROM test;
Update based on a query returning multiple values UPDATE
SET (, ) = (
SELECT (, )
FROM
WHERE = );
CREATE TABLE test AS
SELECT t. table_name, t. tablespace_name, s.extent_management
FROM user_tables t, user_tablespaces s
WHERE t.tablespace_name = s. tablespace_name
AND 1=2;
desc test
SELECT * FROM test;
-- does not work
UPDATE test
SET (table_name, tablespace_name) = (
SELECT table_name, tablespace_name
FROM user_tables);
-- works
INSERT INTO test
(table_name, tablespace_name)
SELECT table_name, tablespace_name
FROM user_tables;
COMMIT;
SELECT *
FROM test
WHERE table_name LIKE '%A%';
-- does not work
UPDATE test t
SET tablespace_name, extent_management = (
SELECT tablespace_name, extent_management
FROM user_tables a, user_tablespaces u
WHERE t.table_name = a.table_name
AND a.tablespace_name = u.tablespace_name
AND t.table_name LIKE '%A%');
-- does not works
UPDATE test t
SET (tablespace_name, extent_management) = (
SELECT DISTINCT u.tablespace_name, u.extent_management
FROM user_tables a, user_tablespaces u
WHERE t.table_name = a.table_name
AND a.tablespace_name = u.tablespace_name
AND t.table_name LIKE '%A%');
rollback;
-- works
UPDATE test t
SET (tablespace_name, extent_management) = (
SELECT DISTINCT u.tablespace_name, u.extent_management
FROM user_tables a, user_tablespaces u
WHERE t.table_name = a.table_name
AND a.tablespace_name = u.tablespace_name)
WHERE t.table_name LIKE '%A%';
COMMIT;
SELECT *
FROM test;
Update the results of a SELECT statement UPDATE ()
SET = ;
SELECT *
FROM test
WHERE table_name LIKE '%A%';
SELECT *
FROM test
WHERE table_name NOT LIKE '%A%';
UPDATE (
SELECT *
FROM test
WHERE table_name NOT LIKE '%A%')
SET extent_management = 'Unknown';
SELECT *
FROM test;
Correlated Update
Single column UPDATE TABLE()
SET = (
SELECT
FROM
WHERE = ;
conn hr/hr
CREATE TABLE empbak AS
SELECT * FROM employees;
UPDATE employees
SET salary = salary * 1.1;
COMMIT;
UPDATE employees t1
SET salary = (
SELECT salary
FROM empbak t2
WHERE t1.employee_id = t2.employee_id);
Multi-column UPDATE TABLE()
SET = ;
UPDATE table1 t_alias1
SET (col1, col2) = (
SELECT col1, col2
FROM table2 t_alias2
WHERE t_alias1.col3 = t_alias2.col3);
Nested Table Update
See Nested Tables page
Update With Returning Clause
Returning Clause demo UPDATE ()
SET ....;
conn hr/hr
var bnd1 NUMBER
var bnd2 VARCHAR2(30)
var bnd3 NUMBER
UPDATE employees
SET job_id ='SA_MAN', salary = salary + 1000,
department_id = 140
WHERE last_name = 'Jones'
RETURNING salary*0.25, last_name, department_id
INTO :bnd1, :bnd2, :bnd3;
print bnd1
print bnd2
print bnd3
conn hr/hr
variable bnd1 NUMBER
UPDATE employees
SET salary = salary * 1.1
WHERE department_id = 100
RETURNING SUM(salary) INTO :bnd1;
print bnd1
Update Object Table
Update a table object UPDATE ()
SET ....;
UPDATE table1 p SET VALUE(p) =
(SELECT VALUE(q) FROM table2 q WHERE p.id = q.id)
WHERE p.id = 10;
Record Update
Update based on a record UPDATE
SET ROW = ;
CREATE TABLE t AS
SELECT table_name, tablespace_name
FROM all_tables;
SELECT DISTINCT tablespace_name
FROM t;
DECLARE
trec t%ROWTYPE;
BEGIN
trec.table_name := 'DUAL';
trec.tablespace_name := 'NEW_TBSP';
UPDATE t
SET ROW = trec
WHERE table_name = 'DUAL';
COMMIT;
END;
/
SELECT DISTINCT tablespace_name
FROM t;
Update Partitioned Table
Update only records in a single partition UPDATE PARTITION
SET =
WHERE ;
UPDATE sales PARTITION (sales_q1_2005) s
SET s.promo_id = 494
WHERE amount_sold > 9000;
×××××××××××××××××××××××××××
在两张表之间使用UPDATE语句???
楼主xiaoailiu(小艾)2002-08-28 09:57:11 在 Oracle / 基础和管理 提问
两张表:id,ida
select * from id;
ID A B C
---------- - - -
1 a c c
2 a c c
3 a c c
4 a c c
select * from ida;
ID A B C
---------- - - -
2 A C C
3 A C C
1 A C C
4 A C C
想用IDA表的A,B,C字段替换到ID表中相同ID的A,B,C字段上去??
请大家帮助!!!
---------------------------------------------------
人见人爱的静静小艾与你交流,共享!
问题点数:0、回复次数:16Top
1 楼bluecetacean(剑鱼)回复于 2002-08-28 10:03:31 得分 0
update id set A=(select A from id,ida where id.id=ida.id),B=(select B from id,ida where id.id=ida.id),C=(select C from id,ida where id.id=ida.id)Top
2 楼xiaoailiu(小艾)回复于 2002-08-28 10:07:28 得分 0
楼上的朋友,请问这句你执行过吗????
我这执行是不正确的!Top
3 楼myxchy(阿呆)回复于 2002-08-28 10:23:50 得分 0
update id t1 set a=(select t2.a from ida t2 where t1.id=t2.id),b=(select t2.b from ida t2 where t1.id=t2.id),c=(select t2.c from ida t2 where t1.id=t2.id)Top
4 楼gwolfupb(括弧)回复于 2002-08-28 10:25:02 得分 0
delete from id where id in (select id from ida);
insert into id select * from ida;Top
5 楼myxchy(阿呆)回复于 2002-08-28 10:30:56 得分 0
还少一句条件
where t1.id=t2.idTop
6 楼myxchy(阿呆)回复于 2002-08-28 10:35:41 得分 0
delete from id where id in (select id from ida);
insert into id select * from ida where id.id=ida.id;
Top
7 楼black_dragon(半仙)回复于 2002-08-28 10:39:29 得分 0
update id set (a,b,c)=(select a,b,c from ida where ida.id=id.id);Top
8 楼myxchy(阿呆)回复于 2002-08-28 10:46:23 得分 0
或者
update id t1 set a=(select t2.a from ida t2 where t1.id=t2.id),b=(select t2.b from ida t2 where t1.id=t2.id),c=(select t2.c from ida t2 where t1.id=t2.id) where t1.id in(select t3.id from id t3,ida t4
where t3.id=t4.id)
Top
9 楼myxchy(阿呆)回复于 2002-08-28 10:54:39 得分 0
半仙的写法不对
Top
10 楼myxchy(阿呆)回复于 2002-08-28 11:00:19 得分 0
update id t1 set a=(select t2.a from ida t2 where t1.id=t2.id),b=(select t2.b from ida t2 where t1.id=t2.id),c=(select t2.c from ida t2 where t1.id=t2.id) where exists (select * from id,ida where id.id=ida.id)
Top
11 楼black_dragon(半仙)回复于 2002-08-28 11:12:27 得分 0
myxchy(阿呆) :
我的数据库是8.1.7,语句我已经验证过了,正确!Top
12 楼lgm76(lgm)回复于 2002-09-02 14:39:30 得分 0
update a set a.a = b.a , a.b = b.b , a.c=b.c from id a , ida b where a.id = b.idTop
13 楼lyl000(liyl)回复于 2002-09-02 17:02:32 得分 0
楼上的与具有错误,不过思路挺好,但没有调出来,它可能做到一条语句更新多个表的
black_dragon(半仙)的方法非常好
Top
14 楼luckysxn(坚持到底)回复于 2002-09-02 17:24:45 得分 0
楼上的楼上。你以为ORACLE是SQLSERVER 或是SYBASE 呢。
半仙是对的。应该没有什么问题。
Top
15 楼biti_rainy(biti_rainy)回复于 2002-09-04 21:37:12 得分 0
http://www.itpub.net/showthread.php?s=&threadid=45822&highlight=%BE%AD%B5%E4
可以给大家以参考
注意8i和9i的变化Top
16 楼jettli(李杰)回复于 2002-09-17 10:56:38 得分 0
我也有同样的问题,我觉得这些回答好象都不太好吧,因为如果两表的数据是一对多的,会发现返回多行数据的情况,就不好解决了但是我记得在某书中见一过,想不起来了,我也想知道一个好的UPDATE的方法
有关UPdate语句的吐血问题
_____
发布时间: 2006-12-15 ; 上次回复: 2006-12-15; 总计回复: 16人次
_____
我有下面的一个批处理更新的SQL Server的语句,请问转到Oracle的语法是怎样的?
这句SQL的含义是,用b表与a表关联,满足条件的a表的记录的字段用B表更新。如果不能实现这个,岂不是样定义Cursor来循环?
UPdate tOperAll set fEmplName = b.EMPNAME ,fPWD = b.PassWord
from tOperAll a ,Employee b
Where a.EMPNO = b.fEmplNo
and a.fSysNo = '1'
哪位大侠能否提供一些Oracle的SQL写法的例子,包括左连接,右连接,子查询等内容,谢谢。
_____
bluelamb [等级:◆◆(初级)] (信誉值: 100) 回复于: 2004-10-12 10:56:19 Top
转换为oracle为:
update (select a.*,b.* from tOperAll a ,Employee b
where a.EMPNO = b.fEmplNo
and a.fSysNo = '1') set fEmplName =EMPNAME ,fPWD = PassWord
_____
匿名 [等级:◆(初级)] (信誉值: 100) 回复于: 2004-10-12 10:59:55 Top
谢谢大哥,我马上Try,看结构很有道理,还有其他的写法吗?
_____
bluelamb [等级:◆◆(初级)] (信誉值: 100) 回复于: 2004-10-12 11:00:55 Top
或者
update tOperAll a set fEmplName =(select EMPNAME from Employee b
where Where a.EMPNO = b.fEmplNo
and a.fSysNo = '1'),fPWD =select PassWord from Employee b
where Where a.EMPNO = b.fEmplNo
and a.fSysNo = '1');
这样比较罗索
_____
SongZip [等级:★★(中级)] (信誉值: 149) 回复于: 2004-10-12 11:03:37 Top
UPdate tOperAll a set (fEmplName,fPWD) = (
select b.EMPNAME,b.PassWord
from Employee b
Where a.EMPNO = b.fEmplNo
and a.fSysNo = '1')
where exits(select b.EMPNAME,b.PassWord
from Employee b
Where a.EMPNO = b.fEmplNo
and a.fSysNo = '1')
_____
@ [等级:◆(初级)] (信誉值: 100) 回复于: 2004-10-12 11:08:22 Top
UPdate tOperAll a set
fEmplName = (select b.EMPNAME from Employee b Where a.EMPNO = b.fEmplNo ),
fPWD = (select c.PassWord from Employee c Where a.EMPNO = c.fEmplNo )
where a.fSysNo = '1'
没调过,试试吧
_____
@ [等级:◆(初级)] (信誉值: 100) 回复于: 2004-10-12 11:10:11 Top
UPdate tOperAll a set
(fEmplName,fPWD) = (select b.EMPNAME,b.PassWord from Employee b Where a.EMPNO = b.fEmplNo )
where a.fSysNo = '1'
_____
bluelamb [等级:◆◆(初级)] (信誉值: 100) 回复于: 2004-10-12 11:14:31 Top
还有就是
UPdate tOperAll a set
(fEmplName,fPWD) = (select b.EMPNAME,b.PassWord from Employee b Where a.EMPNO = b.fEmplNo )
where a.fSysNo = '1'
_____
匿名 [等级:◆(初级)] (信誉值: 100) 回复于: 2004-10-12 11:15:59 Top
还有吗?让我开开眼界,我只熟悉SQL SERVER的语法。谢谢了
_____
匿名 [等级:◆(初级)] (信誉值: 100) 回复于: 2004-10-12 11:27:36 Top
csdn的高手很多,热心人也很多,谢谢.
_____
king [等级:◆(初级)] (信誉值: 98) 回复于: 2004-10-12 13:40:10 Top
update (select a.*,b.* from tOperAll a ,Employee b
where a.EMPNO = b.fEmplNo
and a.fSysNo = '1') set fEmplName =EMPNAME ,fPWD = PassWord
这种写法还是第一次看见,真是长见识了!
_____
匿名 [等级:◆(初级)] (信誉值: 100) 回复于: 2004-10-12 15:27:19 Top
update (select a.*,b.* from tOperAll a ,Employee b
where a.EMPNO = b.fEmplNo
and a.fSysNo = '1') set fEmplName =EMPNAME ,fPWD = PassWord
这种写法效率应该比较好,普通的写法是什么?
_____
匿名 [等级:◆(初级)] (信誉值: 100) 回复于: 2004-10-12 15:27:36 Top
update (select a.*,b.* from tOperAll a ,Employee b
where a.EMPNO = b.fEmplNo
and a.fSysNo = '1') set fEmplName =EMPNAME ,fPWD = PassWord
这种写法效率应该比较好,普通的写法是什么?
[@more@]来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/412/viewspace-912307/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/412/viewspace-912307/