当前位置:网站首页>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 .
边栏推荐
- Check the debug port information in rancher and do idea remote JVM debug
- Proof of the thinking of Hanoi Tower problem
- Matlab boundarymask function (find the boundary of the divided area)
- 16 channel water lamp experiment based on Proteus (assembly language)
- mmclassification 训练自定义数据
- 互联网公司实习岗位选择与简易版职业发展规划
- What is the difference between canvas and SVG?
- Swift - enables textview to be highly adaptive
- 自动化测试生命周期
- Pytorch linear regression
猜你喜欢
【PyTorch预训练模型修改、增删特定层】
什么是数字化存在?数字化转型要先从数字化存在开始
无线WIFI学习型8路发射遥控模块
Reinforcement learning - learning notes 3 | strategic learning
How can beginners learn flutter efficiently?
ABAP table lookup program
[deploy pytoch project through onnx using tensorrt]
Multi table operation - Auto Association query
7月华清学习-1
多表操作-子查询
随机推荐
The evolution of mobile cross platform technology
[cloud native | kubernetes] actual battle of ingress case (13)
Embedded software architecture design - message interaction
Yolov 5 Target Detection Neural Network - Loss Function Calculation Principle
POJ-2499 Binary Tree
手机 CPU 架构类型了解
How can beginners learn flutter efficiently?
Pytorch weight decay and dropout
leetcode:1200. Minimum absolute difference
【ijkplayer】when i compile file “compile-ffmpeg.sh“ ,it show error “No such file or directory“.
PXE启动配置及原理
Simple production of wechat applet cloud development authorization login
pytorch-多层感知机MLP
[deploy pytoch project through onnx using tensorrt]
多表操作-自关联查询
Recyclerview paging slide
Two minutes will take you to quickly master the project structure, resources, dependencies and localization of flutter
Pytorch MLP
pytorch-softmax回归
Semantic segmentation experiment: UNET network /msrc2 dataset