SELECT ... FOR UPDATE *的目的是什么? [英] What's the purpose of SELECT ... *FOR UPDATE*?
问题描述
我对为什么要指定FOR UPDATE
感到困惑-为什么数据库为什么要处理SELECT
中的数据呢?
I'm confused as to why you would specify FOR UPDATE
-- why does the database care what you're going to do with the data from the SELECT
?
对不起,我问的问题很差.我知道文档说这会将事情变成锁定读取"-我想知道的是在什么情况下,在指定FOR UPDATE
与不指定FOR UPDATE
之间,可观察到的行为会有所不同-也就是说,专门需要锁定吗?
Sorry, I asked the question poorly. I know the docs say that it turns things into a "locking read" -- what I'd like to know is "what cases exist where the observable behavior will differ between specifying FOR UPDATE
and not specifying it -- that is, what specifically does that lock entail?
推荐答案
http ://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html
这与在事务中锁定表有关.假设您具有以下条件:
It has to do with locking the table in transactions. Let's say you have the following:
START TRANSACTION;
SELECT .. FOR UPDATE;
UPDATE .... ;
COMMIT;
在SELECT语句运行之后,如果您还有其他用户的另一个SELECT,则直到您的第一个事务到达COMMIT行时,它才会运行.
after the SELECT statement runs, if you have another SELECT from a different user, it won't run until your first transaction hits the COMMIT line.
还请注意,在交易之外的FOR UPDATE
是没有意义的.
Also note that FOR UPDATE
outside of a transaction is meaningless.
这篇关于SELECT ... FOR UPDATE *的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!