1. 程式人生 > >oracle 增刪改查常技巧:

oracle 增刪改查常技巧:

oracle 更新的三個方法:
1、T2表的SEX等於T2表的SEX。這個方法只能對T2表進行WHERE更新,不能對T1表進行WHERE更新
update temp2 t2 set t2.sex=(select t1.sex from temp1 t1 where t1.id=t2.id)
where t2.age>50


2、T2表的SEX等於T2表的SEX。這個方法可同時對T1、T2表進行WHERE更新,且要更新的T2表中資料必須在T1中存在
update temp2 t2 set t2.sex=(select t1.sex from temp1 t1 where t1.id=t2.id)
where exists (select  1 from  temp1 t1 where t1.id=t2.id and t1.age>50 and t2.age>50) 


3、T2表的SEX等於T2表的SEX。這個方法可同時對T1、T2表進行WHERE更新,且要更新的T2表中資料必須在T1中存在
(這裡由於使用了中間記憶體表,因此可以很方便地改變兩表左、右、全、內連線的方法來控制要更新的T2表中的資料範圍)
update (select t1.sex s1,t2.sex s2 from temp1 t1,temp2 t2 where t1.id=t2.id and t1.age>50 and t2.age>50)
set s2=s1

(注意:這個寫法必須要求兩張表關鍵條件的欄位有唯一約束或主鍵約束,這也是為了保證查詢的結果必須是一對一關係)

----未完待續-----