Loading...
Loading...
00:00:00

Transaction Control Language (TCL) Commands

TCL (Transaction Control Language) is a subset of SQL that is used to manage transactions, which are sequences of database operations that are treated as a single logical unit of work.

In this response, we will explain all SQL TCL commands with examples in simple English.

  1. COMMIT: The COMMIT command is used to save changes made to the database since the beginning of a transaction. This command is typically used at the end of a transaction to ensure that all changes are permanent.
    BEGIN TRANSACTION;
    UPDATE customers SET name = 'John Smith' WHERE id = 1;
    COMMIT;
    

    In this example, the transaction is started with the BEGIN TRANSACTION command, an update operation is performed on the customers table, and the changes are committed with the COMMIT command.

  2. ROLLBACK: The ROLLBACK command is used to undo changes made to the database since the beginning of a transaction. This command is typically used when an error occurs during a transaction, to ensure that the database remains in a consistent state. 

    For Example:

    BEGIN TRANSACTION;
    UPDATE customers SET name = 'John Smith' WHERE id = 1;
    ROLLBACK;
    

    In this example, the transaction is started with the BEGIN TRANSACTION command, an update operation is performed on the customers table, and the changes are rolled back with the ROLLBACK command.

  3. SAVEPOINT: The SAVEPOINT command is used to create a savepoint within a transaction. A savepoint is a point in the transaction at which the database can be rolled back to if an error occurs.

    For Example:

    BEGIN TRANSACTION;
    UPDATE customers SET name = 'John Smith' WHERE id = 1;
    SAVEPOINT mysavepoint;
    UPDATE orders SET status = 'Cancelled' WHERE customer_id = 1;
    ROLLBACK TO mysavepoint;
    UPDATE orders SET status = 'Shipped' WHERE customer_id = 1;
    COMMIT;
    

    In this example, the transaction is started with the BEGIN TRANSACTION command, an update operation is performed on the customers table, a savepoint is created with the SAVEPOINT command, an update operation is performed on the orders table, and the changes are rolled back to the savepoint with the ROLLBACK TO command. After the rollback, another update operation is performed on the orders table, and the changes are committed with the COMMIT command.

  4. RELEASE SAVEPOINT: The RELEASE SAVEPOINT command is used to release a savepoint within a transaction. Once a savepoint is released, it cannot be rolled back to.

    For Example:

    BEGIN TRANSACTION;
    UPDATE customers SET name = 'John Smith' WHERE id = 1;
    SAVEPOINT mysavepoint;
    UPDATE orders SET status = 'Cancelled' WHERE customer_id = 1;
    RELEASE SAVEPOINT mysavepoint;
    UPDATE orders SET status = 'Shipped' WHERE customer_id = 1;
    COMMIT;
    

    In this example, the transaction is started with the BEGIN TRANSACTION command, an update operation is performed on the customers table, a savepoint is created with the SAVEPOINT command, an update operation is performed on the orders table, and the savepoint is released with the RELEASE SAVEPOINT command. After the release, another update operation is performed on the orders table, and the changes are committed with the COMMIT command.

In summary, these are the four SQL TCL commands: COMMIT, ROLLBACK, SAVEPOINT, and RELEASE SAVEPOINT. These commands are used to manage transactions and ensure that the database remains in a consistent state.