SQL Transactions

Group related statements so they commit together or roll back together.

What & Why

A transaction is an all-or-nothing unit of work bounded by BEGIN and COMMIT or ROLLBACK. Transactions protect consistency when several dependent changes must succeed as one business action.

See How It Works

BUSINESS QUESTION

Illustrate changing one campaign status inside a transaction after reviewing the exact target row.

idnamechannelspendstart_dateend_datestatustarget_segment
1Spring Launchgoogle_ads55000.002024-01-152024-03-31activesmb
2Retention Webinaremail45000.002024-02-102024-04-15activeenterprise
3Finance Retargetinglinkedin50000.002024-03-122024-05-31activeenterprise
4Enterprise Searchgoogle_ads60000.002024-04-012024-06-30activeenterprise
EXAMPLE QUERY
BEGIN;
UPDATE marketing.campaigns
SET status = 'paused'
WHERE id = 1
  AND status = 'active';
COMMIT;
TRANSACTION — campaign 1 status change
stepcampaign_idstatusdurability
BEGIN1activestarting value
UPDATE1pausedpending
COMMIT1pausedpersisted

The finalized example updates Spring Launch (campaign ID 1), and COMMIT makes its paused status durable.

This lesson's practice is part of Pro.

Advanced business practice

Sign up free to try it on a real business scenario