4.7 KiB
SQL Transaction Control
Transaction Control Language (TCL) manages transactions within a database, ensuring data consistency and reliability. A transaction is a logical unit of work comprising one or more SQL statements, executed co-relationally against data.
TCL Commands
COMMITROLLBACKSAVEPOINT
COMMIT
The COMMIT command final izes a transaction. All data modifications made within the transaction become permanent and visible to other database sessions. Executing COMMIT guarantees data persistence up to that point.
Syntax
COMMIT;
ROLLBACK
ROLLBACK discards all changes made since the transaction started or since the last SAVEPOINT. It restores the database to a previous state, effectively undoing operations within the transaction. This is crucial for error handling and partial transaction reversal.
Syntax
ROLLBACK TO savepoint-name;
Note: Without the TO savepoint-name clause, ROLLBACK undoes the entire current transaction.
SAVEPOINT
A SAVEPOINT establishes a marker within a transaction. You can ROLLBACK to a specific savepoint, partially undoing changes while retaining prior modifications within the same transaction. This allows fine-grained control over transaction scope.
Syntax
SAVE POINT savepoint-name;
Examples
Using the purchase table to demonstrate TCL command behavior.
| item | price | customer_name |
|---|---|---|
| Pen | 10 | Troy |
| Bag | 1000 | Troy |
| Vegetables | 500 | Troy |
| Shoes | 5000 | Troy |
| Water Bottle | 800 | XYZ |
| Mouse | 120 | ABC |
| Sun Glasses | 1350 | ABC |
Update the price of "Pen" from 10 to 20.
UPDATE purchase SET price = 20 WHERE item = "Pen";
Output indicates success. The change is applied (though not necessarily permanent yet depending on database settings). ``` Query OK, 1 row affected (...)
View the updated table state.
```sql
SELECT * FROM purchase;
| item | price | customer_name |
|---|---|---|
| Pen | 20 | Troy |
| Bag | 1000 | Troy |
| Vegetables | 500 | Troy |
| Shoes | 5 000 | Troy |
| Water Bottle | 800 | XYZ |
| Mouse | 120 | ABC |
| Sun Glasses | 1350 | ABC |
Begin a transaction explicitly (some databases default to autocommit).
START TRANSACTION;
Commit the changes made so far within this explicit or implicit transaction. This makes the "Pen" price update (10 to 20) permanent.
COMMIT;
Attempting to rollback now has no effect on the committed transaction.
ROLLBACK;
Querying the table shows the committed change persists.
SELECT * FROM purchase;
| item | price | customer_name |
|---|---|---|
| Pen | 20 | Troy |
| Bag | 1000 | Troy |
| Vegetables | 500 | Troy |
| Shoes | 5000 | Troy |
| Water Bottle | 800 | XYZ |
| Mouse | 120 | ABC |
| Sun Glasses | 1350 | ABC |
Create a savepoint named sv_update in the current transaction. Changes before this point are protected from rollbacks targeting this savepoint.
SAVEPOINT sv_update;
Update the price of "Pen" again, from 20 to 30. This change occurs after the sv_update savepoint .
UPDATE purchase SET price = 30 WHERE item = "Pen";
Output indicates success.
Query OK, 1 row affected (...)
View the table state showing the new price (30).
SELECT * FROM purchase;
| item | price | customer_name |
|---|---|---|
| Pen | 30 | Troy |
| Bag | 1000 | Troy |
| Vegetables | 500 | Troy |
| Shoes | 5000 | Troy |
| Water Bottle | 800 | XYZ |
| Mouse | 120 | ABC |
| Sun Glasses | 1350 | ABC |
Rollback the transaction specifically to the sv_update savepoint. This undoes the price change from 20 to 30, but not the prior change from 10 to 20 because it was committed.
ROLLBACK to sv_update;
Querying the table shows the state after the partial rollback. The price of "Pen" is restored to 20, as this was the state at the sv_update savepoint.
SELECT * FROM purchase;
| item | price | customer_name |
|---|---|---|
| Pen | 20 | Troy |
| Bag | 1000 | Troy |
| Vegetables | 500 | Troy |
| Shoes | 5000 | Troy |
| Water Bottle | 800 | XYZ |
| Mouse | 120 | ABC |
| Sun Glasses | 1350 | ABC |