Categories: DBMS

SQL: TCL Commands

What is TCL?

Transactional control commands(TCL) can only be used with DML commands like INSERT, UPDATE, and DELETE. They cannot be used when creating or deleting tables because the database automatically commits these operations.

NOTE: Before proceeding with TCL commands, you must know about the DML & DDL commands.

COMMIT command:

The Commit command is used to save any Transaction into the database permanently. Once you commit a transaction, it cannot be undone. Therefore, it is important to ensure that the changes made during the transaction are correct and have been properly validated before executing the COMMIT command.

Syntax:

COMMIT;

What do we mean by a Transaction?

A Transaction is simply any operation we perform on a Table.

Example:

Consider a products table:

IDITEMPRICE
1Basketball899.00
2Desk1200.00
3Chair700.00
products
BEGIN TRANSACTION;
UPDATE products SET PRICE = 799.99 WHERE ID = 1;
UPDATE products SET PRICE = 1900.99 WHERE ID = 2;
COMMIT;

In this illustration, a new transaction is started by the BEGIN TRANSACTION command, and the products table is updated twice by the two UPDATE statements. These changes are permanently saved to the database by the COMMIT command.

Output:

IDITEMPRICE
1Basketball799.99
2Desk1900.99
3Chair700.00
products
ROLLBACK Command:

In SQL, the ROLLBACK command is used to undo transactional changes and restore the database to its previous state. However, it can only be used before executing a COMMIT command. Once a transaction is committed, the changes made during the transaction are final and cannot be reversed.

Syntax:

ROLLBACK [To SAVEPOINT_NAME];
Example:

Considering the products table:

BEGIN TRANSACTION;
UPDATE products SET PRICE = PRICE - 100 WHERE ID = 1;
UPDATE products SET PRICE = PRICE + 100 WHERE ID = 2;
ROLLBACK;

In this illustration, the two UPDATE statements update the products table while the BEGIN TRANSACTION command initiates a new transaction. The ROLLBACK command reverses these modifications and returns the database to its original state.

SAVEPOINT Command:

A SAVEPOINT is a place in a transaction where a marker can be added to allow us to go back to a particular point in the transaction without going back completely. When there are multiple steps in a transaction, and we want to undo just a few of them without rolling back the entire transaction and starting over.

Syntax:

SAVEPOINT SAVEPOINT_NAME;
Example:
BEGIN TRANSACTION;
UPDATE products SET ITEM = 'Volleyball' WHERE ID = 1;
SAVEPOINT update_items;
DELETE FROM products WHERE ID=2;
SAVEPOINT delete_item;
COMMIT;

The above example updates the products table as well as deletes the item where ID is 2, with savepoints update_items & delete_item. The following output will b generated:

IDITEMPRICE
1Volleyball799.99
3Chair700.00
products

Now we will use the ROLLBACK command.

ROLLBACK TO update_items;

Output:

IDITEMPRICE
1Volleyball799.99
2Desk1900.99
3Chair700.00
products

So this is how the SAVEPOINT could be used with the ROLLBACK command.

Note: also read about SQL: DELETE Query

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago