select * from table_name for update; 和 select t.*, t.rowid from table_name t的区别

来源:互联网 发布:网络金融诈骗公司名单 编辑:程序博客网 时间:2024/05/16 06:00

select * from table_name for update; 和 select t.*, t.rowid from table_name t 的区别

前者会对你查询出来的结果加上锁,而后者就没有这样的机制;

此时如果有另一个session过来去对你结果集中的数据进行更新或者删除操作,则需要等到第一个事务提交或者回滚后,才能够执行该语句;

例如:

create table temp_yaosht(col1 varchar2(32));

insert into temp_yaosht values('a');

insert into temp_yaosht values('b');

insert into temp_yaosht values('c');

insert into temp_yaosht values('d');

insert into temp_yaosht values('e');

第一个session的sql语句

select  * 

   from temp_yaosht

where col1 in ('a','b','c') for update;

第二session的sql语句

update temp_yaosht

     set col1='aa'

where col1='a';

第三session的sql语句

update temp_yaosht

     set col1='dd'

where col1='d';

先执行第一个sql语句;然后当我们执行第二个sql的时候,会发现sql一直处于执行状态,只有当第一个sql的事务提交之后,第二个sql才会执行,且结束。

第三个sql的执行并不受影响,因为第一个sql的只对‘a’,'b','c'这三条记录加了锁,而不是对该表加的锁。


select t.*, t.rowid from table_name t 则没有这样的限制。

0 0