Table Locking — SQL Technical Interview Questions & Answers
Table Locking
Table Locking
- LOCK TABLES students WRITE; stops others from writing that table until UNLOCK TABLES; This is old-school. InnoDB row locks inside transactions are the normal way: START TRANSACTION; UPDATE …; COMMIT;
- READ lock lets others read. WRITE lock is exclusive. Easy to deadlock a lab if you forget UNLOCK.
- Trap: LOCK TABLES as the answer for every concurrency question. Prefer transactions + InnoDB.
- On the example next to this theory: Table Locking: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.
Technical Interview Tip: Know LOCK TABLES exists. Prefer START TRANSACTION for InnoDB.
Code Examples for Table Locking
Table Locking — sample query
-- Table Locking
CREATE TABLE demo (
id INTEGER PRIMARY KEY,
label TEXT NOT NULL,
qty INTEGER DEFAULT 0
);
INSERT INTO demo (id, label, qty) VALUES
(1, 'alpha', 2),
(2, 'beta', 5);
SELECT label, qty FROM demo WHERE qty >= 2 ORDER BY qty DESC;Table Locking: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.
How to Explain Table Locking to an Interviewer
Modern alternative to LOCK TABLES?
InnoDB transactions START TRANSACTION / COMMIT