当前位置:网站首页>MySQL trigger

MySQL trigger

2022-07-05 12:13:00 ziyi813

MySQL trigger

Introduce

Trigger is a special stored procedure , A trigger, like a stored procedure, is a function that can perform a specific function , Stored on the database server SQL fragment , But the trigger does not need to call , When executing... On the data in the data table DML Touch automatically during operation Send this SQL Execution of fragments , No manual call required .

stay MySQL in , Only execute insert, delete, update The execution of the trigger can only be triggered when the operation

This feature of trigger can help to ensure the integrity of data in database , logging , Data verification and other operations

Use the alias OLD and NEW To refer to the changed record content in the trigger , This is similar to other databases . The current trigger only supports row level trigger , Statement level triggering is not supported .

The characteristics of triggers

1、 What conditions trigger : I,D,U

2、 When to trigger , Before or after the addition, deletion or modification

3、 Trigger frequency : Execute... For each line

4、 Triggers are defined on the table , Attach to table

Trigger operation

Create trigger

Format :

Triggering event : inter , delete, update

--  Create a trigger with only one execution statement 
create trigger  Trigger Name  before|after  Triggering event 
on  Table name  for each row
 Execute statement ;
--  Create a trigger with multiple execution statements 
create trigger  Trigger Name  before | after  Triggering event 
on  Table name  for each row
begin
	 Execute statement list 
end;

Example :

--  Data preparation 
create database if not exists mydb_trigger;
use mydb_trigger;

--  User table 
create table user(
	uid int primary key,
    username varchar(50) not null,
    password varchar(50) not null
);

--  User information operation   Make a log table 
create table user_logs(
	id int primary key auto_increment,
    time timestamp,
    log_text varchar(255)
);

--  demand :  When user table , Add a row of data , Will be automatically in user_logs Add logging 

--  Defining triggers 
create trigger trigger_user_inster after insert
on user for each row
insert into user_logs values(NULL, now(), 'user A new record has been added to the table ');

--  This statement will trigger to user_logs Add a new log record 
insert into user values(1,' Zhang San ','123456');

NEW And OLD

MySQL It defines NEW and OLD, Used to indicate that the trigger is in the table , The row that triggered the trigger , To refer to the changed record content in the trigger

Trigger Type Trigger Type NEW and OLD Use
INSERT Type trigger NEW Indicates the data to be added or added
UPDATE Type trigger OLD Represents the data before modification ,NEW Indicates the data to be or have been modified
DELETE Type trigger OLD Data that will be or has been deleted

Usage method :

NEW.columnName (columnName A column name for the corresponding data table )

Example :

 -- insert Type of trigger 
create trigger trigger_user_inster_new after insert
on user for each row
insert into user_logs values(NULL, now(), concat(' New users added , The message is :', NEW.uid, NEW.username, NEW.password));

insert into user values(4, ' Mango. ', '1111111');

-- update  Type of trigger 
create trigger trigger_user_update_old after update
on user for each row
insert into user_logs values(NULL, now(), concat(' User information modification , The message is :', OLD.uid, OLD.username, OLD.password," It is amended as follows :",NEW.uid, NEW.username, NEW.password));

update user set username = ' Mango. 2' where uid = 4;

Query defined triggers

--  Inquire about MYSQL Triggers defined in 
SELECT TRIGGER_SCHEMA,TRIGGER_NAME,EVENT_OBJECT_TABLE FROM information_schema.`TRIGGERS`;

TRIGGER_SCHEMA Is the name of the library 
TRIGGER_NAME  Trigger Name 
EVENT_OBJECT_TABLE  The name of the table where the trigger is located 

 Method 2:  Check triggers 
show triggers;

Delete trigger

Use DROP TRIGGER Statement can delete MySQL Triggers defined in

drop trigger if exists  Trigger Name ;

matters needing attention

  • This table cannot be insert,update,delete fuck do , Avoid recursive loops
  • Use as few triggers as possible
  • Triggers are for each row , Remember not to use triggers for tables with frequent additions, deletions and changes , Very resource intensive .
原网站

版权声明
本文为[ziyi813]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140532140500.html