当前位置:网站首页>MySQL trigger
MySQL trigger
2022-06-13 05:56:00 【A programmer in a wig】
Trigger Overview
Basic concepts
MySQL As if from 5.0.2 The version has been supporting the function of triggers .
Triggers are database objects related to tables , Triggered when the defined conditions are met , And execute the set of statements defined in the trigger . This feature of trigger can help to ensure the integrity of data in database .
The characteristics of triggers :
1、 Yes begin end body ,begin end; The statements between can be simple or complex
2、 What conditions trigger :insert,update,delete
3、 When to trigger : Before or after the addition, deletion or modification
4、 Trigger frequency : Execute... For each line
5、 Triggers are defined on the table , Attach to table .
Trigger execution takes time , Therefore, it is not recommended to use triggers for data tables with frequent additions, deletions, and modifications , Because it reduces efficiency
The type of trigger
According to the trigger time of the trigger and the triggered operation, the trigger is divided into 6 Type in the
- 1 BEFORE INSERT
- 2 AFTER INSERT
- 3 BEFORE UPDATE
- 4 AFTER UPDATE
- 5 BEFORE DELETE
- 6 AFTER DELETE
Create trigger Syntax
CREATE
[DEFINER = { user | CURRENT_USER }]
TRIGGER trigger_name
trigger_time trigger_event
ON tbl_name FOR EACH ROW
[trigger_order]
trigger_body
trigger_time: { BEFORE | AFTER }
trigger_event: { INSERT | UPDATE | DELETE }
trigger_order: { FOLLOWS | PRECEDES } other_trigger_name
BEFORE and AFTER Parameter specifies when execution is triggered , Before or after the event .
FOR EACH ROW Indicates that the trigger event will be triggered if the operation on any record satisfies the trigger event , In other words, the trigger frequency of the trigger is triggered once for each row of data .
tigger_event Detailed explanation :
①INSERT Type trigger : Activate trigger when inserting a row , May be through INSERT、LOAD DATA、REPLACE Statement trigger (LOAD DAT Statement is used to load a file into a data table , It's equivalent to a series of INSERT operation );
②UPDATE Type trigger : Activate trigger when changing a row , May be through UPDATE Statement trigger ;
③DELETE Type trigger : Activate trigger when deleting a row , May be through DELETE、REPLACE Statement trigger .
trigger_order yes MySQL5.7 The next function , Used to define multiple triggers , Use follows( Follow ) or precedes( stay … prior to ) To select the sequence of trigger execution .
Hard vegetable
BEFORE INSERT
stay BEFORE INSERT Trigger NEW Indicates adding a new row of data , have access to NEW.xxx To get a column of data
DELIMITER $$
CREATE TRIGGER triger1 BEFORE INSERT
ON book FOR EACH ROW
BEGIN
DECLARE v_title VARCHAR(255);
if length(NEW.title)>5 THEN
SET NEW.title = ' The title is too long , So just cut it off ';
end if;
END $$
DELIMITER ;
test :
insert into book (isbn,title,price,detail,pubTime,authorId)
values('95275862',' The title is long ',125,' It is used to test triggers ','2021-9-25',1)

AFTER INSERT
there NEW Or the newly added data row
DELIMITER $$
CREATE TRIGGER triger2 AFTER INSERT
ON book FOR EACH ROW
BEGIN
insert into temp values(NEW.bookid);
END $$
DELIMITER ;
BEFORE UPDATE
there NEW Represents a new data row . OLD Represents the data row to be modified .
DELIMITER $$
CREATE TRIGGER triger3 BEFORE UPDATE
ON book FOR EACH ROW
BEGIN
insert into temp values(CONCAT(' Add the title :',OLD.title,' It is amended as follows :',NEW.title));
END $$
DELIMITER ;
test :
update book set title=' short ' where isbn = '95275863';
AFTER UPDATE
It is completely consistent with the description and writing of the previous case
BEFORE DELETE
Nothing here NEW, Only OLD,OLD It means the data row to be deleted
DELIMITER $$
CREATE TRIGGER triger4 BEFORE DELETE
ON book FOR EACH ROW
BEGIN
insert into temp values(CONCAT(' Ready to delete :',OLD.title));
END $$
DELIMITER ;
AFTER DELETE
It is completely consistent with the description and writing of the previous case
边栏推荐
- 2021-9-19
- Why do so many people hate a-spice
- Leetcode- detect uppercase letters - simple
- The reason why the process cannot be shut down after a spark job is executed and the solution
- ArrayList loop removes the pit encountered
- Concurrent programming -- countdownlatch combined with thread pool
- One of PowerShell optimizations: prompt beautification
- Quartz basic use
- Cross compile HelloWorld with cmake
- 20 flowable container (event sub process, things, sub process, pool and pool)
猜你喜欢

Config server configuration center of Nacos series

Quartz database storage

軟件測試——接口常見問題匯總

How MySQL optimizes the use of joint index ABC

Validation set: ‘flowable-executable-process‘ | Problem: ‘flowable-servicetask-missing-implementatio

SPI primary key generation strategy for shardingsphere JDBC

Application virtual directory static resource configuration on tongweb

Concurrent programming -- source code analysis of thread pool

1 Introduction to drools rule engine (usage scenarios and advantages)

A simple recursion problem of linked list
随机推荐
Three paradigms of MySQL
17 servicetask of flowable task
MySQL fuzzy query and sorting by matching degree
若依框架=》如何设置导入导出模板全局为文本格式(解决科学计数问题)
Parallelgateway and exclusivegateway of 14 gateways
[automated test] cypress manual
OpenGL Mosaic (8)
Working principle of sentinel series (source code analysis)
Leetcode- divide candy - simple
Misunderstanding of tongweb due to ease of use
OpenGL马赛克(八)
Basic application of sentinel series
Leetcode- longest palindrome string - simple
Leetcode- third largest number - simple
Conf/tongweb Functions of properties
The 13th week of the second semester of sophomore year
Initial redis experience
Working principle of sentinel series (concept)
Set the correct width and height of the custom dialog
2021.9.29 learning log MIME type