Table of Contents

    TCL Commands

     MYSQL TRANSACTIONS

    TCL Commands in MySQL

    Learn how to save, undo, and control database transactions using COMMIT, ROLLBACK, SAVEPOINT, and more.

     What are TCL Commands?

    TCL stands for Transaction Control Language. It is a category of SQL commands used to manage transactions in a database.

    In simple words, TCL commands are used to control whether the changes made by SQL statements should be saved permanently or cancelled.

      TCL commands control the final result of database changes made by DML commands like INSERT, UPDATE, and DELETE.
    CORE IDEA
    TCL = Save Changes + Undo Changes + Manage Transactions

     What is a Transaction?

    A transaction is a group of one or more SQL operations that are treated as a single unit of work.

    This means either all operations should be completed successfully, or none of them should be applied. This helps maintain accuracy and consistency in the database.

      A transaction protects the database from partial updates and incomplete operations.

     Real-Life Analogy

    Bank Money Transfer Example

    Suppose money is transferred from Account A to Account B. First, money is deducted from Account A, and then the same amount is added to Account B. Both actions must happen together. If one action fails, the whole transaction should be cancelled.

     Correct Transaction

    • Amount deducted from Account A
    • Amount added to Account B
    • Changes saved using COMMIT
    • Database remains consistent

     Failed Transaction

    • Amount deducted from Account A
    • Amount not added to Account B
    • Transaction cancelled using ROLLBACK
    • Database returns to previous state

     Why Do We Need TCL Commands?

    TCL commands are important because database operations may sometimes fail due to errors, power failure, wrong queries, network problems, or application issues. If changes are saved incorrectly, data can become inconsistent.

    TCL commands help us decide whether to confirm the changes or cancel them before they become permanent.

     Main Purpose of TCL

    • To save database changes permanently
    • To undo unwanted or incorrect changes
    • To manage multiple SQL operations as one unit
    • To maintain data consistency and accuracy
    • To support safe execution of INSERT, UPDATE, and DELETE operations
    • To prevent partial updates in important operations

     Prerequisites Before Learning TCL Commands

    Before learning TCL commands, you should understand some basic SQL concepts because TCL works closely with DML commands.

     You Should Know

    • Basic SQL syntax
    • How to create and use a database
    • How to create tables
    • INSERT command
    • UPDATE command
    • DELETE command
    • Basic understanding of transactions
    • Difference between temporary changes and permanent changes
      TCL commands are mainly useful when you are performing data-changing operations such as INSERT, UPDATE, and DELETE.

     ACID Properties of Transactions

    Database transactions follow the ACID properties. These properties help make transactions reliable and safe.

    Property Full Form Meaning
    A Atomicity All operations in a transaction happen completely, or none happen at all.
    C Consistency The database moves from one valid state to another valid state.
    I Isolation Transactions should not wrongly affect each other while running together.
    D Durability Once changes are committed, they remain saved permanently.
    REMEMBER
    ACID = Reliable + Safe + Consistent Transactions

     Main TCL Commands in MySQL

    MySQL provides several transaction-related commands. The most commonly used TCL commands are COMMIT, ROLLBACK, and SAVEPOINT.

    Command Purpose Simple Meaning
    START TRANSACTION Starts a new transaction Begin transaction control
    COMMIT Saves changes permanently Confirm changes
    ROLLBACK Undoes changes Cancel changes
    SAVEPOINT Creates a checkpoint inside a transaction Mark a safe point
    ROLLBACK TO SAVEPOINT Rolls back to a specific savepoint Undo partial changes
    RELEASE SAVEPOINT Removes a savepoint Delete checkpoint
    SET autocommit Controls automatic commit behavior Turn auto-save on or off

     Important MySQL Note: Autocommit

    In MySQL, autocommit mode is enabled by default. This means each SQL statement is automatically saved unless you explicitly start a transaction.

    To manually control a transaction, you can use START TRANSACTION. After that, you can use COMMIT to save changes or ROLLBACK to cancel changes.

      If autocommit is ON, MySQL automatically commits each statement. To control multiple statements together, use START TRANSACTION.

     Check Autocommit Status

    SELECT @@autocommit;

     Disable Autocommit

    SET autocommit = 0;

     Enable Autocommit

    SET autocommit = 1;

     Storage Engine Requirement

    In MySQL, transaction control works properly with transactional storage engines such as InnoDB. If a table uses a non-transactional engine, rollback behavior may not work as expected.

      For learning TCL commands in MySQL, use InnoDB tables.

     Example Table with InnoDB

    CREATE TABLE accounts (
        account_id INT PRIMARY KEY,
        account_name VARCHAR(100),
        balance DECIMAL(10,2)
    ) ENGINE = InnoDB;

     START TRANSACTION Command

    The START TRANSACTION command is used to begin a new transaction manually. After this command, MySQL waits until you use COMMIT or ROLLBACK.

      START TRANSACTION = Begin a group of SQL operations

     Syntax

    START TRANSACTION;

     Alternative Syntax

    BEGIN;
    Explanation Both START TRANSACTION and BEGIN are used to start a transaction.

     COMMIT Command

    The COMMIT command is used to save all changes made during the current transaction permanently. Once a transaction is committed, the changes become permanent in the database.

      COMMIT = Save changes permanently

     Syntax

    COMMIT;

     Example: Insert and Commit

    START TRANSACTION;
    
    INSERT INTO accounts (account_id, account_name, balance)
    VALUES (1, 'Amit Sharma', 5000.00);
    
    COMMIT;
    Result The inserted record is permanently saved in the accounts table.

     Example: Update and Commit

    START TRANSACTION;
    
    UPDATE accounts
    SET balance = balance + 1000
    WHERE account_id = 1;
    
    COMMIT;
    Result The updated balance is permanently saved.
    Important After COMMIT, the changes cannot be undone using ROLLBACK.

     ROLLBACK Command

    The ROLLBACK command is used to undo changes made during the current transaction. It returns the database to the state it had before the transaction started.

      ROLLBACK = Cancel changes before they become permanent

     Syntax

    ROLLBACK;

     Example: Delete and Rollback

    START TRANSACTION;
    
    DELETE FROM accounts
    WHERE account_id = 1;
    
    ROLLBACK;
    Result The DELETE operation is cancelled. The record is restored because the transaction was rolled back.

     Example: Update and Rollback

    START TRANSACTION;
    
    UPDATE accounts
    SET balance = balance - 2000
    WHERE account_id = 1;
    
    ROLLBACK;
    Result The balance change is cancelled, and the old balance remains unchanged.

     SAVEPOINT Command

    The SAVEPOINT command is used to create a checkpoint inside a transaction. It allows you to roll back only part of a transaction instead of cancelling the whole transaction.

      SAVEPOINT = Create a checkpoint inside a transaction

     Syntax

    SAVEPOINT savepoint_name;

     Example

    START TRANSACTION;
    
    INSERT INTO accounts (account_id, account_name, balance)
    VALUES (2, 'Neha Verma', 7000.00);
    
    SAVEPOINT sp_first_insert;
    
    INSERT INTO accounts (account_id, account_name, balance)
    VALUES (3, 'Rahul Khan', 9000.00);
    Explanation The savepoint sp_first_insert is created after the first insert. If needed, we can roll back to this savepoint.

     ROLLBACK TO SAVEPOINT Command

    The ROLLBACK TO SAVEPOINT command is used to undo changes made after a specific savepoint. It does not cancel the entire transaction.

      ROLLBACK TO SAVEPOINT = Undo changes after a checkpoint

     Syntax

    ROLLBACK TO SAVEPOINT savepoint_name;

     Example

    START TRANSACTION;
    
    INSERT INTO accounts (account_id, account_name, balance)
    VALUES (2, 'Neha Verma', 7000.00);
    
    SAVEPOINT sp_first_insert;
    
    INSERT INTO accounts (account_id, account_name, balance)
    VALUES (3, 'Rahul Khan', 9000.00);
    
    ROLLBACK TO SAVEPOINT sp_first_insert;
    
    COMMIT;
    Result The first insert is saved, but the second insert is cancelled because we rolled back to the savepoint.

     RELEASE SAVEPOINT Command

    The RELEASE SAVEPOINT command is used to remove a savepoint from the current transaction. After releasing a savepoint, you cannot roll back to that savepoint anymore.

      RELEASE SAVEPOINT = Remove a checkpoint from the transaction

     Syntax

    RELEASE SAVEPOINT savepoint_name;

     Example

    START TRANSACTION;
    
    UPDATE accounts
    SET balance = balance + 500
    WHERE account_id = 1;
    
    SAVEPOINT sp_update_done;
    
    RELEASE SAVEPOINT sp_update_done;
    
    COMMIT;
    Explanation The savepoint is removed before committing the transaction.

     Complete Practical Example of TCL Commands

    Let us understand TCL commands with a complete bank account example.

     Step 1: Create Database

    CREATE DATABASE bank_db;

     Step 2: Use Database

    USE bank_db;

     Step 3: Create Table

    CREATE TABLE accounts (
        account_id INT PRIMARY KEY,
        account_holder VARCHAR(100),
        balance DECIMAL(10,2)
    ) ENGINE = InnoDB;

     Step 4: Insert Sample Data

    INSERT INTO accounts VALUES
    (101, 'Amit Sharma', 10000.00),
    (102, 'Neha Verma', 8000.00);

     Step 5: Transfer Money Using Transaction

    START TRANSACTION;
    
    UPDATE accounts
    SET balance = balance - 2000
    WHERE account_id = 101;
    
    UPDATE accounts
    SET balance = balance + 2000
    WHERE account_id = 102;
    
    COMMIT;
    Result Money is deducted from Amit's account and added to Neha's account. Both changes are permanently saved.

     Step 6: Cancel Transaction Using ROLLBACK

    START TRANSACTION;
    
    UPDATE accounts
    SET balance = balance - 5000
    WHERE account_id = 101;
    
    ROLLBACK;
    Result The deduction is cancelled, and Amit's balance remains unchanged.

     Practical Example Using SAVEPOINT

    SAVEPOINT is useful when a transaction has multiple steps and you want to undo only some steps.

    START TRANSACTION;
    
    UPDATE accounts
    SET balance = balance - 1000
    WHERE account_id = 101;
    
    SAVEPOINT after_deduction;
    
    UPDATE accounts
    SET balance = balance + 1000
    WHERE account_id = 102;
    
    SAVEPOINT after_deposit;
    
    UPDATE accounts
    SET balance = balance - 300
    WHERE account_id = 101;
    
    ROLLBACK TO SAVEPOINT after_deposit;
    
    COMMIT;
    Explanation The last deduction of 300 is cancelled, but the earlier deduction and deposit are saved because the transaction is committed.

     Difference Between COMMIT, ROLLBACK, and SAVEPOINT

    Command Meaning Effect Can Undo?
    COMMIT Save changes Makes changes permanent No, after commit changes cannot be rolled back
    ROLLBACK Cancel changes Undo changes in current transaction Yes, before commit
    SAVEPOINT Create checkpoint Allows partial rollback Yes, rollback to that savepoint
    ROLLBACK TO SAVEPOINT Undo partial changes Cancels changes after savepoint Yes, for selected part

     Relationship Between TCL and DML

    TCL commands do not directly insert, update, or delete data. Instead, they control the effect of DML commands.

    Category Commands Purpose
    DML INSERT, UPDATE, DELETE Changes data inside tables
    TCL COMMIT, ROLLBACK, SAVEPOINT Controls whether DML changes are saved or cancelled
    SIMPLE RELATION
    DML changes data
    TCL controls those changes

     Good and Bad Transaction Handling

     Bad Practice

    • Running multiple updates without transaction control
    • Committing after every small statement unnecessarily
    • Not using ROLLBACK when an error occurs
    • Using non-transactional tables for transaction examples
    • Mixing important transaction logic without checking results

     Good Practice

    • Use START TRANSACTION before important multi-step operations
    • Use COMMIT only when all steps are successful
    • Use ROLLBACK when an error occurs
    • Use SAVEPOINT for long or complex transactions
    • Use InnoDB tables for transaction support

     Common Mistakes in TCL Commands

    Mistake Problem Correct Approach
    Forgetting START TRANSACTION Statements may be auto-committed Use START TRANSACTION before grouped operations
    Using ROLLBACK after COMMIT Committed changes cannot be undone using ROLLBACK Rollback before COMMIT if needed
    Using MyISAM table Transaction support may not work properly Use InnoDB table
    Not checking data before COMMIT Wrong data may become permanent Verify changes using SELECT before COMMIT
    Overusing SAVEPOINT Transaction becomes confusing Use meaningful savepoint names

     Important Notes About TCL Commands

     Remember These Points

    • TCL stands for Transaction Control Language
    • TCL commands manage transactions
    • COMMIT saves changes permanently
    • ROLLBACK cancels uncommitted changes
    • SAVEPOINT creates a checkpoint inside a transaction
    • ROLLBACK TO SAVEPOINT cancels changes after a savepoint
    • In MySQL, autocommit is usually enabled by default
    • Use START TRANSACTION to manually control a transaction
    • After COMMIT, ROLLBACK cannot undo the committed changes
    • TCL commands mainly control DML operations like INSERT, UPDATE, and DELETE

     Quick Revision Table

    Command One-Line Definition
    START TRANSACTION Starts a new transaction
    COMMIT Permanently saves transaction changes
    ROLLBACK Undo uncommitted transaction changes
    SAVEPOINT Creates a checkpoint inside a transaction
    ROLLBACK TO SAVEPOINT Undo changes after a specific savepoint
    RELEASE SAVEPOINT Removes a savepoint from the transaction
    SET autocommit Turns automatic commit behavior on or off

     Interview Questions on TCL Commands

    1

    What is TCL in SQL?

    Basic definition question

    TCL stands for Transaction Control Language. It is used to manage transactions in a database. It controls whether changes should be saved permanently or cancelled.

    2

    What are the main TCL commands?

    Command-list question

    The main TCL commands are COMMIT, ROLLBACK, and SAVEPOINT. In MySQL, START TRANSACTION and SET autocommit are also commonly used for transaction control.

    3

    What is the use of COMMIT?

    Save changes question

    COMMIT is used to permanently save all changes made during the current transaction. Once COMMIT is executed, the changes cannot be undone using ROLLBACK.

    4

    What is the use of ROLLBACK?

    Undo changes question

    ROLLBACK is used to cancel uncommitted changes in the current transaction. It restores the database to the previous consistent state.

    5

    What is the use of SAVEPOINT?

    Checkpoint question

    SAVEPOINT is used to create a checkpoint inside a transaction. It allows partial rollback without cancelling the entire transaction.

    6

    Can we rollback after COMMIT?

    Important practical question

    No. Once COMMIT is executed, the changes become permanent and cannot be undone using ROLLBACK.

    7

    Which storage engine is best for TCL commands in MySQL?

    MySQL-specific question

    In MySQL, InnoDB is commonly used for transaction control because it supports transactions.

     Important Exam Points

     Most Important Points

    • TCL stands for Transaction Control Language
    • TCL commands control transactions
    • Transaction means a group of SQL operations treated as one unit
    • COMMIT is used to permanently save changes
    • ROLLBACK is used to undo uncommitted changes
    • SAVEPOINT is used to create a rollback point inside a transaction
    • ROLLBACK TO SAVEPOINT is used for partial rollback
    • START TRANSACTION begins a manual transaction in MySQL
    • Autocommit is enabled by default in MySQL
    • TCL commands are mostly used with INSERT, UPDATE, and DELETE

     Summary

    TCL commands are used to manage transactions in SQL. They help control whether changes made by DML commands should be saved permanently or undone.

    In MySQL, transactions are commonly controlled using START TRANSACTION, COMMIT, ROLLBACK, SAVEPOINT, ROLLBACK TO SAVEPOINT, and SET autocommit.

    TCL commands are extremely important in real-world applications such as banking systems, e-commerce orders, inventory management, booking systems, and any system where data accuracy is critical.

     Key Takeaway

    TCL Commands help manage database transactions by allowing us to save changes using COMMIT, cancel changes using ROLLBACK, and create checkpoints using SAVEPOINT.