oracle中数据更新语句怎么写,oracleupdate数据更新的实现语
oracle update数据更新的实现语句
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));
插⼊数据
insert into test(name,code,i_d) values('zhu1','001','1');
insert into test(name,code,i_d) values('zhu2','002','2');
sql中update什么意思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;
更多信息请查看IT技术专栏

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。