MySQL中select中的for update 使用说明

栏目: 数据库 · Mysql · 发布时间: 7年前

内容简介:有时候我们会看到一些select语句后面紧跟一句for update,这里我们就介绍一下对for update的理解。for update是一个排它锁,也就是说对于select ... for update 这条语句所在的事务中可以进行任何操作,但其它事务中只能读取,不能进行update更新操作。为了更加方便理解,我们举例说明

有时候我们会看到一些select语句后面紧跟一句for update,这里我们就介绍一下对for update的理解。

for update是一个排它锁,也就是说对于select ... for update 这条语句所在的事务中可以进行任何操作,但其它事务中只能读取,不能进行update更新操作。

为了更加方便理解,我们举例说明

事务A:

start transaction;
select * from tb where id=1 for update;
update tb set name='xyz' where id=1;

此时另一个事务B:

start transaction;
//如果下面这条select语句也加上for update的话,此时会被阻塞,直到事务A提交或者回滚
select * from tb where id=1; 
//下面的 sql 语句会被阻塞,直到事务A进行提交或者回滚
update tb set name='123' where id=1;

这里id为表的主键。为行锁,对于其它主键值非1的不存在这种情况,只要两个事务操作的不是同一条记录就可以正常运行。


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Introduction to Algorithms, 3rd Edition

Introduction to Algorithms, 3rd Edition

Thomas H. Cormen、Charles E. Leiserson、Ronald L. Rivest、Clifford Stein / The MIT Press / 2009-7-31 / USD 94.00

Some books on algorithms are rigorous but incomplete; others cover masses of material but lack rigor. Introduction to Algorithms uniquely combines rigor and comprehensiveness. The book covers a broad ......一起来看看 《Introduction to Algorithms, 3rd Edition》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

html转js在线工具
html转js在线工具

html转js在线工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换