SQL Keywords

SQL UNLOCK

What does the SQL UNLOCK TABLES command do?

Releases all explicit table locks previously obtained in the current session.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL UNLOCK:

SQL UNLOCK Full Explanation

UNLOCK TABLES is a MySQL and MariaDB command that frees every table lock acquired with LOCK TABLES in the current client session. It restores normal concurrency so that other sessions can read or write the affected tables. UNLOCK TABLES is automatically executed when a COMMIT, ROLLBACK, or connection close occurs, but issuing it explicitly lets you shorten the blocking window and avoid long-running locks. You cannot unlock individual tables; the statement always releases all locks held by the session. UNLOCK TABLES is not part of the ANSI SQL standard and is unsupported in PostgreSQL, SQL Server, SQLite, or Oracle.

SQL UNLOCK Syntax

UNLOCK TABLES;

SQL UNLOCK Parameters

Example Queries Using SQL UNLOCK

-- Obtain explicit locks
LOCK TABLES orders WRITE, customers READ;

-- Perform work that requires those locks
UPDATE orders SET status = 'shipped' WHERE id = 101;
SELECT c.name FROM customers AS c JOIN orders o ON c.id = o.customer_id;

-- Release all locks early
UNLOCK TABLES;

-- Alternative: implicit unlock via COMMIT
LOCK TABLES inventory WRITE;
UPDATE inventory SET quantity = quantity - 5 WHERE sku = 'A123';
COMMIT;  -- also unlocks

Expected Output Using SQL UNLOCK

  • MySQL returns "Query OK, 0 rows affected" and immediately removes every lock held by the session, allowing other connections to access the previously locked tables

Use Cases with SQL UNLOCK

  • Free table locks as soon as critical updates finish to reduce contention
  • Release read locks acquired for consistent logical backups
  • Switch from one locked work set to another without ending the session
  • Avoid holding locks across network round-trips in interactive tools

Common Mistakes with SQL UNLOCK

  • Forgetting to call UNLOCK TABLES, leaving tables blocked until the session ends
  • Expecting to unlock only specific tables (UNLOCK TABLES releases all locks)
  • Using UNLOCK TABLES in databases that do not support it
  • Mixing LOCK TABLES with transactional statements on InnoDB tables and assuming row-level locking still applies

Related Topics

First Introduced In

MySQL 3.23

Frequently Asked Questions

When should I call UNLOCK TABLES?

Call it immediately after the critical section of code that required explicit locks finishes. Doing so reduces blocking time for other sessions.

Does UNLOCK TABLES end my current transaction?

No. It only frees table locks. If autocommit is disabled, your transaction remains open until you issue COMMIT or ROLLBACK.

Can UNLOCK TABLES be rolled back?

No. Once locks are released, they cannot be re-acquired automatically by rolling back; you must call LOCK TABLES again.

What privileges are needed to use UNLOCK TABLES?

You need the same privileges required for LOCK TABLES (typically SELECT for READ locks and INSERT/UPDATE/DELETE for WRITE locks) on the affected tables.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!