Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 82

Transactions in MySQL

What Is a Transaction in MySQL?

A transaction groups related database operations into one logical unit. A school fee payment, order checkout or bank transfer may touch several rows and tables; partial completion would leave the database wrong. A transaction gives the application one final decision: COMMIT all valid changes or ROLLBACK all uncommitted changes.

Faculty definition: A transaction is a sequence of operations that moves the database from one valid state to another and is treated as a single unit of work.
StageDatabase stateAction
Before startPreviously committed dataPlan the complete business unit
ActiveChanges are pending in this sessionValidate rows and execute DML
CommittedChanges are permanentReturn success to the application
Rolled backPending changes are cancelledReport failure or safely retry

Verified Bank Transaction Lab

Run schema creation before the transaction because DDL can cause an implicit commit. Both tables explicitly use InnoDB.

CREATE TABLE bank_accounts (
  account_id INT PRIMARY KEY,
  holder_name VARCHAR(60) NOT NULL,
  balance DECIMAL(12,2) NOT NULL,
  CONSTRAINT chk_nonnegative_balance CHECK (balance >= 0)
) ENGINE = InnoDB;

CREATE TABLE transfer_ledger (
  transfer_id BIGINT PRIMARY KEY,
  from_account INT NOT NULL,
  to_account INT NOT NULL,
  amount DECIMAL(12,2) NOT NULL,
  status VARCHAR(20) NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT chk_positive_amount CHECK (amount > 0),
  CONSTRAINT fk_transfer_from FOREIGN KEY (from_account)
    REFERENCES bank_accounts(account_id),
  CONSTRAINT fk_transfer_to FOREIGN KEY (to_account)
    REFERENCES bank_accounts(account_id)
) ENGINE = InnoDB;

INSERT INTO bank_accounts VALUES
(101, 'Asha',  5000.00),
(102, 'Ravi',  3000.00),
(103, 'Meera', 1200.00);
Opening total balance = 9200.00

The total is a useful invariant: transferring money may change individual balances but must not create or destroy the combined 9200.00.

Transaction Commands and Boundaries

START TRANSACTION;
-- SELECT, INSERT, UPDATE and DELETE statements
COMMIT;

START TRANSACTION;
-- statements that should not be kept
ROLLBACK;
StatementPurposeEnds transaction?
START TRANSACTIONBegin an explicit multi-statement transactionStarts it
COMMITMake current changes permanentYes
ROLLBACKCancel current uncommitted changesYes
SAVEPOINT nameMark a partial recovery pointNo
ROLLBACK TO nameUndo work after one savepointNo

BEGIN is an alias for starting an ad-hoc transaction, but START TRANSACTION is clearer and supports transaction characteristics. In stored programs, BEGIN normally opens a compound block, so use START TRANSACTION where transaction control is permitted and appropriate.

Safe Transfer with COMMIT

Lock both account rows in a consistent order, validate the source balance, then perform the debit, credit and audit insert as one unit.

START TRANSACTION;

SELECT account_id, balance
FROM bank_accounts
WHERE account_id IN (101, 102)
ORDER BY account_id
FOR UPDATE;

UPDATE bank_accounts
SET balance = balance - 1000.00
WHERE account_id = 101
  AND balance >= 1000.00;

-- Application must confirm ROW_COUNT() = 1.
UPDATE bank_accounts
SET balance = balance + 1000.00
WHERE account_id = 102;

INSERT INTO transfer_ledger
  (transfer_id, from_account, to_account, amount, status)
VALUES
  (9001, 101, 102, 1000.00, 'COMMITTED');

COMMIT;
101 | Asha | 4000.00 102 | Ravi | 4000.00 103 | Meera | 1200.00 Total | 9200.00 Ledger rows | 1

The application must not blindly continue if the conditional debit affects zero rows. It should throw an error and execute ROLLBACK. The database cannot infer every business rule from SQL text alone.

Prove ROLLBACK with a Controlled Test

START TRANSACTION;

UPDATE bank_accounts
SET balance = balance - 300.00
WHERE account_id = 103;

SELECT balance FROM bank_accounts
WHERE account_id = 103;
-- Current session sees 900.00.

ROLLBACK;

SELECT balance FROM bank_accounts
WHERE account_id = 103;
-- Final balance is again 1200.00.
Inside transaction: 900.00 After ROLLBACK: 1200.00

A full ROLLBACK also releases InnoDB locks held by the transaction. Never use rollback as an ordinary correction mechanism after data has already been committed; instead, record a compensating business transaction when history must remain auditable.

Autocommit, Session Scope and Storage Engines

MySQL sessions normally begin with autocommit = 1. Outside an explicit transaction, each successful statement is its own transaction and cannot later be grouped with the next statement.

SELECT @@autocommit;       -- normally 1
SET SESSION autocommit = 0;
-- A transaction remains open until COMMIT or ROLLBACK.
COMMIT;
SET SESSION autocommit = 1;
  • Prefer explicit START TRANSACTION for a clearly bounded unit while leaving normal session behavior unchanged.
  • Autocommit is per session; changing it in one connection does not change another.
  • Use InnoDB tables. A ROLLBACK cannot undo changes made to a nontransactional table.
  • Do not mix storage engines inside one critical transaction.
  • If a connection with autocommit disabled closes without its final COMMIT, MySQL rolls back that open transaction.

Implicit Commits and Non-Rollback Traps

Many DDL and administrative statements end an active transaction implicitly. Typical examples include CREATE TABLE, ALTER TABLE, DROP TABLE, TRUNCATE TABLE and CREATE INDEX. MySQL's atomic DDL protects the DDL operation itself, but it does not make DDL part of your surrounding business transaction.

START TRANSACTION;
UPDATE bank_accounts SET balance = balance + 50
WHERE account_id = 101;

ALTER TABLE bank_accounts ADD COLUMN branch_code CHAR(4);
-- Do not expect a later ROLLBACK to undo the earlier UPDATE.
Production rule: migrations and schema changes belong in a controlled deployment, not in the middle of fee, order or payment DML.

Concurrency, Error Handling and Best Practice

A normal SELECT does not reserve rows for a later update. SELECT ... FOR UPDATE takes locking reads on matching InnoDB records until COMMIT or ROLLBACK. This closes the gap between checking a balance and changing it.

  1. Keep transactions short; never wait for user input while locks are held.
  2. Access shared rows in the same order to reduce deadlocks.
  3. Index predicates used by FOR UPDATE and UPDATE.
  4. Check affected-row counts after debit, credit and ledger statements.
  5. Catch every exception, ROLLBACK, and retry only transient failures such as selected deadlocks.
  6. Use DECIMAL for money, never FLOAT.
  7. Commit only after all business invariants are verified.

Continue with the complete transaction example, ACID properties and COMMIT, ROLLBACK and SAVEPOINT.

Official References

Syntax and behavior were checked against the official MySQL 8.4 manual. Test the workflow, permissions and failure paths on your own server before production use.

Frequently Asked Questions

What is a transaction in MySQL?
A transaction is one logical unit of work containing one or more SQL statements. COMMIT makes its successful changes permanent; ROLLBACK cancels its uncommitted changes.
Is autocommit enabled by default in MySQL?
Yes. A new MySQL session normally starts with autocommit enabled, so each successful statement is committed unless an explicit transaction is active.
Which MySQL storage engine should be used for transactions?
Use a transaction-safe engine such as InnoDB. Changes to nontransactional tables cannot be fully undone by ROLLBACK.
Can CREATE TABLE or ALTER TABLE be rolled back with the surrounding DML?
Do not rely on that. Many DDL statements cause an implicit commit, and atomic DDL is not the same as transactional DDL. Create the schema before the business transaction.
Why use SELECT FOR UPDATE before transferring money?
It locks the selected InnoDB rows until COMMIT or ROLLBACK, preventing another transaction from changing the balance between validation and update.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.