Database Transaction
Context: FIT2094_MOC ยท groups DML into one logical unit ยท the frame for ACID, locking and recovery
Quick Revision
- ๐ฏ Objective: bundle several DML statements into one all-or-nothing unit โ COMMIT saves all, ROLLBACK undoes all.
- โก Key Constraint: a mid-transaction state is inconsistent; a failure between steps must ROLLBACK the earlier ones โ partial commit is never allowed.
๐ Core
- Definition โ a group of SQL operations executed completely or not at all.
- COMMIT โ save all changes permanently; ROLLBACK โ undo all changes since the last commit.
- Scope โ transaction control applies to INSERT/UPDATE/DELETE only (DDL auto-commits, see SQL Sublanguages (DDL, DML, DCL)).
- Worked case (drone return) โ (1) UPDATE
rentalreturn date/condition/emp_no_in, (2) UPDATEdroneadd flight time, (3) COMMIT โ all three are one logical operation.
โ ๏ธ Common Mistakes
- ๐ก No
COMMIT/ROLLBACKfor DDL โ they apply only to DML; aCREATE/ALTERis already permanent. - ๐ก Partial success = corruption โ if step 2 fails after step 1 commits data, the drone shows returned with no flight time logged; ROLLBACK the whole unit instead.
๐ง Active Recall
Why must a drone-return's two UPDATEs be one transaction rather than two independent statements?
Answer
- Short answer: They are all-or-nothing โ if the flight-time update fails after the rental update, the rental update must be rolled back to avoid an inconsistent record.
- Why: Atomicity โ the DBMS treats the group as one unit; COMMIT publishes both, ROLLBACK discards both.