Name

LOCK

Synopsis

LOCK TABLES name [AS alias] READ|WRITE [, name2 [AS alias] READ|WRITE, ...]

Locks a table for the use of a specific thread. This command is generally used to emulate transactions as described in Chapter 7. If a thread creates a READ lock all other threads may read from the table but only the controlling thread can write to the table. If a thread creates a WRITE lock, no other thread may read from or write to the table.

Warning

Using locked and unlocked tables at the same time can cause the process thread to freeze. You must lock all of the tables you will be accessing during the time of the lock. Tables you access only before or after the lock do not need to be locked. The newest versions of MySQL generate an error if you attempt to access an unlocked table while you have other tables locked.

Example

# Lock tables 'table1' and 'table3' to prevent updates, and block all access 
# to 'table2'. Also create the alias 't3' for 'table3' in the current thread.
LOCK TABLES table1 READ, table2 WRITE, table3 AS t3 READ

Get MySQL and mSQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.