A transaction is a group of database operations that must succeed or fail as one unit. Without this, a crash halfway through a bank transfer could destroy money.
What a transaction is
Moving Rs 500 from account A to account B takes two writes: subtract from A, add to B. If the system fails after the first, Rs 500 vanishes. A transaction wraps both steps so that either both happen or neither does.
START TRANSACTION;
UPDATE account SET balance = balance - 500 WHERE acc_no = 'A';
UPDATE account SET balance = balance + 500 WHERE acc_no = 'B';
COMMIT; -- make permanent; use ROLLBACK to undo
ACID properties
| Property | Meaning | Banking example |
|---|---|---|
| Atomicity | All or nothing | Both updates occur or neither |
| Consistency | Moves the database from one valid state to another | Total money stays the same |
| Isolation | Concurrent transactions do not disturb each other | Another user never sees the half-done transfer |
| Durability | Committed changes survive crashes | After COMMIT, a power cut cannot undo it |
Transaction states
Active, partially committed, committed, failed and aborted. A failed transaction is rolled back to the start.
Transaction support in SQL
COMMIT saves the work, ROLLBACK undoes it, and SAVEPOINT name marks a point to which you can roll back with ROLLBACK TO name.
SAVEPOINT s1;
UPDATE part SET price = price * 2;
ROLLBACK TO s1; -- undoes only the update
COMMIT;
Schedules
A schedule is the order in which operations of several transactions run. A serial schedule runs one transaction fully before the next. A concurrent schedule interleaves them to use the system better, but must be controlled so results stay correct.
Problems without control
- Lost update: two transactions write the same item and one overwrite vanishes.
- Dirty read: reading data that another transaction later rolls back.
- Non-repeatable read: reading the same row twice and getting different values.
Common mistakes
- Thinking durability means data can never be lost by disk failure. It means committed work survives a crash; backups handle disk loss.
- Forgetting that many tools auto-commit each statement by default.
Practice questions
- Define transaction. Why is it needed?
- Explain ACID properties with an example.
- What is the difference between COMMIT and ROLLBACK?
- What is a schedule? Differentiate serial and concurrent schedules.
- What is a dirty read?
Answers to the practice questions
- A logical unit of work that must be executed fully or not at all, to keep data correct.
- Atomicity, consistency, isolation and durability, as shown with the bank transfer.
- COMMIT makes changes permanent; ROLLBACK undoes them.
- The order in which operations of transactions run; serial runs one transaction at a time, concurrent interleaves them.
- Reading data written by a transaction that later rolls back.
Sources
- Official BCA syllabus Database Management Systems, course code CC202, Unit III
- Raghu Ramakrishnan and Johannes Gehrke, Database Management Systems, 3rd edition, McGraw-Hill
