select for update

有些时候对于要更新的数据,为了防止其他查询同时修改该数据会用到select for update,即锁定要更新的记录,等待整个事务提交后,才允许其他请求操作该数据。

需注意: for update 会锁定行或者表

区别在于:

当for update的字段为索引或者主键的时候,只会锁住索引或者主键对应的行。

select * from table where id = 1 for udpate  锁定主键id为1的行

select * from table 锁定整张表

原文地址:https://www.cnblogs.com/flysand/p/11188587.html