sqlcmd.net validated sql reference
intermediate transactions MySQL MariaDB SQL Server PostgreSQL SQLite

Group Statements Into A Transaction

Use `BEGIN`, `COMMIT`, and `ROLLBACK` to execute multiple statements as an atomic unit.

Docker-validated Not currently validation-green

Transfer funds between accounts atomically

Alice's balance decreases by 100 and Bob's increases by 100. Both updates are committed together. If either update had failed, a ROLLBACK would have reverted both — keeping the total balance consistent. Replace COMMIT with ROLLBACK to undo both changes instead.

MySQL MariaDB
Engine-specific syntax
Setup
CREATE TABLE accounts (id INT, name VARCHAR(50), balance INT);

INSERT INTO
  accounts (id, name, balance)
VALUES
  (1, 'Alice', 1000),
  (2, 'Bob', 500);
SQL
START TRANSACTION;

UPDATE accounts
SET
  balance = balance - 100
WHERE
  id = 1;

UPDATE accounts
SET
  balance = balance + 100
WHERE
  id = 2;

COMMIT;

SELECT
  id,
  name,
  balance
FROM
  accounts
ORDER BY
  id;
idnamebalance
1Alice900
2Bob600
SQL Server
Engine-specific syntax
Setup
CREATE TABLE accounts (id INT, name VARCHAR(50), balance INT);

INSERT INTO
  accounts (id, name, balance)
VALUES
  (1, 'Alice', 1000),
  (2, 'Bob', 500);
SQL
BEGIN TRANSACTION;

UPDATE accounts
SET
  balance = balance - 100
WHERE
  id = 1;

UPDATE accounts
SET
  balance = balance + 100
WHERE
  id = 2;

COMMIT;

SELECT
  id,
  name,
  balance
FROM
  accounts
ORDER BY
  id;
idnamebalance
1Alice900
2Bob600
PostgreSQL SQLite
Engine-specific syntax
Setup
CREATE TABLE accounts (id INT, name VARCHAR(50), balance INT);

INSERT INTO
  accounts (id, name, balance)
VALUES
  (1, 'Alice', 1000),
  (2, 'Bob', 500);
SQL
BEGIN;

UPDATE accounts
SET
  balance = balance - 100
WHERE
  id = 1;

UPDATE accounts
SET
  balance = balance + 100
WHERE
  id = 2;

COMMIT;

SELECT
  id,
  name,
  balance
FROM
  accounts
ORDER BY
  id;
idnamebalance
1Alice900
2Bob600

SQL Server uses BEGIN TRANSACTION, MySQL/MariaDB use START TRANSACTION, and PostgreSQL uses BEGIN. The resulting data is identical across engines.

Where this command helps.

  • grouping multiple updates so they all succeed or all fail together
  • safely transferring value between rows without partial state

What the command is doing.

A transaction groups multiple SQL statements so they either all succeed or all fail together. BEGIN (or START TRANSACTION / BEGIN TRANSACTION) opens the transaction. COMMIT applies all changes permanently. ROLLBACK discards all changes made since BEGIN, leaving the data unchanged. Transactions prevent partial updates — for example, if a funds transfer debits one account but crashes before crediting another, a rollback restores both accounts to their original state.