Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lock a single row

I have a user table with field lastusedecnumber.

I need to access and increment lastusedecnumber.

During that accessing time I need to lock that particular user row (not the entire table).

How do I do this?

The table type is MyISAM.

like image 481
svk Avatar asked Sep 06 '25 03:09

svk


2 Answers

MySQL uses only table-level locking from MyISAM tables. If you can, switch to InnoDB for row-level locking.

Here's a link to the MySQL site describing Locks set by SQL Statements for InnoDB tables. http://dev.mysql.com/doc/refman/5.0/en/innodb-locks-set.html

like image 144
belwood Avatar answered Sep 07 '25 21:09

belwood


Kind of late, but hope it will help someone:

UPDATE user SET lastusedecnumber = LAST_INSERT_ID(lastusedecnumber + 1);
SELECT LAST_INSERT_ID();

Will give you atomic increment of lastusedecnumber and ability to read new value of lastusedecnumber field (after increment) using SELECT LAST_INSERT_ID().

like image 28
Aleksandr Guidrevitch Avatar answered Sep 07 '25 21:09

Aleksandr Guidrevitch