当前位置:网站首页>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 !
边栏推荐
- 5 minutes to explain what is redis?
- [tcapulusdb knowledge base] insert data example (TDR table)
- [tcapulusdb knowledge base] update data example (TDR table)
- Detailed explanation of GC principle
- Advantages of micro service registry Nacos over Eureka
- Problem solving: inittramfs unpacking failed:decoding failed
- Usage of cobaltstrike: Part 1 (basic usage, listener, redirector)
- Pourquoi une seule valeur apparaît - elle sur votre carte de données?
- Detailed explanation of flutter exception capture
- 脚本之美│VBS 入门交互实战
猜你喜欢
Mysql中的触发器定义及语法介绍

Opengauss Developer Day 2022 was officially launched to build an open source database root community with developers

openGauss Developer Day 2022正式开启,与开发者共建开源数据库根社区

为什么你的数据图谱分析图上只显示一个值?
SQL语句中EXISTS的详细用法大全

為什麼你的數據圖譜分析圖上只顯示一個值?

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

Pourquoi une seule valeur apparaît - elle sur votre carte de données?

Section 30 high availability (HA) configuration case of Tianrongxin topgate firewall

脚本之美│VBS 入门交互实战
随机推荐
Why don't people like PHP?
How to set the website address for website construction can the website be put on record
PHPMailer 发送邮件 PHP
Redis6.x.x build rediscluster cluster
How to set up links for website construction how to build a website
Heat transfer oil electric heater
2021-12-10: which can represent a 64 bit floating point number or a 64 bit signed integer
Ranking of high cost performance commercial endowment insurance products in 2022
AAAI 2022 | Tencent Youtu 14 papers were selected, including image coloring, face security, scene text recognition and other frontier fields
Low code technology
PostgreSQL怎么创建分区表详解
First talk about the necessary skills of Architecture
Installation and use of qingscan scanner
How do new investors open accounts by speculation? Is online account opening safe?
Section 30 high availability (HA) configuration case of Tianrongxin topgate firewall
5 minutes to explain what is redis?
Batch production of plant hangtag
What should I do if the RDP fortress server connection times out? Why should enterprises use fortress machines?
FTP server setup setting website information can I set up FTP myself
Get and post are nothing more than TCP links in nature?