欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

ORACLE 多表關(guān)聯(lián) UPDATE 語句

系統(tǒng) 2456 0
快樂無極 , 15:18 , 開發(fā)文檔 , 評論(0) , , 閱讀(539) , Via aceplus | | 引用地址:
注意: 該地址僅在今日23:59:59之前有效?? 為了方便起見,建立了以下簡單模型,和構(gòu)造了部分測試數(shù)據(jù):
?? 在某個業(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)
?? 由于某些原因,客戶所在城市這個信息并不什么準(zhǔn)確,但是在
?? 客戶服務(wù)部的CRM子系統(tǒng)中,通過主動服務(wù)獲取了部分客戶20%的所在
?? 城市等準(zhǔn)確信息,于是你將該部分信息提取至一張臨時表中:
??create table tmp_cust_city
?? (
?? customer_id???? number(8) not null,
?? citye_name???? varchar2(10) not null,
?? customer_type??char(2)?? not null
??)


1) 最簡單的形式
?? --經(jīng)確認(rèn)customers表中所有customer_id小于1000均為'北京'
?? --1000以內(nèi)的均是公司走向全國之前的本城市的老客戶:)
?? 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 -- 被修改值由另一個表運算而來
?? 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
???????????????? )
?? 是兩個獨立的子查詢,查看執(zhí)行計劃可知,對b表/索引掃描了2篇;
?? 如果舍棄where條件,則默認(rèn)對A表進(jìn)行全表
?? 更新,但由于(select b.city_name from tmp_cust_city b where where??b.customer_id=a.customer_id)
?? 有可能不能提供"足夠多"值,因為tmp_cust_city只是一部分客戶的信息,
?? 所以報錯(如果指定的列--city_name可以為NULL則另當(dāng)別論):
??
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),'未知')
?? -- 當(dāng)然這不符合業(yè)務(wù)邏輯了

4) 上述3)在一些情況下,因為B表的紀(jì)錄只有A表的20-30%的紀(jì)錄數(shù),
?? 考慮A表使用INDEX的情況,使用cursor也許會比關(guān)聯(lián)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) 關(guān)聯(lián)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
???????????????? )
?? 當(dāng)對于一個給定的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:

?? 一個比較簡單近似于不負(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 錯誤,在一個很復(fù)雜的多表連接update的語句,經(jīng)常因考慮不周,出現(xià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 多表關(guān)聯(lián) UPDATE 語句


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 色欲天天婬色婬香影院 | 范丞丞星座| 久久福利青草精品资源 | 久久久国产99久久国产首页 | 欧美精品一区二区在线电影 | 毛片a片免费看 | 亚洲一区二区免费视频 | 亚洲一区二区色情苍井空 | 北岛玲亚洲一区在线观看 | 最新中文字幕 | 91精品国产91久久久久久 | 黄色av网站免费看 | 一区二区欧美视频 | 色偷偷成人网免费视频男人的天堂 | 成人性生交大片 | 亚洲中午字幕 | 日韩欧美国产精品第一页不卡 | 日日碰 | 欧美色欧美亚洲高清在线视频 | 午夜精品一区二区三区免费视频 | 久久久久女人精品毛片 | 成人在线第一页 | 成人性大片免费观看网站 | 日韩在线观看毛片 | 精品粉嫩aⅴ一区二区三区四区 | 青青草国产精品欧美成人 | 国产一卡2卡三卡4卡免费网站 | 久久伊人亚洲 | 97骚碰 | 日本三级2020 | 天天干天天色综合 | 国产一区二区在线免费观看 | 久久国产精品一区二区 | 国产苐1页影院草草影院 | 玖玖成人 | 国产乱码精品一区二区三上 | 亚洲美女精品 | 性夜a爽黄爽 | 青草久久免费视频 | 免费国产午夜在线观看 | 亚洲免费人成在线视频观看 |