Home >Database >Mysql Tutorial >How to create and manage triggers for MySQL database?

How to create and manage triggers for MySQL database?

王林
王林Original
2023-07-14 11:42:162218browse

How to create and manage triggers for MySQL database?

In the MySQL database, a trigger (Trigger) is a special stored procedure that is automatically executed when a specified database operation occurs. By using triggers, we can define some automated behaviors on database tables, such as performing a series of operations when inserting, updating, or deleting data.

This article will teach you how to create and manage triggers in a MySQL database and provide some code examples to help you understand better.

1. Create a trigger

  1. First, we need to connect to the MySQL database. It can be connected through command line tools or graphical interface tools.
  2. Before creating a trigger, we need to create a table to demonstrate. Let's assume there is a table named "users" with the following columns:

    CREATE TABLE users (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50),
    age INT
    );

  3. To create a trigger, you need to use the "CREATE TRIGGER" statement, and specify the name of the trigger, the triggering time (BEFORE or AFTER) and the triggered database operation (INSERT, UPDATE or DELETE).

    The following is an example to create a trigger that automatically sets the "age" column to the default value = 18 before inserting data:

    DELIMITER //

    CREATE TRIGGER before_insert_users
    BEFORE INSERT ON users
    FOR EACH ROW
    BEGIN
    SET NEW.age = 18;
    END;
    //

    DELIMITER ;

  4. The name of the trigger must be unique. "BEFORE INSERT" means that the trigger is triggered before inserting data, and "FOR EACH ROW" means that the trigger will be triggered for each row.
  5. After creating a trigger, you can use the "SHOW TRIGGERS" statement to view the created triggers.

2. Manage triggers

  1. Update triggers: If you need to modify an existing trigger, you can use the "ALTER TRIGGER" statement.

    The following is an example, modify the previously created trigger so that it sets the "age" column to the default value = 20 when inserting data:

    DELIMITER //

    ALTER TRIGGER before_insert_users
    BEFORE INSERT ON users
    FOR EACH ROW
    BEGIN
    SET NEW.age = 20;
    END;
    //

    DELIMITER ;

  2. Delete a trigger: If a trigger is no longer needed, you can use the "DROP TRIGGER" statement to delete it.

    Here is an example to delete a previously created trigger:

    DROP TRIGGER before_insert_users;

  3. Disable trigger: Sometimes we don’t want to trigger execution, can be disabled using the "DISABLE TRIGGER" statement.

    The following is an example to disable a previously created trigger:

    DISABLE TRIGGER before_insert_users;

    To enable a trigger, you can use the "ENABLE TRIGGER" statement.

3. Trigger code example

The following is an example of creating a trigger on the "orders" table to automatically generate an order number when inserting data. :

DELIMITER //

CREATE TRIGGER before_insert_orders
BEFORE INSERT ON orders
FOR EACH ROW
BEGIN
SET NEW.order_no = CONCAT('ORD', LPAD(NEW.id, 5, '0'));
END;
//

DELIMITER ;

In the above example, the trigger uses MySQL's built-in characters String functions CONCAT and LPAD to generate order numbers. The CONCAT function is used to concatenate strings, and the LPAD function is used to pad zeros in front of a number to a specified length.

Summary:

Through triggers, we can achieve more flexible and automated database operations. In practical applications, triggers can be used to record logs, verify data integrity, automatically calculate field values, etc. When creating and managing triggers, you need to pay attention to the uniqueness of the trigger name, as well as the definition of trigger timing and trigger operations. I hope this article can help you better understand and use the trigger function in the MySQL database.

The above is the detailed content of How to create and manage triggers for MySQL database?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn