当前位置:网站首页>Trigger definition and syntax introduction in MySQL
Trigger definition and syntax introduction in MySQL
2022-06-23 22:54:00 【1024 Q】
1. Definition :
2. grammar :
3. Delete trigger
4. Query triggers
5. Trigger Type OLD and NEW Use
1. Definition :Triggers are similar to stored procedures , Are embedded in MySQL One of the procedures in . A trigger is an action triggered by an event . When the database executes these events , The trigger will be activated to perform the corresponding operation . These events are called trigger conditions , stay MySQL in , Yes UPDATE,INSERT, and DELETE.
stay MySQL in , At present, only UPDATE,INSERT, and DELETE These three operations , Trigger will be triggered , Triggers are not supported for other operations . This sum Oracle The triggers in are very different , Pay attention to the distinction .
Each table of the trigger each event , Only one trigger is allowed . therefore , The maximum value allowed for each table is 6 Trigger . If a table has two events ,INSERT and DELETE, Then create a trigger respectively .
Only tables support triggers , Views and temporary tables do not support .
2. grammar :CREATE TRIGGER TRIGGER_NAMEBEFORE/AFTER UPDATE/INSERT/DELETE ON TABLE_NAME FOR EACH ROW# Trigger body [BEGIN]SQL operation [END];Create trigger : It's the same as creating a stored procedure , Using all of these CREATE sentence , But should pay attention to , Nothing here OR REPLACE.
TRIGGER_NAME: Trigger Name . stay MySQL in , Ensure that the triggers in each table are unique , Triggers in each database may not be unique , This shows that two tables in a database can have the same trigger . This behavior is not allowed in other databases . But for the sake of standard format , Still try to guarantee TRIGGER_NAME Uniqueness .
BEFORE/AFTER: When the trigger is triggered . If you want the trigger to happen before the event is triggered , Just use BEFORE, If you want to happen after the trigger event , Just use AFTER.
UPDATE/INSERT/DELETE: Triggering event , The trigger condition . stay MySQL in , There are only three DDL Actions support triggers , Other operations are not allowed .
FOR EACH ROW: Each row affected by the trigger event must activate the trigger action . This refers to row level triggering , stay MySQL in ,FOE EACH ROW Do not omit , Statement level triggering is not supported .
Trigger body : It can be a single one SQL sentence , It can also be done by BEGIN...END A complex structural block composed of .
# Create a trigger T1, On the table customer When inserting , It will trigger T1, Then it will be in the log table note Insert a piece of data in CREATE TRIGGER T1AFTER INSERT ON customerFOR EACH ROW # Trigger body INSERT INTO note( date , The goal is , operation ) VALUES(NOW(),'customer','insert');3. Delete trigger Similar to deleting tables and stored procedures , Is to use DROP Statement to delete the trigger .
# Delete trigger T1DROP TRIGGER IF EXISTS T1;Triggers cannot be updated or modified , Want to modify the contents of a trigger , The trigger can only be deleted first , Creating a new trigger .
4. Query triggers# In a known database , Query triggers SHOW TRIGGERS;SELECT * FROM information_schema.TRIGGERS WHERE TRIGGER_NAME = ' Trigger Name '# Query all triggers SELECT * FROM information_schema.TRIGGERS(WHERE TRIGGER_SCHEMA = ' Database name ')5. Trigger Type OLD and NEW Use Trigger in DDL Triggered before or after the operation , Always make some changes to the target table , Sometimes we need to know the values before and after the target table changes , And that's where it comes in OLD and NEW Usage of .
seeing the name of a thing one thinks of its function ,OLD Is to change the previous value , yes UPDATE Before or to be DELETE Or has been DELETE Value .
and NEW, It is UPDATE The new value after , the INSERT Or already INSERT Value .
Specific examples :
# establish customer surface CREATE TABLE `customer` (`CUST_ID` int(11) NOT NULL AUTO_INCREMENT,`CUST_NAME` varchar(10) NOT NULL,`CUST_TEL` varchar(10) DEFAULT NULL,PRIMARY KEY (`CUST_ID`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;# Insert data into table # Create a update trigger CREATE TRIGGER T2AFTER UPDATE ON customerFOR EACH ROW #old and new How to use :old.columnname/new.columnname( Name )# The values before and after will be updated , Assign to two variables SELECT OLD.CUST_ID,NEW.CUST_ID INTO @OLD_ID,@NEW_ID;# Trigger trigger T2UPDATE customer SET CUST_ID = '10000' WHERE CUST_NAME = 'AAA';# Inquire about OLD and NEWSELECT @OLD_ID,@NEW_ID;This is about Mysql This is the end of the trigger article in , More about mysql For trigger content, please search the previous articles of software development network or continue to browse the relevant articles below. I hope you can support software development network more in the future !
边栏推荐
- Consequences of website construction without SSL authentication are websites without SSL authentication reliable
- How to set up external links in website construction
- Opengauss Developer Day 2022 was officially launched to build an open source database root community with developers
- Apache log4j 2 reported high-risk vulnerability, coding teamed up with Tencent to protect software security
- How to set the search bar of website construction and what should be paid attention to when designing the search box
- Section 30 high availability (HA) configuration case of Tianrongxin topgate firewall
- How to batch generate flattermark barcode
- Is the website under construction because there is no FTP upload? Can I upload without FTP
- Micro build low code tutorial -hello, world
- What are the application flow restrictions of API gateway framework?
猜你喜欢

蚂蚁集团自研TEE技术通过国家级金融科技产品认证

Ant group's self-developed tee technology has passed the national financial technology product certification

In the eyes of the universe, how to correctly care about counting East and West?

脚本之美│VBS 入门交互实战

Game security - call analysis - write code

SLSA: 成功SBOM的促进剂
Docker中部署Redis集群与部署微服务项目的详细过程

解密抖音春节红包背后的技术设计与实践
SQL语句中EXISTS的详细用法大全

Why is only one value displayed on your data graph?
随机推荐
Production of labels for table products
Tcapulusdb Jun · industry news collection
C#/VB.NET Word转Text
PHPMailer 发送邮件 PHP
Get and post are nothing more than TCP links in nature?
Notes to nodejs (III)
How to deploy the deep learning model to the actual project? (classification + detection + segmentation)
This article takes you to understand the JVM class loading mechanism
The article "essence" introduces you to VMware vSphere network, vswitch and port group!
在宇宙的眼眸下,如何正确地关心东数西算?
MySQL数据库配置信息查看与修改方法详解
Analysis and application of ThreadLocal source code
Batch production of plant hangtag
How to set the website construction title bar drop-down
Virtual machine performance monitoring and fault handling commands on the console
Targeted, real-time audio and video optimization in remote control
Grpc: quickly configure the general API to obtain process meta information
Change sql- Tencent cloud database tdsql elite challenge - essence Q & A
How to batch generate flattermark barcode
How to set the website address for website construction can the website be put on record