TCL (Transaction Control Language)

TCL (Transaction Control Language) commands are used to manage transactions, which are a series of database operations treated as a single unit. These commands ensure data integrity by controlling how changes are committed or rolled back.

COMMIT

Makes all changes performed within a transaction permanent. This saves the changes to the database.

  • Syntax

COMMIT;
  • Example

-- Insert data into two tables within a transaction
BEGIN
  INSERT INTO Orders (OrderID, CustomerID, OrderDate) VALUES (2023051401, 1001, SYSDATE);
  INSERT INTO OrderItems (OrderID, ProductID, Quantity) VALUES (2023051401, 1234, 2);
  COMMIT;
END;

ROLLBACK

Undoes all changes made within a transaction, reverting the database to its state before the transaction began.

  • Syntax

ROLLBACK;
  • Example

In Oracle and most relational database systems, transactions do function as atomic units. This means all changes within a transaction are treated as a single entity. If any statement within the transaction fails due to an exception, the entire transaction automatically rolls back, undoing all the changes attempted within that transaction block.

SAVEPOINT

Creates a named point within a transaction. We can optionally roll back to a specific savepoint instead of the entire transaction.

  • Syntax

  • Example

SET TRANSACTION

Defines isolation level and other transaction characteristics. Used for advanced control over transaction behavior.

  • Syntax

  • Example

Last updated