MySQL/MariaDB数据库的触发器
MySQL/MariaDB数据库的触发器
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.触发器概述
1>.什么是触发器
触发器的执行不是由程序调用,也不是由手工启动,而是由事件来触发、激活从而实现执行。=
2>.创建触发器帮助信息
MariaDB [yinzhengjie]> HELP CREATE TRIGGER
Name: 'CREATE TRIGGER'
Description:
Syntax:
CREATE
[DEFINER = { user | CURRENT_USER }]
TRIGGER trigger_name trigger_time trigger_event
ON tbl_name FOR EACH ROW trigger_body This statement creates a new trigger. A trigger is a named database
object that is associated with a table, and that activates when a
particular event occurs for the table. The trigger becomes associated
with the table named tbl_name, which must refer to a permanent table.
You cannot associate a trigger with a TEMPORARY table or a view. CREATE TRIGGER requires the TRIGGER privilege for the table associated
with the trigger. The statement might also require the SUPER privilege,
depending on the DEFINER value, as described later in this section. If
binary logging is enabled, CREATE TRIGGER might require the SUPER
privilege, as described in
https://mariadb.com/kb/en/binary-logging-of-stored-routines/. The DEFINER clause determines the security context to be used when
checking access privileges at trigger activation time. See later in
this section for more information. trigger_time is the trigger action time. It can be BEFORE or AFTER to
indicate that the trigger activates before or after each row to be
modified. trigger_event indicates the kind of statement that activates the
trigger. The trigger_event can be one of the following: o INSERT: The trigger is activated whenever a new row is inserted into
the table; for example, through INSERT, LOAD DATA, and REPLACE
statements. o UPDATE: The trigger is activated whenever a row is modified; for
example, through UPDATE statements. o DELETE: The trigger is activated whenever a row is deleted from the
table; for example, through DELETE and REPLACE statements. However,
DROP TABLE and TRUNCATE TABLE statements on the table do not activate
this trigger, because they do not use DELETE. Dropping a partition
does not activate DELETE triggers, either. See [HELP TRUNCATE TABLE]. URL: https://mariadb.com/kb/en/create-trigger/ MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> HELP CREATE TRIGGER
Syntax:
CREATE
[DEFINER = { user | CURRENT_USER }]
TRIGGER trigger_name trigger_time trigger_event
ON tbl_name FOR EACH ROW trigger_body 说明:
trigger_name:
触发器的名称
trigger_time:
{ BEFORE | AFTER },表示在事件之前或之后触发
trigger_event:
{ INSERT |UPDATE | DELETE },触发的具体事件
tbl_name:
该触发器作用在表名
二.触发器案例展示
1>.创建测试表
MariaDB [yinzhengjie]> CREATE TABLE student_info (
-> stu_id INT(11) NOT NULL AUTO_INCREMENT,
-> stu_name VARCHAR(255) DEFAULT NULL,
-> PRIMARY KEY (stu_id)
-> );
Query OK, 0 rows affected (0.01 sec) MariaDB [yinzhengjie]> CREATE TABLE student_count (
-> student_count INT(11) DEFAULT 0
-> );
Query OK, 0 rows affected (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> INSERT INTO student_count VALUES(0);
Query OK, 1 row affected (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT * FROM student_info;
Empty set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT * FROM student_count;
+---------------+
| student_count |
+---------------+
| 0 |
+---------------+
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
2>.创建触发器,在向学生表INSERT数据时,学生数增加,DELETE学生时,学生数减少
MariaDB [yinzhengjie]> CREATE TRIGGER trigger_student_count_insert
-> AFTER INSERT
-> ON student_info FOR EACH ROW
-> UPDATE student_count SET student_count=student_count+1;
Query OK, 0 rows affected (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> CREATE TRIGGER trigger_student_count_delete
-> AFTER DELETE
-> ON student_info FOR EACH ROW
-> UPDATE student_count SET student_count=student_count-1;
Query OK, 0 rows affected (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]>
3>.查看触发器
MariaDB [yinzhengjie]> SHOW TRIGGERS\G
*************************** 1. row ***************************
Trigger: trigger_student_count_insert
Event: INSERT
Table: student_info
Statement: UPDATE student_count SET student_count=student_count+1
Timing: AFTER
Created: 2019-10-28 22:20:07.74
sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Definer: root@localhost
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
Database Collation: utf8_general_ci
*************************** 2. row ***************************
Trigger: trigger_student_count_delete
Event: DELETE
Table: student_info
Statement: UPDATE student_count SET student_count=student_count-1
Timing: AFTER
Created: 2019-10-28 22:20:12.02
sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Definer: root@localhost
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
Database Collation: utf8_general_ci
2 rows in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SHOW TRIGGERS\G
MariaDB [yinzhengjie]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| yinzhengjie |
+--------------------+
4 rows in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> USE information_schema;
Database changed
MariaDB [information_schema]>
MariaDB [information_schema]> SELECT * FROM triggers WHERE trigger_name='trigger_student_count_insert'\G
*************************** 1. row ***************************
TRIGGER_CATALOG: def
TRIGGER_SCHEMA: yinzhengjie
TRIGGER_NAME: trigger_student_count_insert
EVENT_MANIPULATION: INSERT
EVENT_OBJECT_CATALOG: def
EVENT_OBJECT_SCHEMA: yinzhengjie
EVENT_OBJECT_TABLE: student_info
ACTION_ORDER: 1
ACTION_CONDITION: NULL
ACTION_STATEMENT: UPDATE student_count SET student_count=student_count+1
ACTION_ORIENTATION: ROW
ACTION_TIMING: AFTER
ACTION_REFERENCE_OLD_TABLE: NULL
ACTION_REFERENCE_NEW_TABLE: NULL
ACTION_REFERENCE_OLD_ROW: OLD
ACTION_REFERENCE_NEW_ROW: NEW
CREATED: 2019-10-28 22:20:07.74
SQL_MODE: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
DEFINER: root@localhost
CHARACTER_SET_CLIENT: utf8mb4
COLLATION_CONNECTION: utf8mb4_general_ci
DATABASE_COLLATION: utf8_general_ci
1 row in set (0.01 sec) MariaDB [information_schema]>
MariaDB [information_schema]>
查询系统表information_schema.triggers的方式指定查询条件,查看指定的触发器信息。
4>.测试触发器是否执行
MariaDB [(none)]> use yinzhengjie
Database changed
MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT * FROM student_count;
+---------------+
| student_count |
+---------------+
| 0 |
+---------------+
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT * FROM student_info;
Empty set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> INSERT INTO student_info VALUES (1,'Jason Yin'),(2,'YinZhengJie'),(3,'Shell'),(4,'Python'),(5,'Java'),(6,'Golang');
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0 MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT * FROM student_info;
+--------+-------------+
| stu_id | stu_name |
+--------+-------------+
| 1 | Jason Yin |
| 2 | YinZhengJie |
| 3 | Shell |
| 4 | Python |
| 5 | Java |
| 6 | Golang |
+--------+-------------+
6 rows in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT * FROM student_count;
+---------------+
| student_count |
+---------------+
| 6 |
+---------------+
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> DELETE FROM student_info WHERE stu_id > 3;
Query OK, 3 rows affected (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT * FROM student_count;
+---------------+
| student_count |
+---------------+
| 3 |
+---------------+
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT * FROM student_count;
+---------------+
| student_count |
+---------------+
| 3 |
+---------------+
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT * FROM student_info;
+--------+-------------+
| stu_id | stu_name |
+--------+-------------+
| 1 | Jason Yin |
| 2 | YinZhengJie |
| 3 | Shell |
+--------+-------------+
3 rows in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> TRUNCATE TABLE student_info; #使用TRUNCATE清空表中的数据库不会触发触发器哟,因为触发器定义时只针对了DELETE命令进行触发操作
Query OK, 0 rows affected (0.01 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT * FROM student_info;
Empty set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SELECT * FROM student_count;
+---------------+
| student_count |
+---------------+
| 3 |
+---------------+
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> TRUNCATE TABLE student_info; #使用TRUNCATE清空表中的数据库不会触发触发器哟,因为触发器定义时只针对了DELETE命令进行触发操作
5>.删除触发器
MariaDB [yinzhengjie]> SHOW TRIGGERS\G
*************************** 1. row ***************************
Trigger: trigger_student_count_insert
Event: INSERT
Table: student_info
Statement: UPDATE student_count SET student_count=student_count+1
Timing: AFTER
Created: 2019-10-28 22:20:07.74
sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Definer: root@localhost
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
Database Collation: utf8_general_ci
*************************** 2. row ***************************
Trigger: trigger_student_count_delete
Event: DELETE
Table: student_info
Statement: UPDATE student_count SET student_count=student_count-1
Timing: AFTER
Created: 2019-10-28 22:20:12.02
sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Definer: root@localhost
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
Database Collation: utf8_general_ci
2 rows in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> DROP TRIGGER trigger_student_count_insert;
Query OK, 0 rows affected (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SHOW TRIGGERS\G
*************************** 1. row ***************************
Trigger: trigger_student_count_delete
Event: DELETE
Table: student_info
Statement: UPDATE student_count SET student_count=student_count-1
Timing: AFTER
Created: 2019-10-28 22:20:12.02
sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Definer: root@localhost
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
Database Collation: utf8_general_ci
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> DROP TRIGGER trigger_student_count_insert;
MariaDB [yinzhengjie]> SHOW TRIGGERS\G
*************************** 1. row ***************************
Trigger: trigger_student_count_delete
Event: DELETE
Table: student_info
Statement: UPDATE student_count SET student_count=student_count-1
Timing: AFTER
Created: 2019-10-28 22:20:12.02
sql_mode: STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Definer: root@localhost
character_set_client: utf8mb4
collation_connection: utf8mb4_general_ci
Database Collation: utf8_general_ci
1 row in set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> DROP TABLE student_count;
Query OK, 0 rows affected (0.01 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> DROP TABLE student_info; #当删除表时触发器也会随着自动删除,因为触发器是基于表的,当表都不存在了触发器也没有意义了。
Query OK, 0 rows affected (0.01 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> SHOW TRIGGERS\G
Empty set (0.00 sec) MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]>
MariaDB [yinzhengjie]> DROP TABLE student_info; #当删除表时触发器也会随着自动删除,因为触发器是基于表的,当表都不存在了触发器也没有意义了。
MySQL/MariaDB数据库的触发器的更多相关文章
- MySQL/MariaDB数据库的mysqldump工具备份还原实战
MySQL/MariaDB数据库的mysqldump工具备份还原实战 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.mysqldump概述 1>.逻辑备份工具 mysq ...
- MySQL/MariaDB数据库的冷备份和还原
MySQL/MariaDB数据库的冷备份和还原 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL/MariaDB数据库的备份和还原概述 1>.为什么要备份 为了 ...
- MySQL/MariaDB数据库的用户和权限管理
MySQL/MariaDB数据库的用户和权限管理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.元数据数据库(mysql) 系统授权表(均在mysql数据库中): db hos ...
- MySQL/MariaDB数据库的性能测试
MySQL/MariaDB数据库的性能测试 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数据库服务衡量指标 qps: query per second(每秒支持多少查询 ...
- MySQL/MariaDB数据库的Galera高可用性集群实战
MySQL/MariaDB数据库的Galera高可用性集群实战 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Galera Cluster概述 1>.什么是Gale ...
- MySQL/MariaDB数据库的MHA实现高可用实战
MySQL/MariaDB数据库的MHA实现高可用实战 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL高可用常见的解决方案 1>.Multi-Master ...
- MySQL/MariaDB数据库的PROXY实现读写分离
MySQL/MariaDB数据库的PROXY实现读写分离 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.ProxySQL概述 1>.各家互联网公司读写分离的解决方案 m ...
- MySQL/MariaDB数据库的复制监控和维护
MySQL/MariaDB数据库的复制监控和维护 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.清理日志 1>.删除指定日志文件名称之前的日志(也可用基于时间) M ...
- MySQL/MariaDB数据库的复制加密
MySQL/MariaDB数据库的复制加密 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL的安全问题 1>.基于SSL复制 在默认的主从复制过程或远程连接 ...
随机推荐
- ["Visual Studio快捷键" ,"Vs","IDEA快捷键"]
描述说明 描述 说明 ↑ 方向键.上 ↓ 方向键.下 ← 方向键.左 → 方向键.右 快捷键大比拼 描述 Visual Studio 快捷键 IDEA快捷键 VisualStudio学名 IDEA学名 ...
- php-resque 队列简单使用
一.安装 php-resque 进入项目根目录,composer 安装 php-resque composer require chrisboulton/php-resque 二.常用方法 1.连接 ...
- 【tensorflow-v2.0】如何查看模型的输入输出流的属性
操作过程: 1. 查看mobilenet的variables loaded = tf.saved_model.load('mobilenet') print('MobileNet has {} tra ...
- 使用pkg打包node.js项目(egg框架)为可执行包
问题: 公司有个工具型项目使用node.js 开发,需要部署到客户的服务器中,遇到的问题: 1.客户的服务器没有外网.环境配置,依赖安装等都比较麻烦,只能手工上传,最好能一个文件直接搞定: 2.直接包 ...
- ORA-01126: 数据库必须已装载到此实例并且不在任何实例中打开
原因:修改归档模式的操作只能在 mount 状态下进行,不能处于 open 状态. SQL> alter database archivelog;alter database archivelo ...
- WEBSHELL-恶意代码检测
静态查杀 提取特征写成规则库,调用规则库查杀.基于规则,会比较快,但漏报.误报会比较明显,一般的Webshell一句话木马变形混淆会比较多. yara规则 $eval = /(<\?php|[; ...
- vs解决方案中添加文件夹
一般我们在github上面看到的项目结构基本都是把项目放到src文件夹中,test放测试 查了半天也没查到这个是怎么产生的...这边只能用比较笨的方法来完成. 解决方法中是允许我们添加解决方案文件夹, ...
- csu 1984: LXX的能力值
1984: LXX的能力值 Submit Page Summary Time Limit: 3 Sec Memory Limit: 128 Mb Submitted: 17 ...
- Slenium入门
selenium 为浏览器测试框架,可以调用浏览器webdriver模拟浏览器操作360打开Chrome: from selenium import webdriver from selenium.w ...
- 简单的爬虫程序以及使用PYQT进行界面设计(包含源码解析)
由于这个是毕业设计的内容,而且还是跨专业的.爬虫程序肯定是很简单的,就是调用Yahoo的API进行爬取图片.这篇博客主要讲的是基础的界面设计. 放上源码,然后分部解析一下重要的地方.注:flickra ...