A transaction: all or nothing

Group statements so they succeed together. ROLLBACK undoes everything since START TRANSACTION. Needs InnoDB.

SQL · A transaction: all or nothingIntermediate
START TRANSACTION;

UPDATE products SET stock = stock - 2 WHERE id = 3;
INSERT INTO orders (customer_id, total, status, created_at) VALUES (1, 40.00, 'paid', NOW());

-- if everything went fine:
COMMIT;
-- if something failed:  ROLLBACK;
transactioncommitrollbackinnodb