国产福利在线播放|久久精品福利网站免费|国产呻吟视频在线观看|日韩一区二区三区免费高清|久996视频精品免费观看|欧美日本在线一区二区三区|在线最新无码经典无码免費資訊|国产午夜亚洲精品国产成人最大

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

關(guān)于Oracle中update

發(fā)布時間:  2012/8/21 16:58:03

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

以下所列sql都是基于下表

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

插入數(shù)據(jù)

-
 
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的數(shù)據(jù)

--方式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 說完了,下面寫一下關(guān)于for update,for update of

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

for update 經(jīng)常用,而for updade of 卻不常用,現(xiàn)在將這兩個作一個區(qū)分

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

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

下面再創(chuàng)建一個表

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;  這樣則會鎖定兩張表的所有數(shù)據(jù)

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

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; 注意區(qū)分 d與e,e只分鎖定表test中滿足條件的數(shù)據(jù)行,而不會鎖定表t中的數(shù)據(jù),因為之前在procedure中作一個update,而需要update的數(shù)據(jù)需要關(guān)聯(lián)查詢,所以用了for update造成其他用戶更新造成阻塞,所以才查到這段資料.

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

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

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

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;

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

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


本文出自:億恩科技【www.riomediacenter.com】

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

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

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