ORACLE 多表關(guān)聯(lián) UPDATE 語(yǔ)句_第1頁(yè)
ORACLE 多表關(guān)聯(lián) UPDATE 語(yǔ)句_第2頁(yè)
ORACLE 多表關(guān)聯(lián) UPDATE 語(yǔ)句_第3頁(yè)
ORACLE 多表關(guān)聯(lián) UPDATE 語(yǔ)句_第4頁(yè)
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

ORACLE多表關(guān)聯(lián)UPDATE語(yǔ)句為了方便起見(jiàn),建立了以下簡(jiǎn)單模型,和構(gòu)造了部分測(cè)試數(shù)據(jù):在某個(gè)業(yè)務(wù)受理子系統(tǒng)BSS中,--客戶資料表createtablecustomers(customer_idnumber(8) not null, -- 客戶標(biāo)示city_namevarchar2(10) not null, -- 所在城市customer_typechar(2) not null, -- 客戶類(lèi)型)createuniqueindexPK_customersoncustomers(customer_id)由于某些原因,客戶所在城市這個(gè)信息并不什么準(zhǔn)確,但是在客戶服務(wù)部的CRM子系統(tǒng)中,通過(guò)主動(dòng)服務(wù)獲取了部分客戶20%的所在城市等準(zhǔn)確信息,于是你將該部分信息提取至一張臨時(shí)表中:createtabletmp_cust_city(customer_id number(8)notnull,citye_name varchar2(10)notnull,customer_typechar(2)notnull)最簡(jiǎn)單的形式--經(jīng)確認(rèn)customers表中所有customer_id小于1000均為'北京'--1000以內(nèi)的均是公司走向全國(guó)之前的本城市的老客戶:)updatecustomersset city_name二北京’wherecustomer_id<1000兩表(多表)關(guān)聯(lián)update—僅在where字句中的連接—這次提取的數(shù)據(jù)都是VIP,且包括新增的,所以順便更新客戶類(lèi)別updatecustomersa --使用別名setcustomer_type='01'--01為vip,00為普通whereexists(select1fromtmp_cust_citybwhereb.customer_id=a.customer_id)兩表(多表)關(guān)聯(lián)update—被修改值由另一個(gè)表運(yùn)算而來(lái)updatecustomersa--使用別名wheresetcity_name=(selectb.city_namefromtmp_cust_citybb.customer_id=a.customer_id)wherewhereexists(select1fromtmp_cust_citybwhereb.customer_id=a.customer_id)--update超過(guò)2個(gè)值updatecustomersa--使用別名set(city_name,customer_type)=(selectb.city_name,b.customer_typefromtmp_cust_citybwhereb.customer_id=a.customer_id)whereexists(select1fromtmp_cust_citybwhereb.customer_id=a.customer_id)注意在這個(gè)語(yǔ)句中,=(selectb.city_name,b.customer_typefromtmp_cust_citybwhereb.customer_id=a.customer_id)與(select1fromtmp_cust_citybwhereb.customer_id=a.customer_id)是兩個(gè)獨(dú)立的子查詢,查看執(zhí)行計(jì)劃可知,對(duì)b表/索引掃描了2篇;如果舍棄where條件,則默認(rèn)對(duì)A表進(jìn)行全表更新,但由于(selectb.city_namefromtmp_cust_citybwherewhereb.customer_id=a.customer_id)有可能不能提供"足夠多"值,因?yàn)閠mp_cust_city只是一部分客戶的信息,所以報(bào)錯(cuò)(如果指定的列--city_name可以為NULL則另當(dāng)別論):01407,00000,"cannotupdate(%s)toNULL"http://*Cause://*Action:一個(gè)替代的方法可以采用:updatecustomersa--使用別名setcity_name=nvl((selectb.city_namefromtmp_cust_citybwhereb.customer_id=a.customer_id),a.city_name)或者setcity_name=nvl((selectb.city_namefromtmp_cust_citybwhereb.customer_id二a.customer_id),'未知')--當(dāng)然這不符合業(yè)務(wù)邏輯了上述3)在一些情況下,因?yàn)锽表的紀(jì)錄只有A表的20-30%的紀(jì)錄數(shù),考慮A表使用INDEX的情況,使用cursor也許會(huì)比關(guān)聯(lián)update帶來(lái)更好的性能:setserveroutputondeclarecursorcity_curisselectcustomer_id,city_namefromtmp_cust_cityorderbycustomer_id;beginformy_curincity_curloopupdatecustomerssetcity_name=my_cur.city_namewherecustomer_id=my_cur.customer_id;/**此處也可以單條/分批次提交,避免鎖表情況**/-- ifmod(city_cur%rowcount,10000)=0then-- dbms_output.put_line(' ');-- commit;-- endif;endloop;end;關(guān)聯(lián)update的一個(gè)特例以及性能再探討在oracle的update語(yǔ)句語(yǔ)法中,除了可以u(píng)pdate表之外,也可以是視圖,所以有以下1個(gè)特例:update(selecta.city_name,b.city_nameasnew_namefromcustomersa,tmp_cust_citybwhereb.customer_id=a.customer_id)setcity_name=new_name這樣能避免對(duì)B表或其索引的2次掃描,但前提是A(customer_id)b(customer_id)必需是uniqueindex或primarykey。否則報(bào)錯(cuò):01779,00000,"cannotmodifyacolumnwhichmapstoanonkey-preservedtable"http://*Cause:Anattemptwasmadetoinsertorupdatecolumnsofajoinviewwhich//maptoanon-key-preservedtable.//*Action:Modifytheunderlyingbasetablesdirectly.oracle另一個(gè)常見(jiàn)錯(cuò)誤回到3)情況,由于某些原因,tmp_cust_citycustomer_id不是唯一index/primarykeyupdatecustomersa--使用別名setcity_name=(selectb.city_namefromtmp_cust_citybwhereb.customer_id=a.customer_id)whereexists(select1fromtmp_cust_citybwhereb.customer_id=a.customer_id)當(dāng)對(duì)于一個(gè)給定的a.customer_id(selectb.city_namefromtmp_cust_citybwhereb.customer_id=a.customer_id)返回多余1條的情況,則會(huì)報(bào)如下錯(cuò)誤:01427,00000,"single-rowsubqueryreturnsmorethanonerow"http://*Cause://*Action:一個(gè)比較簡(jiǎn)單近似于不負(fù)責(zé)任的做法是updatecustomersa--使用別名setcity_name=(selectb.city_namefromtmp_cust_citybwhereb.customer_id=a.customer_idandrownum=1)如何理解01427錯(cuò)誤,在一個(gè)很復(fù)雜的多表連接update的語(yǔ)句,經(jīng)常因考慮不周,出現(xiàn)這個(gè)錯(cuò)誤,仍已上述例子來(lái)描述,一個(gè)比較簡(jiǎn)便的方

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論