How do transactions ensure data consistency?
Transactions ensure data consistency because each one:
- Checks the database's rules and constraints, foreign keys, unique values,
CHECKconstraints, triggers. If even one condition is broken, the transaction never commits. - Preserves logical integrity, the data stays in a state consistent with business logic after the transaction runs.
- Uses rollbacks (
ROLLBACK), if something fails, the database returns to its last consistent state.
Example: if a transaction is supposed to save an order and deduct an item from stock, but the deduction fails, the whole process gets cancelled. So the database never ends up in a state where the order exists but the stock doesn't.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.