您好,登錄后才能下訂單哦!
MySQL的觸發器和存儲過程一樣,都是嵌入到MySQL的一段程序。觸發器是由時間來觸發某個操作,這些時間包括INSERT、UODATE和DELETE語句。如果定義了觸發程序,當數據庫執行這些語句的時候就會觸發執行相應的操作,觸發程序是與表有關的命名數據庫對象,當表上出現特定事件時,當激活該對象。
11.1、創建觸發器
觸發器是一個特殊的存儲過程,不同的是,執行存儲過程要使用CALL語句來調用,而觸發器的執行不需要使用CALL語句來調用,也不需要手工啟動,只要當預定義的時間發生的時候,就會被MySQL自動調用。
創建一個觸發器的語法為:
CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_body
trigger_name:觸發器名稱,用戶自行指定 trigger_time:觸發時機,可以指定為before或after trigger_event:標識觸發事件 tbl_name:標識觸發器的表名 trigger_body:觸發器執行語句 |
創建一個單執行語句的觸發器:
mysql> CREATE TABLE account (acc_num INT,amount DECIMAL(10,2)); Query OK, 0 rows affected (0.06 sec) mysql> CREATE TRIGGER ins_sum BEFORE INSERT ON account -> FOR EACH ROW SET @sum = @sum +NEW.amount; Query OK, 0 rows affected (0.02 sec) mysql> SET @sum=0; Query OK, 0 rows affected (0.00 sec) mysql> INSERT INTO account VALUES (1,1.00),(2,2.00); Query OK, 2 rows affected (0.02 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> SELECT @sum; +------+ | @sum | +------+ | 3.00 | +------+ 1 row in set (0.00 sec)
創建多個執行語句的觸發器的語法:
CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW BEGIN trigger_stmt END
11.2、查看觸發器
查看觸發器是指查看數據庫中已存在的觸發器的定義、狀態和觸發信息等可以通過SHOW TRUGGERS和在triggers表中查看觸發器信息。
mysql> CREATE TABLE myevent -> ( -> id int(11) DEFAULT NULL, -> evt_name char(20) DEFAULT NULL -> ); Query OK, 0 rows affected (0.03 sec) mysql> CREATE TRIGGER trig_update AFTER UPDATE ON account -> FOR EACH ROW INSERT INTO myevent VALUES (1,'AFTER UPDATE'); Query OK, 0 rows affected (0.05 sec) mysql> SHOW TRIGGERS \G *************************** 1. row *************************** Trigger: ins_sum Event: INSERT Table: account Statement: SET @sum = @sum +NEW.amount Timing: BEFORE Created: NULL sql_mode: Definer: root@localhost character_set_client: utf8 collation_connection: utf8_general_ci Database Collation: utf8_general_ci *************************** 2. row *************************** Trigger: trig_update Event: UPDATE Table: account Statement: INSERT INTO myevent VALUES (1,'AFTER UPDATE') Timing: AFTER Created: NULL sql_mode: Definer: root@localhost character_set_client: utf8 collation_connection: utf8_general_ci Database Collation: utf8_general_ci 2 rows in set (0.00 sec)
在MySQL中所有觸發器的定義都存在INFORMATION_SCHEMA數據庫的TRIGGERS表中,可以通過SELECT 來查看,其語法格式為:
SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE condition;
mysql> SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE TRIGGER_NAME='trig_update' \G *************************** 1. row *************************** TRIGGER_CATALOG: def TRIGGER_SCHEMA: test TRIGGER_NAME: trig_update EVENT_MANIPULATION: UPDATE EVENT_OBJECT_CATALOG: def EVENT_OBJECT_SCHEMA: test EVENT_OBJECT_TABLE: account ACTION_ORDER: 0 ACTION_CONDITION: NULL ACTION_STATEMENT: INSERT INTO myevent VALUES (1,'AFTER UPDATE') 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: NULL SQL_MODE: DEFINER: root@localhost CHARACTER_SET_CLIENT: utf8 COLLATION_CONNECTION: utf8_general_ci DATABASE_COLLATION: utf8_general_ci 1 row in set (0.00 sec)
11.3、觸發器的使用
觸發程序是與表有關的命名數據庫對象,當表上出現特定時間時,將激活該對象。
創建一個在account表插入數據之后,更新myevent數據表的觸發器。
mysql> CREATE TRIGGER trig_inster AFTER INSERT ON account -> FOR EACH ROW INSERT INTO myevent VALUES (2,'AFTER INSERT'); Query OK, 0 rows affected (0.02 sec) mysql> INSERT INTO account VALUES (1,1.00),(2,2.00); Query OK, 2 rows affected (0.01 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM myevent; +------+--------------+ | id | evt_name | +------+--------------+ | 2 | AFTER INSERT | | 2 | AFTER INSERT | +------+--------------+ 2 rows in set (0.00 sec)
11.4、刪除觸發器
使用DROP TRIGGER語句可以刪除MySQL中已經存在的觸發器,其語法格式為:
DROP TRIGGER [schema_name.]trigger_name
刪除一個觸發器
mysql> DROP TRIGGER test.ins_sum; Query OK, 0 rows affected (0.02 sec)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。