Transactions: BEGIN, COMMIT and ROLLBACK

A transaction groups changes. COMMIT keeps them; ROLLBACK undoes them.

Syntax

BEGIN; … COMMIT;
BEGIN; … ROLLBACK;
  • Without BEGIN, each statement is committed on its own.

Example

BEGIN; DELETE FROM order_items WHERE order_id = 12; ROLLBACK;

The delete is undone, so the table is unchanged afterwards.

Tables used: order_items. Try it in a lesson below.

Common mistakes

  • A transaction left open with no COMMIT or ROLLBACK holds its locks. Always end it.

Practise it