MySQL trigger (advanced)

MySQL trigger

The concept and function of a trigger

1 Concept A
trigger is a database object related to a table, which refers to a set of statements defined in a trigger that is triggered and executed before and after insert, update, and delete.
2 Function
Triggers can assist applications to ensure data integrity, log records, data verification and other operations on the database side.
The aliases OLD and NEW are used to refer to the contents of the record that sends changes in the trigger. Now the trigger only supports row-level triggering, not statement-level triggering. The use of NEW and OLD of insert, update, and delete is shown in the following figure:
Insert picture description here

The creation of the second trigger

1 Grammar

create trigger trigger name
before/after insert/update/delete
on table name
[for each row] -row -level trigger
begin
trigger_stmt;-SQL statement for addition, deletion and modification
end$

2 Case
Record the data change log of the emp table through triggers, including addition, modification, and deletion;

-- 首先创建一张日志表 : 
create table emp_logs(
  id int(11) not null auto_increment,
  operation varchar(20) not null comment '操作类型, insert/update/delete',
  operate_time datetime not null comment '操作时间',
  operate_id int(11) not null comment '操作表的ID',
  operate_params varchar(500) comment '操作参数',
  primary key(`id`)
)engine=innodb default charset=utf8;

(1) Insert trigger

create trigger emp_logs_delete_trigger
after delete 
on emp 
for each row 
begin
  insert into 
  emp_logs (id,operation,operate_time,operate_id,operate_params)
  values(null,'delete',now(),old.id,
         concat('删除前(id:',old.id,', name:',old.name,', age:',old.age,', salary:',old.salary,')')
        );                                                                      
end $

(2) update trigger

create trigger emp_logs_update_trigger
after update 
on emp 
for each row 
begin
  insert into 
  emp_logs (id,operation,operate_time,operate_id,operate_params)
  values(null,'update',now(),new.id,
         concat(
             '修改前(id:',old.id,', name:',old.name,', age:',old.age,', salary:',old.salary,') , 
             修改后(id',new.id, 'name:',new.name,', age:',new.age,', salary:',new.salary,')'
         		)
        );                                                                      
end$

(3) Delete type trigger

create trigger emp_logs_delete_trigger
after delete 
on emp 
for each row 
begin
  insert into 
  emp_logs (id,operation,operate_time,operate_id,operate_params)
  values(null,'delete',now(),old.id,
         concat('删除前(id:',old.id,', name:',old.name,', age:',old.age,', salary:',old.salary,')')
        );                                                                      
end $

(4) Test

insert into emp(id,name,age,salary) values(null, '光明右使',33,3200)$

update emp set age = 39 where id = 3$

delete from emp where id = 7$

Three view triggers

Syntax: Execute the show triggers command to view the status, syntax and other information of the trigger. grammar structure

show triggers

Four delete trigger

(1) Syntax: schema_name is not specified, the default is the current database

drop trigger [schema_name.]trigger_name

Guess you like

Origin blog.csdn.net/hcyxsh/article/details/114793341