Member-only story
Transaction management is a crucial aspect of database systems, including those that use SQL (Structured Query Language). A transaction is a sequence of one or more SQL statements that are executed as a single unit of work. The primary goal of transaction management is to ensure the consistency and integrity of the database despite failures or errors that may occur during the execution of SQL statements.
Here are the key concepts and commands related to transaction management in SQL:
I. ACID Properties:
1. Atomicity:
- A transaction is atomic, meaning that it is treated as a single, indivisible unit of work. Either all of its operations are executed, or none of them are.
-- Atomicity example
BEGIN TRANSACTION;
UPDATE bank_accounts
SET balance = balance - 100
WHERE account_id = 123;
UPDATE bank_accounts
SET balance = balance + 100
WHERE account_id = 456;
-- If any of the above updates fail, the entire transaction will be rolled back
COMMIT;
-- or
ROLLBACK;
2. Consistency:
- The database must remain in a consistent state before and after the transaction. The integrity constraints should not be violated.
-- Consistency example
BEGIN TRANSACTION;
-- Assume a check constraint ensures that…