当前位置:网站首页>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 .
边栏推荐
- Splunk configuration 163 mailbox alarm
- Video networkstate property
- 【load dataset】
- Redirection of redis cluster
- Principle of persistence mechanism of redis
- pytorch-线性回归
- Acid transaction theory
- 【TFLite, ONNX, CoreML, TensorRT Export】
- Four operations and derivative operations of MATLAB polynomials
- Ncp1342 chip substitute pn8213 65W gallium nitride charger scheme
猜你喜欢

Flutter2 heavy release supports web and desktop applications

【SingleShotMultiBoxDetector(SSD,单步多框目标检测)】

Course design of compilation principle --- formula calculator (a simple calculator with interface developed based on QT)

【yolov3损失函数】

Hiengine: comparable to the local cloud native memory database engine

Reinforcement learning - learning notes 3 | strategic learning

How to clear floating?

MySQL splits strings for conditional queries

Splunk configuration 163 mailbox alarm

Why learn harmonyos and how to get started quickly?
随机推荐
Use and install RkNN toolkit Lite2 on itop-3568 development board NPU
Image hyperspectral experiment: srcnn/fsrcnn
Automated test lifecycle
信息服务器怎么恢复,服务器数据恢复怎么弄[通俗易懂]
codeforces每日5题(均1700)-第五天
互联网公司实习岗位选择与简易版职业发展规划
Matlab imoverlay function (burn binary mask into two-dimensional image)
Principle and performance analysis of lepton lossless compression
Seven polymorphisms
Pytorch MLP
The survey shows that traditional data security tools cannot resist blackmail software attacks in 60% of cases
Halcon template matching actual code (I)
跨平台(32bit和64bit)的 printf 格式符 %lld 输出64位的解决方式
Simply solve the problem that the node in the redis cluster cannot read data (error) moved
[mainstream nivida graphics card deep learning / reinforcement learning /ai computing power summary]
[upsampling method opencv interpolation]
Intern position selection and simplified career development planning in Internet companies
Flutter2 heavy release supports web and desktop applications
Sentinel sentinel mechanism of master automatic election in redis master-slave
Pytorch linear regression