一级女人毛片人一女人-一级女性大黄生活片免费-一级女性全黄久久生活片-一级女性全黄生活片免费-国产美女在线一区二区三区-国产美女在线观看

始創于2000年 股票代碼:831685
咨詢熱線:0371-60135900 注冊有禮 登錄
  • 掛牌上市企業
  • 60秒人工響應
  • 99.99%連通率
  • 7*24h人工
  • 故障100倍補償
您的位置: 網站首頁 > 幫助中心>文章內容

關于Oracle中update

發布時間:  2012/8/21 16:58:03

前幾天用到Oracle數據庫的update更新,對于這個簡單問題,卻出現了不少問題,所以現將從網上搜索資料及自已的總結羅列在此以備一時之用.

以下所列sql都是基于下表

create table test (name  varchar2(30),code varchar2(10),i_d varchar2(10));

插入數據

-
 
insert into test(name,code,i_d) values('zhu1','001','1');
insert into test(name,code,i_d) values('zhu2','002','2');
insert into test(name,code,i_d) values('zhu3','003','3');
commit;
select * from test s;

1. update 更新i_d為1的數據

--方式1
update test  set name='zhurhyme1',
code='007' where i_d='1';
commit;

這樣可以成功

--方式2

update test set (name,code)=(
'zhurhyme2','007')
where i_d='1';

注意,這樣是不行,update set 必須為子查詢,所以需要將其改為 :

--方式3

update test set (name,code)=(
select 'zhurhyme3','007' from dual)
where i_d='1';

commit;

2.update 說完了,下面寫一下關于for update,for update of

 下面的資料是從網上找到的,可是具體的網址現在找不到了,請原諒小弟的粗心,引用人家的東東而不寫出處.

for update 經常用,而for updade of 卻不常用,現在將這兩個作一個區分

a.    select * from test for update 鎖定表的所有行,只能讀不能寫

b.  select * from test where i_d = 1 for update 只鎖定i_d=1的行,對于其他的表的其他行卻不鎖定

下面再創建一個表

create table t (dept_id  varchar(10),dept_name varchar2(50));

c.  select * from test  a join t on a.i_d=t.dept_id for update;  這樣則會鎖定兩張表的所有數據

d.  select * from test  a join t on a.i_d=t.dept_id where a.i_d=1 for update;  這樣則會鎖定滿足條件的數據

e.  select * from test  a join t on a.i_d=t.dept_id where a.i_d=1 for update of a.i_d; 注意區分 d與e,e只分鎖定表test中滿足條件的數據行,而不會鎖定表t中的數據,因為之前在procedure中作一個update,而需要update的數據需要關聯查詢,所以用了for update造成其他用戶更新造成阻塞,所以才查到這段資料.

 for update of 是一個行級鎖,這個行級鎖,開始于一個cursor 打開時,而終止于事務的commit或rollback,而并非cursor的close.

如果有兩個cursor對于表的同一行記錄同時進行update,實際上只有一個cursor在執行,而另外一個一直在等待,直至另一個完成,它自己再執行.如果第一個cursor不能被很好的處理,第二個cursor也不主動釋放資源,則死鎖就此產生.

執行如下代碼就會死鎖(在兩個command window中執行)

declare
 cursor cur_test
   is
   select name,code from test where i_d=1 for update of name;
begin

   for rec in cur_test loop
      update test set name='TTTT1' where current of cur_test;
 end loop;
end;
/

declare
 cursor cur_test
   is
   select name,code from test where i_d=1 for update of name;
begin

   for rec in cur_test loop
      update test set name='TTTT2' where current of cur_test;
 end loop;
end;
/

注意兩個pl/sql塊中沒有commit;

為了解決這個死鎖問題,要么就是第一個塊釋放資源,要么就是第二塊主動放棄.第一次釋放資源很簡單,那就執行commit或rollback;而讓第二塊主動放棄,在for update 后加no wait;這樣就會報

ORA-00054 [resource busy and acquire with NOWAIT specified 的錯誤,這樣就沒有死鎖了.


本文出自:億恩科技【www.laynepeng.cn】

服務器租用/服務器托管中國五強!虛擬主機域名注冊頂級提供商!15年品質保障!--億恩科技[ENKJ.COM]

  • 您可能在找
  • 億恩北京公司:
  • 經營性ICP/ISP證:京B2-20150015
  • 億恩鄭州公司:
  • 經營性ICP/ISP/IDC證:豫B1.B2-20060070
  • 億恩南昌公司:
  • 經營性ICP/ISP證:贛B2-20080012
  • 服務器/云主機 24小時售后服務電話:0371-60135900
  • 虛擬主機/智能建站 24小時售后服務電話:0371-60135900
  • 專注服務器托管17年
    掃掃關注-微信公眾號
    0371-60135900
    Copyright© 1999-2019 ENKJ All Rights Reserved 億恩科技 版權所有  地址:鄭州市高新區翠竹街1號總部企業基地億恩大廈  法律顧問:河南亞太人律師事務所郝建鋒、杜慧月律師   京公網安備41019702002023號
      0
     
     
     
     

    0371-60135900
    7*24小時客服服務熱線