Introduction
ACID properties help keep data safe and correct when many people use a database at the same time.
ACID properties help keep data safe and correct when many people use a database at the same time.
ACID stands for: - Atomicity - Consistency - Isolation - Durability
Atomicity means all parts of a task happen or none do.
Consistency means data rules are always followed.
START TRANSACTION;
-- perform multiple SQL commands
COMMIT;ROLLBACK;
This moves 100 units from account 1 to account 2 safely. If any step fails, no money moves.
START TRANSACTION; UPDATE accounts SET balance = balance - 100 WHERE id = 1; UPDATE accounts SET balance = balance + 100 WHERE id = 2; COMMIT;
Isolation prevents other users from seeing partial changes during a transaction.
Durability means once changes are saved, they stay even if power goes out.
ACID ensures database tasks are safe and reliable.
Use transactions to group related changes.
ACID helps avoid errors when many users work together.
START TRANSACTION; to begin a transaction.START TRANSACTION; UPDATE accounts SET balance = balance - 100 WHERE id = 1; UPDATE accounts SET balance = balance + 100 WHERE id = 2; COMMIT;
START TRANSACTION; UPDATE inventory SET quantity = quantity - 5 WHERE product_id = 10; COMMIT;
Transaction 1: START TRANSACTION; UPDATE accounts SET balance = balance - 50 WHERE id = 1; -- waits before COMMIT Transaction 2: START TRANSACTION; SELECT balance FROM accounts WHERE id = 1; COMMIT;
Pass the test and this pattern moves unknown