Post Image
  • April 06, 2023
  • MySQL , Database

What is Trigger in MySQL?

A trigger is a set of actions that are automatically executed in response to certain events, such as INSERT, UPDATE, or DELETE operations on a table. In this blog, we will discuss triggers in MySQL and their usage.

Before we dive into the details, let's understand the basics of triggers. A trigger is a database object that is associated with a table, and it is executed automatically when a specific event occurs on the table. The event can be an INSERT, UPDATE, or DELETE operation. A trigger is defined using the CREATE TRIGGER statement, which specifies the event that triggers the execution of the trigger, the table on which the trigger is defined, and the actions to be performed when the trigger is executed.

Here's the basic syntax of creating a trigger in MySQL:

                                    CREATE TRIGGER trigger_name
                                    AFTER INSERT/UPDATE/DELETE ON table_name
                                    FOR EACH ROW
                                    BEGIN
                                    -- statements to be executed when the trigger is fired
                                    END;
                                

Let's understand each of the above parameters in detail:

  • 'trigger_name': The name of the trigger that you want to create.
  • 'AFTER INSERT/UPDATE/DELETE': The event that triggers the execution of the trigger. In this example, the trigger will be executed after an INSERT/UPDATE/DELETE operation is performed on the table.
  • 'table_name': The name of the table on which the trigger is defined.
  • 'FOR EACH ROW': Specifies that the trigger is executed once for each row affected by the trigger event.
  • 'BEGIN...END': The body of the trigger that contains the statements to be executed when the trigger is fired.
  • Now, let's see an example of creating a trigger in MySQL. Suppose we have a table named employees that contains the details of employees in a company, and we want to log the changes made to the table in a separate table named employees_log. We can achieve this by creating a trigger on the employees table that inserts a row in the employees_log table every time an INSERT, UPDATE or DELETE operation is performed on the employees table.

                                        CREATE TRIGGER log_employee_changes
                                        AFTER INSERT/UPDATE/DELETE ON employees
                                        FOR EACH ROW
                                        BEGIN
                                        IF (NEW.id IS NOT NULL) THEN
                                        INSERT INTO employees_log (employee_id, operation_type, operation_date)
                                        VALUES (NEW.id, 'INSERT/UPDATE/DELETE', NOW());
                                        END IF;
                                        END;
                                    

    In the above example, we have created a trigger named log_employee_changes that is executed after an INSERT/UPDATE/DELETE operation is performed on the employees table. The trigger checks whether the id column of the affected row is not null, and if it is not null, it inserts a row in the employees_log table with the employee ID, the type of operation performed (INSERT/UPDATE/DELETE), and the current date and time.

    Triggers in MySQL provide a powerful mechanism to enforce business rules, audit data changes, and perform other tasks automatically. However, they should be used judiciously as poorly designed triggers can have a negative impact on the performance of the database.