日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ORACLE多表关联UPDATE 语句

發布時間:2023/12/31 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ORACLE多表关联UPDATE 语句 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

GPS平臺、網站建設、軟件開發、系統運維,找森大網絡科技!
https://cnsendnet.taobao.com
來自森大科技官方博客
http://www.cnsendblog.com/index.php/?p=1197

為了方便起見,建立了以下簡單模型,和構造了部分測試數據:
在某個業務受理子系統BSS中,

1.--客戶資料表 2.create table customers 3. ( 4. customer_id number(8) not null, -- 客戶標示 5.city_name varchar2(10) not null, -- 所在城市 6.customer_type char(2) not null, -- 客戶類型 7.... 8. ) 9.create unique index PK_customers on customers (customer_id)

由于某些原因,客戶所在城市這個信息并不什么準確,但是在
客戶服務部的CRM子系統中,通過主動服務獲取了部分客戶20%的所在
城市等準確信息,于是你將該部分信息提取至一張臨時表中:

1.create table tmp_cust_city 2. ( 3. customer_id number(8) not null, 4. citye_name varchar2(10) not null5.customer_type char(2) not null 6. )
  • 最簡單的形式
  • 1.--經確認customers表中所有customer_id小于1000均為'北京' 2.--1000以內的均是公司走向全國之前的本城市的老客戶:) 3.update customers 4.set city_name='北京' 5.where customer_id<1000
  • 兩表(多表)關聯update – 僅在where字句中的連接
  • 1.--這次提取的數據都是VIP,且包括新增的,所以順便更新客戶類別 2.update customers a -- 使用別名 3.set customer_type='01' --01 為vip,00為普通 4.where exists (select 1 5.from tmp_cust_city b 6.where b.customer_id=a.customer_id 7. )
  • 兩表(多表)關聯update – 被修改值由另一個表運算而來
  • 1.update customers a -- 使用別名 2.set city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id) 3.where exists (select 1 4.from tmp_cust_city b 5.where b.customer_id=a.customer_id 6. ) 7.-- update 超過2個值 8.update customers a -- 使用別名 9.set (city_name,customer_type)=(select b.city_name,b.customer_type 10.from tmp_cust_city b 11.where b.customer_id=a.customer_id) 12.where exists (select 1 13.from tmp_cust_city b 14.where b.customer_id=a.customer_id 15. )

    注意在這個語句中,
    =(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表進行全表
    更新,但由于

    1.select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id

    有可能不能提供"足夠多"值,因為tmp_cust_city只是一部分客戶的信息,所以報錯(如果指定的列–city_name可以為NULL則另當別論):

    1.01407, 00000, "cannot update (%s) to NULL" 2. // *Cause: 3. // *Action:

    一個替代的方法可以采用:

    1.update customers a -- 使用別名 2.set city_name=nvl((select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id),a.city_name)

    或者

    1.set city_name=nvl((select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id),'未知')

    – 當然這不符合業務邏輯了

  • 上述3)在一些情況下,因為B表的紀錄只有A表的20-30%的紀錄數,
    考慮A表使用INDEX的情況,使用cursor也許會比關聯update帶來更好的性能:
  • 1.set serveroutput on 2. 3.declare 4.cursor city_cur is 5.select customer_id,city_name 6.from tmp_cust_city 7.order by customer_id; 8.begin 9.for my_cur in city_cur loop 10. 11.update customers 12.set city_name=my_cur.city_name 13.where customer_id=my_cur.customer_id; 14. 15. /** 此處也可以單條/分批次提交,避免鎖表情況 **/ 16.-- if mod(city_cur%rowcount,10000)=0 then 17.-- dbms_output.put_line('----'); 18.-- commit; 19.-- end if; 20.end loop; 21.end;
  • 關聯update的一個特例以及性能再探討
    在oracle的update語句語法中,除了可以update表之外,也可以是視圖,所以有以下1個特例:
  • 1.update (select a.city_name,b.city_name as new_name 2.from customers a, 3. tmp_cust_city b 4.where b.customer_id=a.customer_id 5. ) 6.set city_name=new_name

    這樣能避免對B表或其索引的2次掃描,但前提是 A(customer_id) b(customer_id)必需是unique index或primary key。否則報錯:

    1.01779, 00000, "cannot modify a column which maps to a non key-preserved table" 2. // *Cause: An attempt was made to insert or update columns of a join view which 3. // map to a non-key-preserved table. 4. // *Action: Modify the underlying base tables directly.

    6)oracle另一個常見錯誤
    回到3)情況,由于某些原因,tmp_cust_city customer_id 不是唯一index/primary key

    1.update customers a -- 使用別名 2.set city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id) 3.where exists (select 1 4.from tmp_cust_city b 5.where b.customer_id=a.customer_id 6. )

    當對于一個給定的a.customer_id
    (select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)
    返回多余1條的情況,則會報如下錯誤:

    1.01427, 00000, "single-row subquery returns more than one row" 2. // *Cause: 3. // *Action:

    一個比較簡單近似于不負責任的做法是

    1.update customers a -- 使用別名 2.set city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id and rownum=1)

    如何理解 01427 錯誤,在一個很復雜的多表連接update的語句,經常因考慮不周,出現這個錯誤,
    仍已上述例子來描述,一個比較簡便的方法就是將A表代入 值表達式 中,使用group by 和
    having 字句查看重復的紀錄

    1.(select b.customer_id,b.city_name,count(*) 2.from tmp_cust_city b,customers a 3.where b.customer_id=a.customer_id 4.group by b.customer_id,b.city_name 5.having count(*)>=2 6. )

    GPS平臺、網站建設、軟件開發、系統運維,找森大網絡科技!
    https://cnsendnet.taobao.com
    來自森大科技官方博客
    http://www.cnsendblog.com/index.php/?p=1197

    總結

    以上是生活随笔為你收集整理的ORACLE多表关联UPDATE 语句的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。