当前位置:网站首页>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 .
边栏推荐
- [deploy pytoch project through onnx using tensorrt]
- 调查显示传统数据安全工具在60%情况下无法抵御勒索软件攻击
- HiEngine:可媲美本地的云原生内存数据库引擎
- Matlab struct function (structure array)
- Simply solve the problem that the node in the redis cluster cannot read data (error) moved
- 什么是数字化存在?数字化转型要先从数字化存在开始
- What is the difference between canvas and SVG?
- 【load dataset】
- Redirection of redis cluster
- July Huaqing learning-1
猜你喜欢
Read and understand the rendering mechanism and principle of flutter's three trees
Matlab boundarymask function (find the boundary of the divided area)
Yolov5 target detection neural network -- calculation principle of loss function
Linux Installation and deployment lamp (apache+mysql+php)
Embedded software architecture design - message interaction
mmclassification 训练自定义数据
Matlab imoverlay function (burn binary mask into two-dimensional image)
Redis cluster (master-slave) brain crack and solution
报错ModuleNotFoundError: No module named ‘cv2.aruco‘
Principle of redis cluster mode
随机推荐
ABAP table lookup program
How to clear floating?
Intern position selection and simplified career development planning in Internet companies
II. Data type
Why learn harmonyos and how to get started quickly?
【SingleShotMultiBoxDetector(SSD,单步多框目标检测)】
Take you two minutes to quickly master the route and navigation of flutter
Get all stock data of big a
Codeforces Round #804 (Div. 2)
2022年国内云管平台厂商哪家好?为什么?
【ijkplayer】when i compile file “compile-ffmpeg.sh“ ,it show error “No such file or directory“.
投资理财适合女生吗?女生可以买哪些理财产品?
Recyclerview paging slide
1 plug-in to handle advertisements in web pages
Read and understand the rendering mechanism and principle of flutter's three trees
Image hyperspectral experiment: srcnn/fsrcnn
调查显示传统数据安全工具在60%情况下无法抵御勒索软件攻击
Flutter2 heavy release supports web and desktop applications
MySQL splits strings for conditional queries
Error modulenotfounderror: no module named 'cv2 aruco‘