SELECT ... *FOR UPDATE* 的目的是什么?
我很困惑为什么你会指定 FOR UPDATE
——为什么数据库关心你要如何处理 SELECT
中的数据?
编辑:抱歉,我问的问题很糟糕。我知道文档说它会将事情变成“锁定读取”——我想知道的是“在哪些情况下,指定 FOR UPDATE
和不指定它之间可观察的行为会有所不同——也就是说,这个锁具体意味着什么?
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
?
EDIT: 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
http://dev.mysql.com/doc/refman/5.0/en/ innodb-locking-reads.html
它与事务中锁定表有关。假设您有以下情况:
在 SELECT 语句运行后,如果您有来自不同用户的另一个 SELECT,则它不会运行,直到您的第一个事务到达 COMMIT 行。
另请注意,事务之外的
FOR UPDATE
是没有意义的。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:
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.
Also note that
FOR UPDATE
outside of a transaction is meaningless.其旨在解决的特定情况是当您需要读取和更新列中的值时。有时,您可以先更新列(锁定它),然后再读取它,例如:
这将返回 counter_field 的新值,但这在您的应用程序中可能是可以接受的。如果您尝试重置字段(因此需要原始值)或者您有无法在更新语句中表达的复杂计算,则这是不可接受的。在这种情况下,为了避免两个连接同时更新同一列,您需要锁定该行。
如果您的 RDBMS 不支持 FOR UPDATE 那么您可以通过执行无用的更新来模拟它,例如
The specific case that this is designed to fix is when you need to read and update a value in a column. Sometimes you can get away with updating the column first (which locks it) and then reading it afterwards, for instance:
This will return the new value of counter_field, but that may be acceptable in your application. It would not be acceptable if you were trying to reset the field (and you therefore needed the original value) or if you had a complex calculation that could not be expressed in an update statement. In this case to avoid two connections racing to update the same column at the same time you need to lock the row.
If your RDBMS doesn't support FOR UPDATE then you can simulate it by performing a useless update e.g.
SELECT FOR UPDATE 告诉 RDBMS 您想要锁定这些行,以便其他人无法访问它们,直到您更新并提交或回滚它们并解锁它们:
http://www.techonthenet.com/oracle/cursors/for_update.php
SELECT FOR UPDATE tells the RDBMS that you want to lock those rows so no one else can access them until you UPDATE and commit or roll them back and unlock them:
http://www.techonthenet.com/oracle/cursors/for_update.php
它创建一个锁定读取,以便在完成之前没有人可以更新它,示例
请参见此处 http://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html
It creates a locking read so that nobody can update it until you are done, example
See here http://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html
它将锁定行(或整个表),以便不能在另一个会话中同时更新这些行。锁定将一直保持到事务提交或回滚为止。
It will lock the rows (or the whole table) so that the rows can't be updated in another session concurrently. The lock is held until the transactions is committed or rolled back.