Ein Lock (Sperre) verhindert, dass mehrere Benutzer gleichzeitig dieselben Daten ändern und dadurch inkonsistente Zustände entstehen.
Beispiel ohne Locking:
MySQL verwendet verschiedene Lock-Typen:
-- Explizites Table-Lock
LOCK TABLES mitarbeiter WRITE;
UPDATE mitarbeiter SET gehalt = gehalt * 1.1;
UNLOCK TABLES;
-- Explizites Row-Lock
SELECT * FROM konten
WHERE konto_id = 123
FOR UPDATE;InnoDB verwendet automatisch Row-Level-Locking:
-- Automatisches Row-Lock bei Update
UPDATE konten
SET kontostand = kontostand - 200
WHERE konto_id = 123;