oracle update數(shù)據(jù)更新的實(shí)現(xiàn)語(yǔ)句
來(lái)源:易賢網(wǎng) 閱讀:7556 次 日期:2014-09-16 16:02:00
溫馨提示:易賢網(wǎng)小編為您整理了“oracle update數(shù)據(jù)更新的實(shí)現(xiàn)語(yǔ)句”,方便廣大網(wǎng)友查閱!

oracle update數(shù)據(jù)更新的實(shí)現(xiàn)語(yǔ)句

SQL> -- create demo table

SQL> create table Employee(

2 ID VARCHAR2(4 BYTE) NOT NULL,

3 First_Name VARCHAR2(10 BYTE),

4 Last_Name VARCHAR2(10 BYTE),

5 Start_Date DATE,

6 End_Date DATE,

7 Salary Number(8,2),

8 City VARCHAR2(10 BYTE),

9 Description VARCHAR2(15 BYTE)

10 )

11 /

Table created.

SQL>

SQL> -- prepare data

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

2 values ('01','Jason', 'Martin', to_date('19960725','YYYYMMDD'), to_date('20060725','YYYYMMDD'), 1234.56, 'Toronto', 'Programmer')

3 /

1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

2 values('02','Alison', 'Mathews', to_date('19760321','YYYYMMDD'), to_date('19860221','YYYYMMDD'), 6661.78, 'Vancouver','Tester')

3 /

1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

2 values('03','James', 'Smith', to_date('19781212','YYYYMMDD'), to_date('19900315','YYYYMMDD'), 6544.78, 'Vancouver','Tester')

3 /

1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

2 values('04','Celia', 'Rice', to_date('19821024','YYYYMMDD'), to_date('19990421','YYYYMMDD'), 2344.78, 'Vancouver','Manager')

3 /

1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

2 values('05','Robert', 'Black', to_date('19840115','YYYYMMDD'), to_date('19980808','YYYYMMDD'), 2334.78, 'Vancouver','Tester')

3 /

1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

2 values('06','Linda', 'Green', to_date('19870730','YYYYMMDD'), to_date('19960104','YYYYMMDD'), 4322.78,'New York', 'Tester')

3 /

1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

2 values('07','David', 'Larry', to_date('19901231','YYYYMMDD'), to_date('19980212','YYYYMMDD'), 7897.78,'New York', 'Manager')

3 /

1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date, End_Date, Salary, City, Description)

2 values('08','James', 'Cat', to_date('19960917','YYYYMMDD'), to_date('20020415','YYYYMMDD'), 1232.78,'Vancouver', 'Tester')

3 /

1 row created.

SQL>

SQL>

SQL>

SQL> -- display data in the table

SQL> select * from Employee

2 /

ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION

---- ---------- ---------- --------- --------- ---------- ---------- ---------------

01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer

02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester

03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester

04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager

05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester

06 Linda Green 30-JUL-87 04-JAN-96 4322.78 New York Tester

07 David Larry 31-DEC-90 12-FEB-98 7897.78 New York Manager

08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester

8 rows selected.

SQL>

SQL>

SQL>

SQL>

SQL>

SQL>

SQL>

SQL> --Modify multiple rows with a single UPDATE statement;

SQL>

SQL>

SQL> UPDATE Employee

2 SET City ='L.A.'

3 WHERE City = 'New York';

2 rows updated.

SQL>

SQL> select * from employee;

ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION

---- ---------- ---------- --------- --------- ---------- ---------- ---------------

01 Jason Martin 25-JUL-96 25-JUL-06 1234.56 Toronto Programmer

02 Alison Mathews 21-MAR-76 21-FEB-86 6661.78 Vancouver Tester

03 James Smith 12-DEC-78 15-MAR-90 6544.78 Vancouver Tester

04 Celia Rice 24-OCT-82 21-APR-99 2344.78 Vancouver Manager

05 Robert Black 15-JAN-84 08-AUG-98 2334.78 Vancouver Tester

06 Linda Green 30-JUL-87 04-JAN-96 4322.78 L.A. Tester

07 David Larry 31-DEC-90 12-FEB-98 7897.78 L.A. Manager

08 James Cat 17-SEP-96 15-APR-02 1232.78 Vancouver Tester

以下所列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 說(shuō)完了,下面寫一下關(guān)于for update,for update of

下面的資料是從網(wǎng)上找到的,可是具體的網(wǎng)址現(xiàn)在找不到了,請(qǐng)?jiān)徯〉艿拇中模萌思业臇|東而不寫出處.

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

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

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

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

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

d. select * from test a join t on a.i_d=t.dept_id where a.i_d=1 for update; 這樣則會(huì)鎖定滿足條件的數(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ù)行,而不會(huì)鎖定表t中的數(shù)據(jù),因?yàn)橹霸趐rocedure中作一個(gè)update,而需要update的數(shù)據(jù)需要關(guān)聯(lián)查詢,所以用了for update造成其他用戶更新造成阻塞,所以才查到這段資料.

for update of 是一個(gè)行級(jí)鎖,這個(gè)行級(jí)鎖,開始于一個(gè)cursor 打開時(shí),而終止于事務(wù)的commit或rollback,而并非cursor的close.

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

執(zhí)行如下代碼就會(huì)死鎖(在兩個(gè)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;

/

注意兩個(gè)pl/sql塊中沒有commit;

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看數(shù)據(jù)庫(kù)
易賢網(wǎng)手機(jī)網(wǎng)站地址:oracle update數(shù)據(jù)更新的實(shí)現(xiàn)語(yǔ)句
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)