SyntaxStudy
Sign Up
MySQL Nested Transactions and Savepoints
MySQL Intermediate 4 min read

Nested Transactions and Savepoints

Nested Transactions

MySQL does not support true nested transactions. Use savepoints to create sub-transaction checkpoints within a transaction.

Example
START TRANSACTION;
INSERT INTO orders (user_id, total) VALUES (1, 99.99);
SAVEPOINT order_created;

-- Try to reserve inventory
INSERT INTO reservations (product_id, qty) VALUES (5, 2);
-- If reservation fails:
ROLLBACK TO SAVEPOINT order_created;
-- Mark order as pending instead
UPDATE orders SET status = "pending_inventory" WHERE id = LAST_INSERT_ID();
COMMIT;
Pro Tip

SAVEPOINT names are scoped to the current transaction — release them to free resources: RELEASE SAVEPOINT name.