Transaction control commands in SQL are used to manage changes made by DML operations like INSERT, UPDATE, and DELETE. They help ensure data consistency and integrity by grouping multiple operations into a single logical unit — meaning either all changes succeed or none of them do.
The main transaction control commands are COMMIT, ROLLBACK, and SAVEPOINT.
For example, let’s say I’m transferring money between two bank accounts. I’ll first deduct the amount from one account and then add it to another. Both actions must succeed together. I’d handle it like this:
BEGIN TRANSACTION;
UPDATE Accounts
SET Balance = Balance - 500
WHERE AccountID = 101;
UPDATE Accounts
SET Balance = Balance + 500
WHERE AccountID = 102;
COMMIT;
If both updates are successful, I’ll use COMMIT to make the changes permanent. But if something goes wrong — say, the second update fails — I can use ROLLBACK to undo everything and bring the data back to its previous state:
ROLLBACK;
There’s also SAVEPOINT, which lets me create a checkpoint inside a transaction. That’s useful when I want to roll back part of a transaction instead of the whole thing. For example:
SAVEPOINT BeforeUpdate;
UPDATE Employees SET Salary = Salary * 1.10;
ROLLBACK TO BeforeUpdate;
One challenge I faced was when long transactions locked multiple tables and blocked other users’ operations. To handle that, I optimized queries, committed frequently, or broke the process into smaller transactions.
A limitation is that transaction control applies only to DML statements, not DDL like CREATE or DROP, since those are auto-committed in most databases. But overall, these commands are essential for maintaining data reliability in multi-step operations.
