当前位置:网站首页>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 !
边栏推荐
- 新股民怎样炒股票开户?在线开户安全么?
- Payment industry tuyere project: smart digital operation 3.0
- Detailed explanation of GC principle
- 为什么你的数据图谱分析图上只显示一个值?
- How to shut down the server in the fortress machine? What other operations can the fortress machine perform?
- Troubleshooting of undefined problems in the channel list of easynvr channel management
- Mysql中的触发器定义及语法介绍
- 0day1- (cve-2021-44228) log4j2 rce recurrence
- Problem solving: inittramfs unpacking failed:decoding failed
- Recommended | January activity 2-core 4G lightweight application server, enterprise nationwide purchase 6.7 yuan / month!!!
猜你喜欢

C#/VB.NET Word转Text

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

SAVE: 软件分析验证和测试平台

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

Application practice | Apache Doris integrates iceberg + Flink CDC to build a real-time federated query and analysis architecture integrating lake and warehouse
PostgreSQL怎么创建分区表详解

SLSA: 成功SBOM的促进剂
![[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

解密抖音春节红包背后的技术设计与实践

Chaos engineering, learn about it
随机推荐
5 minutes to explain what is redis?
How to batch generate flattermark barcode
Service API version design and Practice
Apache log4j 2 reported high-risk vulnerability, coding teamed up with Tencent to protect software security
How to set dynamic background for website construction what are the benefits of dynamic background
Consequences of website construction without SSL authentication are websites without SSL authentication reliable
Semaphore semaphore details
Notes to nodejs (III)
The technical design and practice of decrypting the red envelopes of Tiktok Spring Festival
SQL语句中EXISTS的详细用法大全
Six supervised learning methods: classification of poisonous mushrooms
Ranking of high cost performance commercial endowment insurance products in 2022
脚本之美│VBS 入门交互实战
How to choose the server for website construction, including which servers and how much to rent
How to shut down the server in the fortress machine? What other operations can the fortress machine perform?
This article takes you to understand the JVM class loading mechanism
How to build a business analysis system
openGauss Developer Day 2022正式开启,与开发者共建开源数据库根社区
How to set up external links in website construction
Talk about the problems and solutions of IT enterprise fixed assets management system