MySQL + SQL · Lesson 81

Transactions in MySQL

What is a Transaction?

A transaction is a group of SQL statements treated as ONE unit — either all succeed (COMMIT) or none do (ROLLBACK). This keeps data safe during multi-step operations.

Bank Transfer Example

START TRANSACTION;
UPDATE accounts SET balance = balance - 500 WHERE id = 1;
UPDATE accounts SET balance = balance + 500 WHERE id = 2;
COMMIT;   -- both updates saved together
If the second update failed, ROLLBACK would undo the first — money is never lost.

Key Commands

  • START TRANSACTION — begin.
  • COMMIT — save all changes.
  • ROLLBACK — undo all changes.

Summary

  • A transaction groups statements into one all-or-nothing unit.
  • COMMIT saves; ROLLBACK undoes. Essential for money/critical updates.
🔗

Share this topic with a friend

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

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

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

WhatsApp