Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lock rows in an InnoDB table?

Tags:

php

mysql

innodb

I'm a new MySQL user. I create an InnoDB database system.

In my case, I need lock rows if select for update is use. But even if I've done many search I don't understand how to lock row for all users if this row is loaded by one user.

I use php to connect on mySQL database.

Can you help me to solve that?

like image 822
Flex60460 Avatar asked Dec 28 '22 11:12

Flex60460


1 Answers

MySQL will do the row-locking for you automatically.
There should be no need to do the row locking yourself.

However if you insist on doing this, you can use a select ... for update.
See: http://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html

SELECT counter_field FROM child_codes FOR UPDATE;
UPDATE child_codes SET counter_field = counter_field + 1;
like image 70
Johan Avatar answered Jan 08 '23 01:01

Johan