当前位置:网站首页>MySQL (17 trigger)
MySQL (17 trigger)
2022-06-10 19:10:00 【Fate friend I】
Make progress every day , Make progress !! Thank myself 
List of articles
One Trigger Overview
- MySQL from 5.0.2 Trigger is supported in version .MySQL The trigger is the same as the stored procedure , Are embedded in MySQL A program of the server .
- The trigger is made by Events to trigger Some operation , These events include INSERT 、 UPDATE 、 DELETE event . The so-called event refers to the user's action or triggering a certain behavior . If the trigger program is defined , When the database executes these statements , It's equivalent to an event , will Automatically The trigger performs the corresponding action .
- When inserting data in the data table 、 Update and delete operations , When some database logic needs to be executed automatically , Triggers can be used to implement .
Two Trigger creation
2.1 grammar -‘ Only child ’
- Grammatical structure
CREATE TRIGGER Trigger Name
{BEFORE|AFTER} {
INSERT|UPDATE|DELETE} ON Table name
FOR EACH ROW
The block of statements executed by the trigger ;
- explain
- Table name : Represents the object monitored by the trigger
- BEFORE|AFTER: Indicates the trigger time ( Relative to event ).
- INSERT|UPDATE|DELETE: Indicates the departure event
- INSERT —— Triggered when a record is inserted ;
- UPDATE —— Triggered when the record is updated .
- DELETE —— Triggered when a record is deleted .
- The block of statements executed by the trigger : It can be a single sql sentence , Or by BEGIN……END Structure .
2.2 Case understanding -‘ triplets ’
🪬🪬2.2.1 Case a
- Create trigger : Create name as before_insert The trigger of , towards test_trigger Data table before inserting data , towards
test_trigger_log Insert... Into the data table before_insert Log information .
DELIMITER //
CREATE TRIGGER before_insert
BEFORE INSERT ON test_trigger
FOR EACH ROW
BEGIN
INSERT INTO test_trigger_log (t_log)
VALUES('before_insert');
END //
DELIMITER ;
2.2.2 Case 2
- Create name as after_insert The trigger of , towards test_trigger After inserting data into the data table , towards test_trigger_log Insert in data table
Enter into after_insert Log information
DELIMITER //
CREATE TRIGGER after_insert
AFTER INSERT ON test_trigger
FOR EACH ROW
BEGIN
INSERT INTO test_trigger_log (t_log)
VALUES('after_insert');
END //
DELIMITER ;
2.2.3 Case three
DELIMITER //
CREATE TRIGGER salary_check_trigger
BEFORE INSERT ON employees FOR EACH ROW
BEGIN
DECLARE mgrsalary DOUBLE;
SELECT salary INTO mgrsalary FROM employees WHERE employee_id = NEW.manager_id;
IF NEW.salary > mgrsalary THEN #NEW Keyword represents INSERT Add a new record of the statement
SIGNAL SQLSTATE 'HY000' SET MESSAGE_TEXT = ' The salary is higher than the leader's salary ';
END IF;
END //
DELIMITER ;
3、 ... and see 、 Delete trigger
3.1 View by -‘ Three brothers ’
- The eldest brother – View the definitions of all triggers in the current database
SHOW TRIGGERS\G
- The second – View the definition of a trigger in the current database
SHOE CREATE TRIGGER Trigger Name
- Youngest – From the system library information_schema Of TRIGGERS Query in table “salary_check_trigger” Trigger information .
SELECT * FROM information_schema.TRIGGERS;
3.2 Delete trigger
- Triggers are also database objects , Delete triggers also use DROP sentence , The syntax is as follows :
DROP TRIGGER IF EXISTS Trigger Name ;
Four Success and failure of triggers
4.1 become
- Triggers ensure data integrity
- Triggers can help us log operations .
- Triggers can also be used before manipulating data , Check the validity of the data
🧩🧩4.2 Defeat
- One of the biggest problems with triggers is poor readability .
- Change of relevant data , May cause trigger errors
边栏推荐
- HelloWorld example of TestNG and how to run it from the command line
- 如何设置 SaleSmartly 以进行 Google Analytics(分析)跟踪
- 北京地铁票务系统
- Opencv does not rely on any third-party database for face detection
- 锐捷x32pro刷openwrt开启无线160MHz
- Adobe Premiere基础(动画制作-弹性动画)(八)
- [kuangbin] topic 12 basic DP1
- 第四章 数据类型(三)
- Common methods of stream flow lambder
- Adobe Premiere foundation - tool use (selection tool, razor tool, and other common tools) (III)
猜你喜欢

Adobe Premiere foundation - Import and export, merge materials, source file compilation, offline (II)

WordPress 6.0 “Arturo阿图罗” 发布

腾讯云数据库TDSQL-大咖论道 | 基础软件的过去、现在、未来

Request header field XXXX is not allowed by access control allow headers in preflight response

基于JSP的医院预约挂号平台设计与开发.zip(论文+项目源码)

Design and implementation of online ordering system based on SSM Rar (project source code)

Request header field xxxx is not allowed by Access-Control-Allow-Headers in preflight response问题

How to realize the management "desire" of Business Intelligence BI service objects and enterprise managers?

Stream生成的3张方式-Lambda

锐捷x32pro刷openwrt开启无线160MHz
随机推荐
《Single Image Haze Removal Using Dark Channel Prior》去雾代码实现分析
[kuangbin]专题二十二 区间DP
Seata安装Window环境
Jsp基于ssm项目实验室管理系统设计与现实.doc
c指针(面试经典题目练习)
Salesmartly | add a new channel slack to help you close the customer relationship
Nodejs basic architecture analysis parsing engine directory plug-in installation core module
阵列信号处理仿真之四——Z变换分析阵列多项式
Low carbon data center construction ideas and future trends
Adobe Premiere基础(视频的最后一步字幕添加)(六)
[vulnhub range] janchow: 1.0.1
Dynamic SQL of DB2 SQL pl
Ruixin micro rk1126 platform platform porting libevent cross compiling libevent
数据库防火墙的性能和高可用性分析
Introduction to ad18 device library import
Some summary about YUV format
Opencv does not rely on any third-party database for face detection
Upgrade the playing method of snatching singing, integrate the climax clips of genuine music and real-time scoring ability~
直播预告 | 解构OLAP!新型多维分析架构范式全公开!Apache Doris 将带来五个重磅议题!
mysql8.0(新特性小结)
