oracle兩種更新update和merge

2021-10-08 09:33:34 字數 976 閱讀 1623

update和merge只用於更新時,倆種方式是可互更換的

update適用於

1、更改單錶速度快穩定性好;

2、某字段即是過濾條件又是更新字段,且該字段有選擇性很強的索引時「update a set status=1 where  id=1 and status=2 and idc in (表)」

merge適用於

1、根據一張表或多表聯合查詢的連線條件對另外一張表進行查詢,連線條件匹配上的進行update,無法匹配的執行insert可直接用merge實現,執行效率要高於insert+update;

2、「update a set i=(select i from b where a.id=b.id) where not exists (select 1 from b where a.id=b.id)」not exists部分需要額外消耗,可以用merge避免;

merge不支援更改字段放入on【會報錯 ora-38104: 無法更新 on 子句中引用的列: xx】

多表處理:

update d d set

(d.number)=(

select i.number from i i where i.code in (

select code from d where number = '123456')

)where exists(

select 1 from i i where d.code=i.code);

merge into i a

using (select i.number,i.code,i.name from i t,i i

where t.code=i.code and t.number <> i.number and t.type='01') c

on (a.code=c.code)

when matched then

update set a.number=c.number,a.name=c.name;

兩種丟失更新的區別

第一類丟失更新 通過設定隔離級別可以防止 repeatable read a事務撤銷時,把已經提交的b事務的更新資料覆蓋了。這種錯誤可能造成很嚴重的問題,通過下面的賬戶取款轉賬就可以看出來 時間取款事務a 轉賬事務b t1 開始事務 t2 開始事務t3 查詢賬戶餘額為1000元 t4 查詢賬戶餘額為...

oracle兩種認證方式總結

oracle 資料庫通過 sqlnet.ora 檔案中的引數 sqlnet.authentication services,引數檔案中的 remote login passwordfile 和口令檔案 pwdsid.ora 三者協同作用實現身份認證 sqlnet.authentication ser...

Oracle執行有兩種方式

oracle資料有兩種方式 1 歸檔方式 archivelog 歸檔方式的目的在於當資料庫發生故障時最大限度恢復資料庫,保以保證不丟失任何已經提交的資料 2 不歸檔方 noarchivelog 只能恢復資料庫到最近的 點 冷備份或者邏輯備份 資料丟失是非常可能的。改變不歸檔方式為歸檔方式 切換資料的...