select for update和select for update wait和select for update nowait的区别
CREATE TABLE "TEST6"
(
"ID" VARCHAR2(30),
"NAME" VARCHAR2(30),
"AGE" NUMBER(2,0),
"SEX" VARCHAR2(2),
"ENAME" VARCHAR2(30),
"ADDTIME" DATE
)
insert into TEST6 (id, name, age, sex, ename, addtime) values ('', '张三', 18, null, 'zhangsan', to_date('14-03-2017 00:00:09', 'dd-mm-yyyy hh24:mi:ss'));
insert into TEST6 (id, name, age, sex, ename, addtime) values ('', '李四', null, '', 'Lisi', to_date('01-03-2017 02:00:00', 'dd-mm-yyyy hh24:mi:ss'));
insert into TEST6 (id, name, age, sex, ename, addtime) values ('', '王五', 20, '', 'wangwu', to_date('09-01-2017 08:55:00', 'dd-mm-yyyy hh24:mi:ss'));
insert into TEST6 (id, name, age, sex, ename, addtime) values ('', '赵六', 23, '', 'zhaoliu', to_date('03-03-2016 04:00:00', 'dd-mm-yyyy hh24:mi:ss'));
insert into TEST6 (id, name, age, sex, ename, addtime) values ('', '冯七', 22, null, 'fengqi', to_date('08-03-2017 12:00:01', 'dd-mm-yyyy hh24:mi:ss'));
CREATE TABLE "TEST8"
(
"ID" NUMBER,
"ORDERID" NUMBER,
"PRODUCTID" NUMBER,
"PRICE" NUMBER(10,2),
"QUANTITY" NUMBER
)
insert into TEST8 (id, orderid, productid, price, quantity) values (1, 1, 1, 6, 10);
insert into TEST8 (id, orderid, productid, price, quantity) values (2, 1, 2, 4, 5);
insert into TEST8 (id, orderid, productid, price, quantity) values (3, 1, 3, 10, 2);
insert into TEST8 (id, orderid, productid, price, quantity) values (4, 2, 1, 3, 6);
insert into TEST8 (id, orderid, productid, price, quantity) values (5, 2, 2, 4, 6);
以上是基础数据
在oracle中,如果只进行select语句的话,是不会进行加锁的,也就是oracle会返回当前时刻的结果集,即使这个时候可能有另外一个进程在修改当前结果集的数据,因为没有加锁,所以oracle还是会正常的返回当前时刻的结果集,不会有任何影响。
他们三个共同点:
当使用select for update 或者select for update wait或者.....,那么oralce会给符合where条件的数据行加上一个行级锁
1、select for update
但是如果你的select 语句加了for update,那么就不是上面这回事了,当oracle发现select的当前结果集中的一条或多条正在被修改(注意:当数据被修改时,此时的数据行是被加锁的),那么他就会等到当前当前结果集被修改完毕并且commit之后才进行select操作,并对结果集进行加锁。同样的,如果查询语句发出后,其他会话需要修改结果集中的一条(或几条数据)也许要等到查询结束(commit)之后,才可以执行修改操作。
代码如下:
新建SQL窗口1,(相当于新建一个session会话)
select * from test8 for update

for update 对整个结果集进行了加锁,意味着在当前session进行commit之前,任何其他的session进行update、delete、insert操作都会进行等待
新建SQL窗口2(相当于新建一个session会话)
update test8 set price=6 where ID=1

显示执行中,等待会话一的查询执行完成
现在我们将会话一的事务提交(commit)

会话二的update语句执行成功
2、select for update nowait
for update和for update nowait都会对查询到的当前结果集进行加锁,所不同的是,当有另外的会话在修改当前结果集中的数据,select for nowait所进行的查询操作不会进行等待,当发现结果集中的一些数据被加锁,立刻返回 “ORA-00054错误,内容是资源正忙, 但指定以 NOWAIT 方式获取资源”。测试代码如下:
新建一个SQL窗口1(相当于新建一个会话)
update test8 set price=3 where ID=1
更新test8表的一条数据,但是不进行commit操作
然后新建SQL窗口2(相当于新建一个会话)select for update nowait操作
select * from test8 for update nowait

总结分析:
因为会话一,并没有commit所以test8中的ID=1的行被加锁了,所以当会话二进行select for update nowait检索到ID=1的数据行被加锁了,就立刻返回 “ORA-00054错误,内容是资源正忙, 但指定以 NOWAIT 方式获取资源”的错误。
接下来我们对会话一进行commit操作,

在执行会话二的select查询,ok,可以查出来了,并且对当前数据集进行了加锁操作,其他会话想要进行修改操作,必须等到会话二commit之后

3、select for update wait
它也会对查询到的结果集进行加锁,select for update wait与select for update nowait不同的地方是,当有另外的会话对它的查询结果集中的某一行数据进行了加锁,那么它不会像nowait一样,立即返回"ORA-00054错误",而是它支持一个参数,设定等待的时间,当超过了设定的时间,那一行数据还处于加锁的状态,那么它也会返回“ORA-00054错误,内容是资源正忙, 但指定以 NOWAIT 方式获取资源”。测试代码如下:
首先新建SQL窗口1(相当于新建一个会话)执行update 语句,但是不进行commit操作,那么当前数据行将被lock
update test8 set price=3 where ID=1
接着新建SQL窗口2(相当于新建一个会话),在执行select for update wait 6,如果当前查询检索的数据集中,有被加锁了的行数据,那么等待6秒,如果6秒后,其他会话,还没有执行commit释放被加了锁的数据行的话,那么返回“ORA-00054错误,内容是资源正忙, 但指定以 NOWAIT 方式获取资源”。
select * from test8 for update wait 6

执行语句6秒后,报错。
最后对会话一(SQL窗口一)进行commit操作

紧接着执行会话二(SQL窗口二)中的sql语句,此时被加锁的数据行被释放

正常的检索除了数据行,当时当前数据集被加锁,其他会话想操作此数据集,必须等会话二中的事务commit之后,才可以进行修改
4、OF子句
在多表查询中如果需要对多表查询的结果集进行加锁,可以使用OF子句。
如果存在OF子句,那么就对满足OF子句的单表进行加锁,如果不存在OF子句就对整个结果集进行加锁,代码如下:
a、不使用OF子句
select a.ID,a.Name,b.price from test6 a
LEFT JOIN test8 b
ON a.ID=b.ID where b.ID>3
for update

没有进行commit操作,此时对test6和test8中的ID>3的数据行都进行了加锁,测试代码如下:
新建一个会话,执行以下语句:
select * from test6 for update skip locked

select * from test8 for update skip locked

测试结果证明,在没有OF子句的情况下,对多表查询的结果集进行select foe update,oracle会对满足where 条件的所有数据行进行加锁
b、使用OF子句
使用OF子句,那么oracle就会对满足OF子句的表进行加锁,在多表查询中。代码如下:
select a.ID,a.Name,b.price from test6 a
LEFT JOIN test8 b
ON a.ID=b.ID where b.ID>3
for update of a.ID

在不执行commit操作的情况,新建一个会话,执行一下语句:
select * from test6 for update skip locked

select * from test8 for update skip locked

比对测试结果,发现在OF子句的作用下,oracle对同时满足where子句(设置要加锁的数据行)和OF子句(主要设置加锁的表)的数据行进行了加锁。
select for update和select for update wait和select for update nowait的区别的更多相关文章
- 数据库(update tab1 set tab1.name=tab1.name+(select t2.name from tab2 t2 where t2.id=tab1.id))
有t1 和 t2 两个表,表中的数据和字段如下: 执行 如下SQL语句: update tab1 set tab1.name=tab1.name+(select t2.name from tab2 t ...
- [转]oracle for update和for update nowait的区别
1概念小结:(针对以下引用区域内容) 1.1 普通select语句不加锁. 1.2 for update和for update nowait都试图将符合条件的数据加上行级锁.用于排斥其他针对这个表的写 ...
- Oracle 中 for update 和 for update nowait 的区别
原文出处http://bijian1013.iteye.com/blog/1895412 一.for update 和 for update nowait 的区别 首先一点,如果只是select 的话 ...
- sql: oracle, for update和for update nowait的区别
1. oracle for update和for update nowait的区别 http://www.cnblogs.com/quanweiru/archive/2012/11/09/276222 ...
- oracle for update和for update nowait的区别 - 转
1.for update 和 for update nowait 的区别: 首先一点,如果只是select 的话,Oracle是不会加任何锁的,也就是Oracle对 select 读到的数据不会有任何 ...
- oracle for update和for update nowait 的区别
原文地址:http://www.cnblogs.com/quanweiru/archive/2012/11/09/2762223.html 1.for update 和 for update nowa ...
- Oracle 中for update和for update nowait的区别
http://www.cnblogs.com/quanweiru/archive/2012/11/09/2762223.html 1.for update 和 for update nowait 的区 ...
- 下拉框——把一个select框中选中内容移到另一个select框中遇到的问题
在使用jQuery实现把一个select框中选中内容移到另一个select框中功能时遇到了一个问题,就是点击按钮时内容可以到另一个select框中,但是到了另一个select框中的内容却很快闪退回原来 ...
- Mysql update error: Error Code: 1175. You are using safe update mode and you tried to update a table
Mysql update error: Error Code: 1175. You are using safe update mode and you tried to update a table ...
- select change事件给其它元素赋值,本select的value或tex
select change事件给其它元素赋值,本select的value或textonchange='$("#areaname").val($("option:selec ...
随机推荐
- 编写高质量代码改善C#程序的157个建议——建议122:以<Company>.<Component>为命名空间命名
建议122:以<Company>.<Component>为命名空间命名 建议以<Company>.<Component>为程序集命名,比如Microso ...
- MySQL查询表内重复记录并删除
在日常业务场景中,经常会出现一个问题就是解决数据重复的问题,这里用到了一张用户表(s_user)做重复数据操作,分别包含了两个字段,id.name分别用于做唯一标示以及相同姓名的检索. 表结构以及测试 ...
- Web开发入门经典:使用PHP6、Apache和MySQL 中文pdf扫描版
通过学习本书,读者很快就能明白为什么PHP.Apache和MySQL会迅速成为开发动态网站最流行的方式,本书将为读者理解这3个核心组件如何独立工作和协同工作奠定良好的基础,引导读者充分利用它们提供的各 ...
- 在Centos 7 上面 安装MySQL 5.7 简录
In a web browser, visit mysql.com page: https://dev.mysql.com/downloads/repo/yum/ Locate the desired ...
- Intel Galileo Debian Image Prequits
Intel Galileo开发板 Debian镜像 在原发布者的基础上进行了更新,附带开发入门套件,打包内容: -intel_galileo_debian_xfce4镜像 -约3GB -putty - ...
- border使用小技巧
border-style 分类 dashed虚线类型 dotted 点线类型 double 双线类型 双线型量根实线的宽度和中间空白区域的间距有一定规律: 可以利用这个规律画出一些特殊的图案 代码如下 ...
- Logiccode GSM SMS .Net Library 3.3
下载 Mega 百度 密码:5pvb
- [ActionScript 3.0] 使用Embed在类中嵌入字体
package { import flash.display.Sprite; import flash.text.Font; import flash.text.TextField; import f ...
- [sloved] IDE JavaServlet "Error: Could not find or load main class Servlet"
[ sloved ] JavaServlet "Error: Could not find or load main class Servlet" 报错内容: 提供一份 Servl ...
- Archlinux下virtualbox报错'/sbin/rcvboxdrv setup'
因为刚刚换成archlinux系统,安装virtualbox的时候报错了.如下图: 可是怎么解决呢?我看了很多资料大多数是ubuntu的,没有archlinux的. 但是原理都差不多我借着也就研究出来 ...