Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

321
Views
LOCK TABLES does not work as expected with multiple tables

I try to understand why there is an error in the following case stating that a table was "not locked" even though a (WRITE) LOCK was issued just before.

This is the setup (MySQL / MariaDB):

CREATE TABLE A
(
    id INT
);

CREATE TABLE B
(
    id INT
);

Here are the cases (first one being the failing one):

# does not work --> Error: "Table 'B' was not locked with LOCK TABLES" on 2nd statement
LOCK TABLES A WRITE, B WRITE;
INSERT INTO A (id) VALUES ((SELECT 1 FROM B)), ((SELECT 2 FROM B));
UNLOCK TABLES;

# works (single statements)
LOCK TABLES A WRITE, B WRITE;
INSERT INTO A (id) VALUES ((SELECT 1 FROM B));
INSERT INTO A (id) VALUES ((SELECT 2 FROM B));
UNLOCK TABLES;

# works (table aliases)
LOCK TABLES A WRITE, B tb1 WRITE, B tb2 WRITE;
INSERT INTO A (id) VALUES ((SELECT 1 FROM B tb1)), ((SELECT 2 FROM B tb2));
UNLOCK TABLES;

What causes the error in the first case?


Edit: I simplified the example again to the bare minimum.

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!