MySQL Tutorial

MySQL Installation and Configuration

MySQL Database Operations

Database Design

MySQL Data Types

MySQL Storage Engines

MySQL Basic Operations of Tables

MySQL Constraints

MySQL Operators

MySQL Function

MySQL Manipulate Table Data

MySQL View

MySQL Indexes

MySQL Stored Procedure

MySQL Trigger

MySQL Transactions

MySQL Character Set

MySQL User Management

MySQL Database Backup and Recovery

MySQL Log

MySQL Performance Optimization

MySQL CREATE TRIGGER: Create Trigger

A trigger is a stored program invoked automatically in response to an event such as an INSERT, UPDATE, or DELETE that occurs in the associated table. Triggers can be very useful for maintaining complex integrity constraints, for auditing changes, or for automatically updating derived column values.

Here is a basic guide on how to create a trigger in MySQL.

Syntax:

CREATE TRIGGER trigger_name trigger_time trigger_event
ON table_name FOR EACH ROW
BEGIN
    -- SQL statements
END;

Where:

  • trigger_name: The name of the trigger.
  • trigger_time: The time when the trigger is invoked. It can be BEFORE or AFTER.
  • trigger_event: The event that activates the trigger. It can be INSERT, UPDATE, or DELETE.
  • table_name: The name of the table to which the trigger is associated.
  • SQL statements: The SQL code to be executed when the trigger is invoked.

Example:

Suppose you have a table named orders and you want to create a trigger that logs an entry into an orders_audit table every time a row is inserted into the orders table.

CREATE TRIGGER orders_after_insert
AFTER INSERT
ON orders FOR EACH ROW
BEGIN
   INSERT INTO orders_audit(order_id, audit_message) 
   VALUES (NEW.order_id, CONCAT('New order inserted with ID ', NEW.order_id));
END;

In this example, the trigger orders_after_insert is created and set to be invoked AFTER INSERT on the orders table. The NEW keyword refers to the new row being inserted into the orders table. This trigger inserts a new row into the orders_audit table with the order_id of the new order and a message noting the insertion.

Notes:

  • Triggers must be associated with a table and the SQL statements you define in the trigger must involve that table.
  • The FOR EACH ROW clause is necessary because a trigger is activated for each row of the table that is affected by the triggering event.
  • MySQL supports triggers that are invoked in response to INSERT, UPDATE, or DELETE operations.
  • You must have the TRIGGER privilege to create a trigger.
  • The BEGIN ... END block is necessary when you have more than one statement in the trigger body. If you have only one statement, you can omit the BEGIN ... END block.
  1. Creating triggers in MySQL:

    • Triggers in MySQL are database objects that automatically execute in response to specific events on a particular table.
    -- Create a trigger in MySQL
    CREATE TRIGGER trigger_name
    BEFORE INSERT ON my_table
    FOR EACH ROW
    BEGIN
        -- Trigger logic
    END;
    
  2. How to use CREATE TRIGGER in MySQL:

    • Use the CREATE TRIGGER statement in MySQL to define and create a new trigger.
    -- Create a trigger in MySQL
    CREATE TRIGGER trigger_name
    AFTER UPDATE ON my_table
    FOR EACH ROW
    BEGIN
        -- Trigger logic
    END;
    
  3. MySQL trigger example:

    • Example of creating a trigger in MySQL that updates a timestamp on each row insert.
    -- Create a trigger to update timestamp on insert
    CREATE TRIGGER update_timestamp
    BEFORE INSERT ON my_table
    FOR EACH ROW
    SET NEW.created_at = NOW();
    
  4. Defining triggers with MySQL:

    • Define triggers in MySQL using the CREATE TRIGGER statement, specifying the trigger name, event (e.g., BEFORE INSERT), and the trigger logic.
    -- Create a trigger in MySQL
    CREATE TRIGGER trigger_name
    AFTER DELETE ON my_table
    FOR EACH ROW
    BEGIN
        -- Trigger logic
    END;
    
  5. Creating and managing triggers in MySQL:

    • Create and manage triggers in MySQL by using the CREATE TRIGGER statement and altering existing triggers.
    -- Create a trigger in MySQL
    CREATE TRIGGER trigger_name
    BEFORE UPDATE ON my_table
    FOR EACH ROW
    BEGIN
        -- Trigger logic
    END;
    
    -- Alter a trigger in MySQL
    ALTER TRIGGER trigger_name
    AFTER DELETE ON my_table
    FOR EACH ROW
    BEGIN
        -- Updated trigger logic
    END;
    
  6. Using parameters in MySQL triggers:

    • Parameters in MySQL triggers allow you to pass values to the trigger logic.
    -- Create a trigger with parameters in MySQL
    CREATE TRIGGER trigger_name
    BEFORE INSERT ON my_table
    FOR EACH ROW
    BEGIN
        -- Trigger logic using NEW.parameter_name
    END;
    
  7. Executing and testing MySQL triggers:

    • Execute and test MySQL triggers by performing actions that activate the trigger and verifying the expected results.
    -- Test trigger by inserting a new row
    INSERT INTO my_table (column1, column2) VALUES ('value1', 'value2');