Transactions are integral to maintaining data integrity in databases, and Transaction Control Commands (TCCs) provide mechanisms to manage them effectively.
A transaction is a sequence of one or more SQL operations executed as a single logical unit. A transaction must comply with ACID properties:
Example Scenario: Transferring money between bank accounts involves:
Transaction Control Commands are SQL statements used to manage transactions. These commands ensure proper execution, rollback on failure, and checkpoint creation within transactions.
A transaction typically goes through the following states:
ROLLBACK
.Purpose: Saves all changes made during the transaction permanently.
COMMIT;
BEGIN TRANSACTION;
INSERT INTO accounts (account_id, balance) VALUES (101, 5000);
UPDATE accounts SET balance = balance + 1000 WHERE account_id = 101;
COMMIT;
Output: All changes are saved to the database.
Purpose: Reverts all changes made during the transaction.
ROLLBACK;
BEGIN TRANSACTION;
INSERT INTO accounts (account_id, balance) VALUES (102, 7000);
UPDATE accounts SET balance = balance - 1000 WHERE account_id = 999; -- Intentional error
ROLLBACK;
Output: No changes are applied due to the error.
Purpose: Creates a checkpoint in a transaction to which you can roll back.
SAVEPOINT savepoint_name;
BEGIN TRANSACTION;
INSERT INTO accounts (account_id, balance) VALUES (103, 8000);
SAVEPOINT sp1;
UPDATE accounts SET balance = balance + 500 WHERE account_id = 103;
ROLLBACK TO sp1; -- Undo the update but keep the insert
COMMIT;
Output: Only the INSERT
operation is saved.
Purpose: Deletes a previously created savepoint.
RELEASE SAVEPOINT savepoint_name;
BEGIN TRANSACTION;
SAVEPOINT sp1;
UPDATE accounts SET balance = balance + 500 WHERE account_id = 103;
RELEASE SAVEPOINT sp1; -- Savepoint is removed
COMMIT;
Purpose: Configures transaction properties like isolation level.
SET TRANSACTION [READ WRITE | READ ONLY];
SET TRANSACTION READ ONLY;
SELECT * FROM accounts;
COMMIT;
Output: Ensures the transaction only reads data.
Some systems support transactions within transactions. Nested transactions are rolled back independently of the parent transaction.
Savepoints are useful in long transactions to handle partial rollbacks without undoing the entire transaction.
Distributed transactions involve multiple databases. Tools like Two-Phase Commit (2PC) ensure ACID compliance across systems.
BEGIN TRANSACTION;
INSERT INTO accounts (account_id, balance) VALUES (201, 10000);
SAVEPOINT sp1;
UPDATE accounts SET balance = balance - 200 WHERE account_id = 201;
SAVEPOINT sp2;
ROLLBACK TO sp1; -- Undo the deduction
COMMIT;
COMMIT
only after verifying the transaction’s success.SAVEPOINT
for granular control in complex transactions.Transaction Control Commands provide powerful tools for managing and maintaining data integrity in SQL databases. By mastering these commands, developers can handle complex scenarios with confidence. Whether you’re creating checkpoints with SAVEPOINT, undoing changes with ROLLBACK, or ensuring proper isolation with SET TRANSACTION, these tools ensure that your data remains consistent and reliable. Happy Coding!❤️