What operations need to be performed to create a trigger in MySQL?

what is a trigger

A trigger is a special stored procedure. Like a stored procedure, a trigger is a SQL fragment that can complete a specific function and is stored on the database server, but the trigger does not need to be called. When the DML operation is performed on the data in the database table, the execution of this SQL fragment is automatically triggered without manual call . This feature of the trigger can assist the application to ensure data integrity, log records, data verification and other operations on the database side. In MySQL, triggers can only be executed when insert, delete, and update operations are executed.

Use the aliases OLD and NEW to refer to the changed record content in the trigger, similar to other databases. Currently, triggers only support row-level triggers, not statement-level triggers.
what is a trigger

create trigger

Create a trigger with only one execution statement, the specific code is as follows:

create trigger 触发器名 before|after 触发事件
on 表名 for each row 
执行语句;

The specific code for creating a trigger with multiple execution statements is as follows:

create trigger 触发器名 before|after  触发事件 
on 表名 for each row
begin
     执行语句列表
end;
-- 数据准备
create database if not exists mydb10_trigger;
use mydb10_trigger;
 
-- 用户表
create table user(
    uid int primary key ,
    username varchar(50) not null,
    password varchar(50) not null
);
-- 用户信息操作日志表
create table user_logs(
    id int primary key auto_increment,
    time timestamp,
    log_text varchar(255)
);
-- 如果触发器存在,则先删除
drop trigger if  exists trigger_test1;
 
-- 创建触发器trigger_test1
create trigger trigger_test1
after insert on user -- 触发时机:当添加user表数据时触发
for each row
insert into user_logs values(NULL,now(), '有新用户注册');
 
-- 添加数据,触发器自动执行并添加日志代码
insert into user values(1,'张三','123456');
-- 如果触发器trigger_test2存在,则先删除
drop trigger if exists trigger_test2;

-- 创建触发器trigger_test2
delimiter $$
create trigger trigger_test2
after update on user  -- 触发时机:当修改user表数据时触发
for each row -- 每一行
begin
insert into user_logs values(NULL,now(), '用户修改发生了修改');
end $$
 
delimiter ;
 
-- 添加数据,触发器自动执行并添加日志代码
update user set password = '888888' where uid = 1;

Operation -NEW and OLD

New and OLD are defined in MySQL, which are used to indicate the row of data that triggered the trigger in the table where the trigger is located, and to refer to the record content that has changed in the trigger, specifically:
1667208206301_trigger.png

How to use: NEW.columnName (columnName is a column name in the corresponding data table) The sample code is as follows:

create trigger trigger_test3 after insert
on user for each row
insert into user_logs values(NULL,now(),concat('有新用户添加,信息为:',NEW.uid,NEW.username,NEW.password));
 
-- 测试
insert into user values(4,'赵六','123456');

You can use the following code to view the trigger:

show triggers;
删除触发器可以使用入下代码:

-- drop trigger [if exists] trigger_name 
drop trigger if exists trigger_test1;

Notice:

1. The triggers in MYSQL cannot perform insert, update, and delete operations on this table to avoid recursive loop triggers.

2. Try to use triggers as little as possible. Assuming that the trigger triggers each execution for 1s and insert table 500 pieces of data, then the trigger needs to be triggered 500 times. The execution time of the trigger alone takes 500s, and the total of insert 500 pieces of data If it is 1s, then the efficiency of this insert is very low.

3. Triggers are for each row; remember not to use triggers on tables that are frequently added, deleted, or modified, because it will consume a lot of resources.

Guess you like

Origin blog.csdn.net/cz_00001/article/details/131131570