MYSQL LOCK TABLE
Row Level Locking
If the tables use InnoDB, MySQL automatically uses row level locking so that multiple transactions can use same table simultaneously for read and write, without making each other wait.
If two transactions trying to modify the same row and both uses row level locking, one of the transactions waits for the other to complete.
Row level locking also can be obtained by using SELECT ... FOR UPDATE statement for each rows expected to be modified.
Consider two connections to explain Row level locking in detail
In connection 1, row level lock obtained by SELECT ... FOR UPDATE statement.
When some one try to update same row in connection 2, that will wait for connection 1 to finish transaction or error message will be displayed according to the innodb_lock_wait_timeout setting, which defaults to 50 seconds.
To view details about this lock, run SHOW ENGINE INNODB STATUS
UPDATE accDetails SET ledgerAmount = ledgerAmount + 250 WHERE id=2;
But while updating some other row in connection 2 will be executed without any error.
Now row lock is released, because transaction is commited in Connection 1.
UPDATE accDetails SET ledgerAmount = ledgerAmount + 500 WHERE id=1;
The update is executed without any error in Connection 2 after Connection 1 released row lock by finishing the transaction.