Select com Update

Asked

Viewed 492 times

3

Would anyone know if there is an SQL calling to run a SELECT using LOCK IN SHARE MODE registry and running an UPDATE at the same time? example:

SELECT * FROM Nome_Tabela WHERE id = 5 LOCK IN SHARE MODE (UPDATE AQUI)

Any suggestions? I’m using Mysql.

  • 2

    Using LOCK IN SHARE MODE in an update is not very recommended, because if two users are reading the same content, one of them will end up being lost when the value is updated. To implement the reading of a given value, first perform the reading lock of it, using FOR UPDATE and then increment the value. https://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html

1 answer

1

If your intention is "lock" on the record so no one changes before you make your update, recommend using the FOR UPDATE.

Example:

SELECT nome FROM clientes WHERE nome = 'josé' FOR UPDATE; UPDATE clientes SET nome = 'josé das couves';

Reference Mysql.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.