当前位置:网站首页>How to create and delete MySQL triggers
How to create and delete MySQL triggers
2022-06-29 17:23:00 【Yisu cloud】
MySQL How to create and delete triggers
This article mainly introduces “MySQL How to create and delete triggers ” Knowledge about , Xiaobian shows you the operation process through practical cases , The operation method is simple and fast , Practical , Hope this article “MySQL How to create and delete triggers ” The article can help you solve problems .

1. Why triggers are needed
There are some tables that are related to each other , For example, commodity table and inventory table , We operate on the data of the commodity table , Then the corresponding inventory table has to change , Only in this way can the integrity of the data be guaranteed . If we maintain it manually , More trouble .
At this point we can use triggers , Create a trigger , Let the insertion of commodity information data automatically trigger the insertion of inventory data , In this way, we don't need to worry about data loss caused by forgetting to add inventory data .
2. Trigger Overview
MySQL It's from 5.0.2 Version starts to support trigger
MySQL Like stored procedures, triggers are embedded in MySQL The server It's a piece of procedure
A trigger is an event that triggers an action , These events include insert,delete,update event
If trigger procedure is defined , When the database executes these statements , It's equivalent to an event , The trigger will be automatically fired to perform the corresponding operation
If you are inserting data from tables in the database , When some database logic needs to be executed automatically , We can use triggers to implement .
3. Trigger creation
3.1 grammar
Triggers work on tables , For example, we want to watch A Triggered when a new record is added Trigger execution , And also choose whether the trigger is in insert Whether the trigger is executed before or after the statement is executed .
for each row indicate , Every time an event is executed (insert,update or delete) Will trigger a trigger
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 .BEFORE Indicates that... Is triggered before the event ;AFTER Indicates that... Is triggered after the event .INSERT|UPDATE|DELETE: Indicates the event triggered .INSERT Indicates that... Is triggered when a record is inserted ;
UPDATE Indicates that... Is triggered when the record is updated ;
DELETE Indicates that... Is triggered when a record is deleted .
The block of statements executed by the trigger: It can be a single SQL sentence , It can also be done by BEGIN…END Structure .
3.2 Case presentation
Prepare the table first
CREATE TABLE test_trigger (id INT PRIMARY KEY AUTO_INCREMENT,t_note VARCHAR(30));CREATE TABLE test_trigger_log (id INT PRIMARY KEY AUTO_INCREMENT,t_log VARCHAR(30));
requirement : 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 .

4. Check triggers
View trigger is to view the definition of triggers that already exist in the database , Status and syntax information
Mode one : View the definitions of all triggers in the current database
show triggers\G Be careful , stay SQLyog in , It can't be added \G

Mode two : View the definition of a trigger in the current database
show create trigger Trigger Name

Mode three : From the system library information_schema Of TRIGGERS Query in table “salary_check_trigger” Trigger information
SELECT * FROM information_schema.TRIGGERS;
5. Delete trigger
Triggers are also database objects , Triggers are also used drop Statement to delete
drop trigger if exists Trigger Name ;
About “MySQL How to create and delete triggers ” That's all for , Thanks for reading . If you want to know more about the industry , You can pay attention to the Yisu cloud industry information channel , Xiaobian will update different knowledge points for you every day .
边栏推荐
- 从Mpx资源构建优化看splitChunks代码分割
- 535. TinyURL 的加密与解密 / 剑指 Offer II 103. 最少的硬币数目
- mysql.sock的概念是什么
- 0 basic self-study STM32 (wildfire) -- use register to light LED -- Explanation of GPIO function block diagram
- Viewing splitchunks code segmentation from MPX resource construction optimization
- C comparison of the performance of dapper efcore sqlsugar FreeSQL hisql sqlserver, an ORM framework at home and abroad
- Kubernetes deployment dashboard (Web UI management interface)
- SpingMVC请求和响应
- C语言练习----指针字符串、链表
- NVIDIA安装最新显卡驱动
猜你喜欢

【R语言数据科学】:文本挖掘(以特朗普推文数据为例)

Online text digit recognition list summation tool
Master slave replication of MySQL

从Mpx资源构建优化看splitChunks代码分割

Redis 原理 - Sorted Set (ZSet)

“授权同意”落地压力大?隐私计算提供一种可能的合规“技术解”

为什么信息化 ≠ 数字化?终于有人讲明白了

序列检测器

In depth analysis of Monai (I) data and transforms

Function calculation asynchronous task capability introduction - task trigger de duplication
随机推荐
Mysql中锁的使用场景是什么
线段树、树状数组模板(复制粘贴确实好用)
What are the advantages of SaaS services
力扣今日题-535. TinyURL 的加密与解密
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
R语言使用epiDisplay包的kap函数(kap.2.raters函数)计算Kappa统计量的值(总一致性、期望一致性)、对两个评分对象的结果进行一致性分析、评分的类别为多个类别
Redis bloom filter and cuckoo filter
C语言练习----指针字符串、链表
Browser large screen capture
如何创建虚拟形象
Go语言多方式并发实现龟兔赛跑
外部自动(PLC启动机器人)
我的远程办公初体验 | 社区征文
OpenFeign使用步骤 轮询策略与权重 log4j使用 openFeign拦截器的配置
函数计算异步任务能力介绍 - 任务触发去重
MySQL触发器如何创建与删除
Kotlin basic grammar
2022 software evaluator examination outline
在供应链场景应用中,供应链管理系统扮演什么角色?
自动收售报机