当前位置:网站首页>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 !
边栏推荐
- Urgent! Tencent cloud container security supports the detection of Apache log4j2 vulnerabilities for the first time. It is in free trial
- How to use xshell to log in to the server through the fortress machine? How does the fortress machine configure the tunnel?
- Game security - call analysis - write code
- Advantages of micro service registry Nacos over Eureka
- SAVE: 软件分析验证和测试平台
- C#/VB.NET Word转Text
- 脚本之美│VBS 入门交互实战
- Production of labels for table products
- Pressure measuring tool platform problem case base
- Intelligent storage | high speed HD media processing capability
猜你喜欢

混沌工程,了解一下

SLSA: 成功SBOM的促进剂

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

Section 29 basic configuration case of Tianrongxin topgate firewall

C#/VB.NET Word转Text

Pourquoi une seule valeur apparaît - elle sur votre carte de données?
![[technical dry goods] the technical construction route and characteristics of zero trust in ant Office](/img/d1/ce999b9f72bbb8f692c4298b4042aa.png)
[technical dry goods] the technical construction route and characteristics of zero trust in ant Office

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

The technical design and practice of decrypting the red envelopes of Tiktok Spring Festival

In the eyes of the universe, how to correctly care about counting East and West?
随机推荐
Batch production of plant hangtag
How to set the website address for website construction can the website be put on record
In the eyes of the universe, how to correctly care about counting East and West?
Tcapulusdb Jun · industry news collection
Detailed explanation of bitmap optimization
Six supervised learning methods: classification of poisonous mushrooms
How to build a website after registering a domain name
FTP server setup setting website information can I set up FTP myself
Log4j has been exposed to a nuclear bomb level vulnerability, and the developer has fried the pot!
What are the processes, levels, stages and key points of requirements analysis in software development
Tcapulusdb Jun · industry news collection
Mysql中的触发器定义及语法介绍
How to judge the video frame type in h265 in golang development
How to access the top-level domain name and automatically jump to the secondary domain name?
Get and post are nothing more than TCP links in nature?
MySQL highly available version 1c1g exclusive cloud database value-added special offers!
Flutter Utils
JMeter pressure measuring tool beginner level chapter
Notes to nodejs (III)
Discussion: will low code integrated oa/erp/mes system be an important part of enterprise application ecology?