Row Level Locks in MySQL

This section provides tutorial examples on how row locks resulted implicitly in transactions with the READ UNCOMMITTED isolation level.

In MySQL, row level locks happen implicitly within transactions. Here is an example:

Session 1:                        Session 2:

mysql> PROMPT >;
>DROP TABLE IF EXISTS User;
>CREATE TABLE User (ID INT 
>   PRIMARY KEY, Name CHAR(8))
>   ENGINE=InnoDB; 
>INSERT INTO User VALUES (2, 
>   'bill');
>SET TRANSACTION ISOLATION LEVEL 
>   READ UNCOMMITTED;
>START TRANSACTION;
>UPDATE User SET Name='bob' 
>   WHERE ID='2';

                                  mysql> PROMPT >;
                                  >INSERT INTO User VALUES (3,
                                  >   'jack');
                                  >SELECT * FROM User;
                                  +----+------+
                                  |  2 | bill |
                                  |  3 | jack |
                                  +----+------+
                                  >UPDATE User SET Name='brown' 
                                  >   WHERE ID='2';
                                  (waiting for the lock to release)
>COMMIT;
                                  (insert done)

The output seems to be telling me that:

Another way to lock rows is to the "LOCK IN SHARE MODE" option in SELECT statements:

Session 1:                        Session 2:

mysql> PROMPT >;
>DROP TABLE IF EXISTS User;
>CREATE TABLE User (ID INT 
>   PRIMARY KEY, Name CHAR(8))
>   ENGINE=InnoDB; 
>INSERT INTO User VALUES (2, 
>   'bill');
>SET TRANSACTION ISOLATION LEVEL 
>   READ UNCOMMITTED;
>START TRANSACTION;
>SELECT * FROM User WHERE ID='2'
>   LOCK IN SHARE MODE;

                                  mysql> PROMPT >;
                                  >INSERT INTO User VALUES (3,
                                  >   'jack');
                                  >SELECT * FROM User;
                                  +----+------+
                                  |  2 | bill |
                                  |  3 | jack |
                                  +----+------+
                                  >UPDATE User SET Name='brown' 
                                  >   WHERE ID='2';
                                  (waiting for the lock to release)
>COMMIT;
                                  (insert done)

Last update: 2015.

Table of Contents

 About This Book

 Introduction of SQL

 MySQL Introduction and Installation

 Introduction of MySQL Programs

 Perl Programs and MySQL Servers

 PHP Programs and MySQL Servers

 Java Programs and MySQL Servers

 Datatypes and Data Literals

 Operations and Expressions

 Character Strings and Bit Strings

 Commonly Used Functions

 Table Column Types for Different Types of Values

 Using DDL to Create Tables and Indexes

 Using DML to Insert, Update and Delete Records

 Using SELECT to Query Database

 Transaction Management and Isolation Levels

Locks Used in MySQL

 Lock Types and Lock Levels

 Table Level Locks in MySQL

Row Level Locks in MySQL

 Defining and Calling Stored Procedures

 Variables, Loops and Cursors Used in Stored Procedures

 Outdated Tutorials

 References

 PDF Printing Version