Iam using mysql v5.6 .
When i send the following mysql query using php which will select some rows also locking them:
SELECT * FROM accounts WHERE id = 1 FOR UPDATE;
I get the following error message:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your Mysql server version for the right syntax to use near 'LIMIT 0, 25' at line 2
Thanks
InnoDB implements standard row-level locking
where there are two types of locks, shared (S) locks
and exclusive (X) locks
.
A shared (S) lock permits a transaction to read a row. An exclusive (X) lock permits a transaction to update or delete a row.
Locking of rows for update using SELECT FOR UPDATE
only applies when autocommit
is disabled (either by beginning transaction with START TRANSACTION
or by setting autocommit
to 0
. If autocommit is enabled, the rows matching the specification are not locked.
Refer link
https://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html
https://dev.mysql.com/doc/refman/5.0/en/innodb-lock-modes.html
Mysql innodb row lock doesn't work
Now, I think its problem is you should not use the transaction not started
that only make big different so, you should must START TRANSACTION
like my code, and
some times it was happens when in a running transaction
, two identical statements get different values, because some other transaction has modified the table's rows.
like your as following as:
transaction1> START TRANSACTION;
transaction1> SELECT * FROM accounts WHERE id=1 FOR UPDATE;
For example:
transaction1> SELECT first_name, last_name FROM customer WHERE id = 3 FOR UPDATE;
+------------+-----------+
| first_name | last_name |
+------------+-----------+
| JMAIL | KRISH |
+------------+-----------+
1 row in set (0.00 sec)
you should refer this link:
http://www.mysqlperformanceblog.com/2012/03/27/innodbs-gap-locks/
http://dev.mysql.com/doc/refman/5.1/en/innodb-locks-set.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.