当前位置:网站首页> Mysql中的触发器定义及语法介绍
Mysql中的触发器定义及语法介绍
2022-06-23 21:49:00 【1024问】
1.定义:
2.语法:
3.删除触发器
4.查询触发器
5.触发器类型OLD和NEW的使用
1.定义:触发器和存储过程相似,都是嵌入到 MySQL 中的一段程序。触发器是由事件来触发某个操作。当数据库执行这些事件时,就会激活触发器来执行相应的操作。这些事件称为触发条件,在MySQL中,有UPDATE,INSERT,和DELETE。
在MySQL中,目前只有UPDATE,INSERT,和DELETE这三种操作,才会触发触发器,其他操作都不支持触发器。这和Oracle中的触发器有很大的区别,要注意区分。
触发器的每个表每次事件,都只允许一个触发器。因此,每个表最多值允许有6个触发器。如果一个表有两个事件,INSERT和DELETE,那么就要分别创建一个触发器。
只有表才支持触发器,视图和临时表不支持。
2.语法:CREATE TRIGGER TRIGGER_NAMEBEFORE/AFTER UPDATE/INSERT/DELETE ON TABLE_NAME FOR EACH ROW#触发器主体[BEGIN]SQL操作[END];创建触发器:和创建存储过程是一样,用的都是CREATE 语句,但要注意,这里没有 OR REPLACE。
TRIGGER_NAME:触发器名称。在MySQL中,要保证每个表中的触发器是唯一的,每一个数据库内的触发器可以不唯一,这就说明了一个数据库内的两个表可以有相同的触发器。这种行为在其他的数据库内是不被允许的。但是为了格式上的规范,还是要尽可能的保证TRIGGER_NAME的唯一性。
BEFORE/AFTER:触发器被触发的时刻。你如果希望触发器在触发事件之前发生,就使用BEFORE,如果希望在触发事件之后发生,就使用AFTER。
UPDATE/INSERT/DELETE:触发事件,触发条件。在MySQL中,只有这三种DDL操作支持触发器,其他操作都不被允许。
FOR EACH ROW:受触发事件影响的每一行都要激活触发器的动作。这里指的是行级触发,在MySQL里,FOE EACH ROW不可省略,不支持语句级触发。
触发器主体:既可以是单独的一条SQL语句,也可以是由BEGIN...END组成的复杂结构块。
# 创建一个触发器T1,在对表customer做插入操作时,就会触发T1,之后会在日志表note中插入一条数据CREATE TRIGGER T1AFTER INSERT ON customerFOR EACH ROW # 触发器主体INSERT INTO note(日期,目标,操作) VALUES(NOW(),'customer','insert');3.删除触发器和删除表和存储过程类似,都是使用DROP语句来删除触发器。
#删除触发器T1DROP TRIGGER IF EXISTS T1;触发器不能够更新和修改,想要修改一个触发器的内容,只能先将触发器删除,在创建一个新的触发器。
4.查询触发器#在已知数据库内,查询触发器SHOW TRIGGERS;SELECT * FROM information_schema.TRIGGERS WHERE TRIGGER_NAME = '触发器名'#查询所有的触发器SELECT * FROM information_schema.TRIGGERS(WHERE TRIGGER_SCHEMA = '数据库名')5.触发器类型OLD和NEW的使用触发器在DDL操作之前或者之后触发,总会对目标表做出一定的改动,有时我们需要知道目标表改变前后的值,这就用到了OLD和NEW的用法。
顾名思义,OLD就是改变之前的值,是UPDATE之前或者是要被DELETE或者已经被DELETE的值。
而NEW,则是UPDATE之后的新值,即将INSERT或者已经INSERT的值。

具体实例:
#创建customer表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;#向表中插入数据#创建一个update触发器CREATE TRIGGER T2AFTER UPDATE ON customerFOR EACH ROW #old和new的使用方法:old.columnname/new.columnname(列名)#将更新前后的值,赋值给两个变量SELECT OLD.CUST_ID,NEW.CUST_ID INTO @OLD_ID,@NEW_ID;#触发触发器T2UPDATE customer SET CUST_ID = '10000' WHERE CUST_NAME = 'AAA';#查询OLD和NEWSELECT @OLD_ID,@NEW_ID;到此这篇关于Mysql中的触发器的文章就介绍到这了,更多相关mysql触发器内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- Introduction to tcapulusdb lossless relocation
- Opengauss Developer Day 2022 was officially launched to build an open source database root community with developers
- Integrated management and control system of 3D intelligent warehousing and logistics park
- Talk about the problems and solutions of IT enterprise fixed assets management system
- Batch production of plant hangtag
- How many times can the server address and fortress address be entered before the connection is successful? Why did the connection fail?
- C language picture transcoding for performance testing
- 2021-12-10: which can represent a 64 bit floating point number or a 64 bit signed integer
- How to choose the server for website construction, including which servers and how much to rent
- WordPress plugin smart product review 1.0.4 - upload of any file
猜你喜欢

应用实践 | Apache Doris 整合 Iceberg + Flink CDC 构建实时湖仓一体的联邦查询分析架构

Game security - call analysis - write code

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

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

Slsa: accelerator for successful SBOM

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

【技术干货】蚂蚁办公零信任的技术建设路线与特点

SLSA: 成功SBOM的促进剂

在宇宙的眼眸下,如何正确地关心东数西算?

Pourquoi une seule valeur apparaît - elle sur votre carte de données?
随机推荐
Hackinglab penetration test question 8:key can't find it again
How does data Vientiane CI | app quickly integrate HLS encryption to prevent video leakage?
Integrated management and control system of 3D intelligent warehousing and logistics park
How to batch output EAN13 code to PDF
How does the fortress machine view the account assigned by the server? What are the specific steps?
[tcapulusdb knowledge base] [generic table] read data interface description
Command line enumeration, obtaining and modifying time zones
Performance test - LoadRunner obtains the return value and user-defined parameters (parameter operation)
Usage of cobaltstrike: Part 1 (basic usage, listener, redirector)
Impala port
Micro build low code tutorial - variable definition
How to choose the server for website construction, including which servers and how much to rent
How to use phpMyAdmin to restore a backed up MySQL database
What should be done when the encryptor fails to authenticate in the new version of easycvr?
2021-12-10: which can represent a 64 bit floating point number or a 64 bit signed integer
What happened when the fortress remote login server was blocked? What can be done to solve it?
The old CVM of Tencent cloud is migrated to the new CVM, and the IP remains unchanged
Why is the server fortress machine error code 110? How to solve error code 110?
Achieve scoring (Star scoring) effect through native JS
[tcapulusdb knowledge base] insert data example (TDR table)