美文网首页
Oracle中for update的用法

Oracle中for update的用法

作者: innerClass | 来源:发表于2016-08-09 09:08 被阅读0次

语法:

<pre>
SELECT ......FOR UPDATE [ OF column_list ] [ WAIT n ] [ SKIP LOCKED ];
</pre>其中OF字句用于指定即将更新的列,即锁定行上的特定列;WAIT子句指定等待其他用户释放锁的秒数,防止无限期的等待。

使用“FORUPDATE WAIT”子句的优点如下:

1、防止无限期地等待被锁定的行;
2、允许应用程序中对锁的等待时间进行更多的控制。
3、对于交互式应用程序非常有用,因为这些用户不能等待不确定
4、若使用了skip locked,则可以越过锁定的行,不会报告由wait n引发的‘资源忙’异常报告

举例:

1)、
<pre>
create table t(a varchar(20) ,b varchar(20));

select * from t;

insert into t values (1,1);
insert into t values (2,3);
insert into t values (3,3);
insert into t values (4,4);
insert into t values (5,5);
insert into t values (6,6);

select * from t for update;
</pre>

图20168983719.png

在窗口2中执行

<pre>select * from t where a='1' for update;</pre>

发现无法查询出结果且PLSQLDeveloper的执行按钮一直为灰色。
这是因为表被窗口1里的语句锁住了,窗口2处于等待状态。
只有等窗口1中提交了事务之后才能在窗口2中正常执行上述语句。
在窗口1中点击提交事务的按钮后,窗口2中立马显示出正常结果。
2)、
<pre>
select * from t for update;
select * from t for update nowait;
select * from t for update wait 6;
</pre>


图20168984640.png
图20168985022.png

3)、
<pre>
select * from t where rownum<=3 for update skip locked;
select * from t where rownum<=6 for update skip locked;
</pre>

图20168985641.png

可见前三条数据因被窗口1锁住而没有查出来。

相关文章

网友评论

      本文标题:Oracle中for update的用法

      本文链接:https://www.haomeiwen.com/subject/fwarsttx.html