MySQL trigger usage guide

1. Introduction

Triggers are database objects related to tables, which trigger and execute the set of SQL statements defined in the trigger before or after insert/update/delete. This feature of triggers can assist applications in ensuring data integrity, logging, data verification and other operations on the database side.
Use the aliases OLD and NEW to refer to the changed record content in the trigger, which is similar to other databases. Currently, triggers only support row-level triggering (for example, if a statement affects 5 rows, it will be triggered 5 times), but does not support statement-level triggering (for example, if a statement affects 5 rows, it will be triggered once).

Trigger type NEW and OLD
INSERT NEW represents data that will be or has been added
UPDATE OLD represents the data before modification, NEW represents the data that will be or has been modified.
DELETE OLD indicates data that will be or has been deleted

 

2. Trigger-Syntax
 

CREATE
TRIGGER trigger_name

BEFORE/AFTER INSERT/UPDATE/DELETEON

tbl_name FOR EACH ROW --row-level trigger BEGIN

BEGIN

        trigger_stmt ;

END;

View
SHOW TRIGGERS ;


Delete
DROP TRIGGER [schema_name]trigger_name; --If schema_name is not specified, the default is the current database.
 

3. Trigger-Case 1 (insert type)

--Insert data trigger as follows:
create trigger tb_user_insert_trigger
after insert on tb_user for each row

begin

insert into user_logs(id, operation, operate_time, operate_id, operate_params) VALUES(null, 'insert ', now(), new.id, concat( 'The inserted data content is: id=', new.id, ', name =' ,new.name) );

end;

--View triggers

SHOW TRIGGERS ;

--delete trigger

DROP TRIGGER tb_user_insert_trigger

4. Trigger-Case 2 (update type)

--The update data trigger is as follows:
create trigger tb_user_update_trigger
after update on tb_user for each row

begin

insert into user_logs(id, operation, operate_time, operate_id, operate_params) VALUES(null, 'update ', now(), new.id, concat('Data before update: id=', old.id, ', name= ' , old.name, 'Updated data: id=' , new.id , ' , name=' , new.name) );

end;

--View triggers

SHOW TRIGGERS ;

--delete trigger

DROP TRIGGER tb_user_update_trigger

5. Trigger-Case 3 (delete type)

--The delete data trigger is as follows:
create trigger tb_user_delete_trigger
after deleteon tb_user for each row

begin

insert into user_logs(id, operation,operate_time,operate_id,operate_params)VALUES(null, 'delete', now(),old.id, concat('删除前的数据: id=' , old.id , ' , name=' ,old.name) );

end;

--View triggers

SHOW TRIGGERS ;

--delete trigger

DROP TRIGGER tb_user_delete_trigger

6. Summary of triggers

MySQL triggers are stored procedures that execute on a specific table, usually when data is inserted, updated, and deleted. Triggers allow you to execute custom logic before and after database operations, such as validating data, logging, or updating data in other related tables.

Create a MySQL trigger:

CREATE TRIGGER trigger_name
{BEFORE | AFTER} {INSERT | UPDATE | DELETE} ON table_name
FOR EACH ROW
BEGIN
    -- 触发器逻辑
END;

Trigger Name: A unique name you give your trigger.
BEFORE / AFTER: Specifies that the trigger fires before or after the operation.
INSERT / UPDATE / DELETE: Specify the trigger to fire when an insert, update, or delete operation is performed.
table_name: The name of the table to which the trigger belongs.
FOR EACH ROW: Indicates that the trigger will execute once for each affected row.

Write the trigger logic between BEGIN and END. You can use the NEW keyword to reference a new value that is inserted or updated, and the OLD keyword to reference an old value that is updated or deleted.

For example, the following example is a simple trigger case:

Each time a new row is inserted into the "orders" table, the "last_update" column is automatically updated with the current time:

CREATE TRIGGER update_last_update
AFTER INSERT ON orders
FOR EACH ROW
BEGIN
    UPDATE orders
    SET last_update = NOW()
    WHERE id = NEW.id;
END;

This is just a simple example, you can actually write more complex trigger logic to suit your needs.

Guess you like

Origin blog.csdn.net/weixin_55772633/article/details/132779764