Welcome to our deep dive into SQL ACID Properties! Let's explore these fundamental concepts that ensure the reliability and consistency of your database transactions.
ACID is an acronym that stands for Atomicity, Consistency, Isolation, and Durability. These properties are designed to maintain the stability and integrity of database operations.
Atomicity guarantees that a database transaction is treated as a single, indivisible unit. If any part of the transaction fails, the entire transaction is rolled back, ensuring data consistency.
-- Begin transaction
BEGIN;
-- Transfer funds
UPDATE accounts SET balance = balance - 100 WHERE account_id = 1;
UPDATE accounts SET balance = balance + 100 WHERE account_id = 2;
-- Commit transaction if no errors
COMMIT;š Note: In this example, the transfer of funds between two accounts is treated as a single transaction. If any part of the operation fails (e.g., insufficient funds), the entire transaction will be rolled back, leaving both accounts untouched.
Consistency ensures that a transaction brings the database from one valid state to another. This means that after a transaction is completed, the database remains in a consistent state that adheres to established rules and constraints.
-- Create table with a check constraint
CREATE TABLE employees (
id INT PRIMARY KEY,
salary INT CHECK (salary >= 30000)
);
-- Insert a valid record
INSERT INTO employees (id, salary) VALUES (1, 35000);
-- Insert an invalid record
-- The transaction will be rolled back due to the check constraint violation
INSERT INTO employees (id, salary) VALUES (2, 29000);š Note: In this example, the employees table has a check constraint that ensures salaries are above 30,000. When trying to insert a salary below the minimum, the transaction will be rolled back, maintaining the consistency of the database.
Isolation ensures that concurrent transactions do not interfere with each other. This means that each transaction is isolated from others, and the results of one transaction do not affect the results of another.
-- Transaction 1: Transfer funds from account 1 to account 2
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE account_id = 1;
UPDATE accounts SET balance = balance + 100 WHERE account_id = 2;
COMMIT;
-- Transaction 2 (concurrently): Transfer funds from account 1 to account 3
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE account_id = 1;
UPDATE accounts SET balance = balance + 100 WHERE account_id = 3;
COMMIT;š Note: In this example, the isolation property ensures that each transaction is executed independently, even if they both access the same account (account 1).
Durability guarantees that once a transaction has been committed, it will be permanently stored on stable storage, and it can be recovered in case of system failure.
-- Transaction: Create a new record in the accounts table
BEGIN;
INSERT INTO accounts (account_id, balance) VALUES (3, 5000);
COMMIT;š Note: After this transaction is committed, the new record will be permanently stored on stable storage, ensuring its durability.
Which ACID property ensures that each transaction is treated as a single, indivisible unit?
With this introduction to SQL ACID properties, you now have a solid understanding of the essential principles that maintain the reliability and consistency of your database operations. Keep practicing, and happy coding! š