MySQL/MariaDB數據庫的觸發器mysql
做者:尹正傑sql
版權聲明:原創做品,謝絕轉載!不然將追究法律責任。數據庫
一.觸發器概述ide
1>.什麼是觸發器測試
觸發器的執行不是由程序調用,也不是由手工啓動,而是由事件來觸發、激活從而實現執行。=
2>.建立觸發器幫助信息ui
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]>
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:
該觸發器做用在表名
二.觸發器案例展現this
1>.建立測試表spa
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學生時,學生數減小3d
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>.查看觸發器code
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 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]>
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]>
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]> 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]>