日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

oracle update的總結(jié)

系統(tǒng) 2382 0

在某個(gè)業(yè)務(wù)受理子系統(tǒng)BSS中,
--客戶資料表
create table customers
(
customer_id number(8) not null, -- 客戶標(biāo)示
city_name varchar2(10) not null, -- 所在城市
customer_type char(2) not null, -- 客戶類型

...
)
create unique index PK_customers on customers (customer_id)
由于某些原因,客戶所在城市這個(gè)信息并不什么準(zhǔn)確,但是在
客戶服務(wù)部的CRM子系統(tǒng)中,通過(guò)主動(dòng)服務(wù)獲取了部分客戶20%的所在
城市等準(zhǔn)確信息,于是你將該部分信息提取至一張臨時(shí)表中:
create table tmp_cust_city
(
customer_id number(8) not null,
citye_name varchar2(10) not null,
customer_type char(2) not null
)
1) 最簡(jiǎn)單的形式
--經(jīng)確認(rèn)customers表中所有customer_id小于1000均為'北京'
--1000以內(nèi)的均是公司走向全國(guó)之前的本城市的老客戶:)
update customers
set city_name='北京'
where customer_id<1000
2) 兩表(多表)關(guān)聯(lián)update -- 僅在where字句中的連接
--這次提取的數(shù)據(jù)都是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) 兩表(多表)關(guān)聯(lián)update -- 被修改值由另一個(gè)表運(yùn)算而來(lái)
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 超過(guò)2個(gè)值
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
)
注意在這個(gè)語(yǔ)句中,
=(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
)
是兩個(gè)獨(dú)立的子查詢,查看執(zhí)行計(jì)劃可知,對(duì)b表/索引掃描了2篇;
如果舍棄where條件,則默認(rèn)對(duì)A表進(jìn)行全表
更新,但由于(select b.city_name from tmp_cust_city b where
where b.customer_id=a.customer_id)
有可能不能提供"足夠多"值,因?yàn)閠mp_cust_city只是一部分客戶的信息,
所以報(bào)錯(cuò)(如果指定的列--city_name可以為NULL則另當(dāng)別論):

01407, 00000, "cannot update (%s) to NULL"
// *Cause:
// *Action:
一個(gè)替代的方法可以采用:
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),'未知')
-- 當(dāng)然這不符合業(yè)務(wù)邏輯了
4) 上述3)在一些情況下,因?yàn)锽表的紀(jì)錄只有A表的20-30%的紀(jì)錄數(shù),
考慮A表使用INDEX的情況,使用cursor也許會(huì)比關(guān)聯(lián)update帶來(lái)更好的性能:

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) 關(guān)聯(lián)update的一個(gè)特例以及性能再探討
在oracle的update語(yǔ)句語(yǔ)法中,除了可以u(píng)pdate表之外,也可以是視圖,所以有以下1個(gè)特例:
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
這樣能避免對(duì)B表或其索引的2次掃描,但前提是 A(customer_id) b(customer_id)必需是unique index
或primary key。否則報(bào)錯(cuò):

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另一個(gè)常見(jiàn)錯(cuò)誤
回到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
)
當(dāng)對(duì)于一個(gè)給定的a.customer_id
(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)
返回多余1條的情況,則會(huì)報(bào)如下錯(cuò)誤:

01427, 00000, "single-row subquery returns more than one row"
// *Cause:
// *Action:
一個(gè)比較簡(jiǎn)單近似于不負(fù)責(zé)任的做法是
update customers a -- 使用別名
set city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)
如何理解 01427 錯(cuò)誤,在一個(gè)很復(fù)雜的多表連接update的語(yǔ)句,經(jīng)常因考慮不周,出現(xiàn)這個(gè)錯(cuò)誤,
仍已上述例子來(lái)描述,一個(gè)比較簡(jiǎn)便的方法就是將A表代入值表達(dá)式中,使用group by 和
having 字句查看重復(fù)的紀(jì)錄
(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
)
作者Blog:http://blog.csdn.net/aceplus/
相關(guān)文章
ORACLE UPDATE 語(yǔ)句語(yǔ)法與性能分析的一點(diǎn)看法
移動(dòng)的微軟移動(dòng)的Windows
[收藏]數(shù)據(jù)庫(kù)設(shè)計(jì)中的反規(guī)范技術(shù)探討
Oracle9i與SYBASE ASE12.5相比的幾個(gè)不足
如何在已裝Windowns XP機(jī)器上再裝Windows 2000

對(duì)該文的評(píng)論
aceplus ( 2005-07-04)
/**** 改正1個(gè)錯(cuò)誤 ****/
一個(gè)比較簡(jiǎn)單近似于不負(fù)責(zé)任的做法是
update customers a -- 使用別名
set city_name=(select b.city_name from tmp_cust_city b
where b.customer_id=
a.customer_id and rownum=1)
如何理解 01427 錯(cuò)誤,在一個(gè)很復(fù)雜的多表連接update的語(yǔ)句,經(jīng)常因考慮不周,出現(xiàn)這個(gè)錯(cuò)誤,
仍已上述例子來(lái)描述,一個(gè)比較簡(jiǎn)便的方法就是將A表代入值表達(dá)式中,使用group by 和
having 字句查看重復(fù)的紀(jì)錄
(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
)

aceplus ( 2005-07-04)
/**** 改正1個(gè)錯(cuò)誤 ****/
一個(gè)比較簡(jiǎn)單近似于不負(fù)責(zé)任的做法是
update customers a -- 使用別名
set city_name=(select b.city_name from tmp_cust_city b
where b.customer_id=
a.customer_id rownum=1)
如何理解 01427 錯(cuò)誤,在一個(gè)很復(fù)雜的多表連接update的語(yǔ)句,經(jīng)常因考慮不周,出現(xiàn)這個(gè)錯(cuò)誤,
仍已上述例子來(lái)描述,一個(gè)比較簡(jiǎn)便的方法就是將A表代入值表達(dá)式中,使用group by 和
having 字句查看重復(fù)的紀(jì)錄
(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
)

oracle update的總結(jié)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 嘉义县| 东宁县| 临武县| 蕉岭县| 山西省| 清远市| 东乡族自治县| 恭城| 壤塘县| 琼中| 利辛县| 康平县| 黔西| 汤阴县| 博湖县| 山东省| 安远县| 防城港市| 巴彦县| 扶绥县| 松潘县| 花莲县| 保靖县| 阿尔山市| 射洪县| 岑溪市| 凤冈县| 浙江省| 砀山县| 江孜县| 新疆| 满城县| 辛集市| 娱乐| 三都| 芒康县| 成都市| 阜新市| 铜川市| 江陵县| 盐津县|