🔐 Transactions
- What is a transaction in a database?
A transaction is a sequence of one or more SQL operations executed as a single unit of work.
Either all the operations are committed (saved) together, or none are applied (rolled back).- Why are transactions important?
Transactions ensure data integrity, especially in multi-user environments.
They allow multiple related operations to be treated as one atomic action, avoiding partial updates.- What are the properties of a transaction?
ACID Properties:
- Atomicity: All operations succeed or none are applied.
- Consistency: Database state remains valid after transaction.
- Isolation: Transactions are isolated from each other.
- Durability: Changes are permanently saved.Example of Transaction:
BEGIN;
UPDATE Accounts SET balance = balance - 100 WHERE account_id = 1;
UPDATE Accounts SET balance = balance + 100 WHERE account_id = 2;
COMMIT;