SQL Keywords

SQL BEGIN

What is SQL BEGIN?

BEGIN starts an explicit database transaction so multiple statements can be committed or rolled back as a single unit.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL BEGIN:

SQL BEGIN Full Explanation

BEGIN marks the start of a transaction block. All subsequent DML statements run under the same transactional context until a COMMIT or ROLLBACK ends the block. Inside the block, changes stay invisible to other sessions, allowing atomic, consistent updates. BEGIN is synonymous with BEGIN TRANSACTION and BEGIN WORK in many systems, although the full ANSI-SQL form is START TRANSACTION. Modern engines let you add options such as isolation level, read-write mode, or deferrable constraints directly after BEGIN. If the session is already inside a transaction, issuing BEGIN may raise an error (PostgreSQL) or silently nest (SQL Server “savepoints”). Some dialects (MySQL) reserve BEGIN for procedure blocks; for transactions use START TRANSACTION or BEGIN WORK instead.

SQL BEGIN Syntax

BEGIN;
-- or with options (PostgreSQL)
BEGIN [ ISOLATION LEVEL { READ COMMITTED | REPEATABLE READ | SERIALIZABLE } ]
      [ READ WRITE | READ ONLY ]
      [ DEFERRABLE | NOT DEFERRABLE ];

SQL BEGIN Parameters

  • WORK (TRANSACTION) - optional keywords accepted for readability
  • ISOLATION LEVEL (string) - sets transaction isolation mode
  • READ WRITE (READ ONLY) - keyword|||limits write capability inside transaction
  • DEFERRABLE (NOT DEFERRABLE) - keyword|||controls constraint evaluation timing

Example Queries Using SQL BEGIN

-- Basic transaction
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;

-- Rollback on error
BEGIN;
DELETE FROM orders WHERE created_at < NOW() - INTERVAL '1 year';
-- oops, wrong filter
ROLLBACK;

-- Set stricter isolation
BEGIN ISOLATION LEVEL SERIALIZABLE READ WRITE;
INSERT INTO inventory_log(item_id, qty) VALUES (42, -5);
COMMIT;

Expected Output Using SQL BEGIN

  • BEGIN itself returns a simple success message
  • The database allocates a new transaction ID
  • All following statements run inside this transaction until COMMIT makes the changes permanent or ROLLBACK reverts them

Use Cases with SQL BEGIN

  • Group multiple DML statements so they succeed or fail together
  • Maintain consistency during complex updates like money transfers
  • Perform read-only analytical queries at a stable snapshot
  • Apply bulk deletes or inserts while giving an option to undo
  • Test schema changes in a safe reversible block

Common Mistakes with SQL BEGIN

  • Forgetting to COMMIT, leaving idle in-transaction sessions holding locks
  • Calling BEGIN inside an existing transaction in PostgreSQL (raises error)
  • Confusing MySQL BEGIN (procedure block) with transaction BEGIN
  • Assuming autocommit is off; in many clients you must explicit BEGIN
  • Mixing transaction modes (e.g., setting isolation after some statements ran)

Related Topics

First Introduced In

PostgreSQL 6.5 (1999)

Frequently Asked Questions

What is the difference between BEGIN, BEGIN TRANSACTION, and BEGIN WORK?

All three forms start a transaction. They are synonyms in PostgreSQL, SQL Server, and many others. The shorter BEGIN is most common.

Does BEGIN lock the entire table?

No. Locks are taken only when later DML statements run, and the scope depends on the operation and isolation level.

How do I cancel a transaction after BEGIN?

Issue ROLLBACK to revert all changes made since BEGIN.

Can I change isolation level after BEGIN?

Most engines require isolation level to be set in the BEGIN statement itself. Setting it later has no effect in PostgreSQL.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!