当前位置:网站首页>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 .
边栏推荐
- [yolov5.yaml parsing]
- Redis master-slave mode
- Thoughts and suggestions on the construction of intelligent management and control system platform for safe production in petrochemical enterprises
- 图像超分实验:SRCNN/FSRCNN
- 无线WIFI学习型8路发射遥控模块
- 【上采样方式-OpenCV插值】
- Yolov 5 Target Detection Neural Network - Loss Function Calculation Principle
- Multi table operation - sub query
- abap查表程序
- Pytorch weight decay and dropout
猜你喜欢
Uniapp + unicloud + Unipay realize wechat applet payment function
【 YOLOv3中Loss部分计算】
Two minutes will take you to quickly master the project structure, resources, dependencies and localization of flutter
Reinforcement learning - learning notes 3 | strategic learning
pytorch-多层感知机MLP
[singleshotmultiboxdetector (SSD, single step multi frame target detection)]
【PyTorch预训练模型修改、增删特定层】
Automated test lifecycle
ABAP table lookup program
1个插件搞定网页中的广告
随机推荐
Complete activity switching according to sliding
互联网公司实习岗位选择与简易版职业发展规划
Ncp1342 chip substitute pn8213 65W gallium nitride charger scheme
[calculation of loss in yolov3]
[untitled]
Recyclerview paging slide
[mainstream nivida graphics card deep learning / reinforcement learning /ai computing power summary]
JS for loop number exception
Which domestic cloud management platform manufacturer is good in 2022? Why?
2022年国内云管平台厂商哪家好?为什么?
Check the debug port information in rancher and do idea remote JVM debug
Use and install RkNN toolkit Lite2 on itop-3568 development board NPU
Four operations and derivative operations of MATLAB polynomials
Video networkState 属性
【ijkplayer】when i compile file “compile-ffmpeg.sh“ ,it show error “No such file or directory“.
JS for循环 循环次数异常
MVVM framework part I lifecycle
byte2String、string2Byte
Redis master-slave mode
pytorch-权重衰退(weight decay)和丢弃法(dropout)