第12章 触发器

12.1 创建触发器
12.1.1创建只有一个执行语句的触发器
创建一个 触发器的语法如下:

CREATE TRIGGER  trigger_name trigger_time trigger_event
ON tb1_name FOR EACH ROW rtigger_stmt;

trigger_name:标识触发器名称
trigger_time:标识触发时机
trigger_event:标识出发事件(包括INSERT、UPDATE和DELETE)
tb1_name:建立触发器的表名(即在哪张表上建立表名)
trigger_stmt:触发器程序体

例:创建一个单执行语句的触发器:

mysql> CREATE DATABASE trigger_test;
Query OK, 1 row affected (0.01 sec)

mysql> USE trigger_test;
Database changed
mysql> CREATE TABLE account (acc_num INT,amount DECIMAL(10,2));
Query OK, 0 rows affected (0.29 sec)

mysql> CREATE TRIGGER ins_num BEFORE INSERT ON account
    -> FOR EACH ROW SET @sum =@sum+NEW.amount;
Query OK, 0 rows affected (0.22 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.17 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> SELECT @sum;
+------+
| @sum |
+------+
| 3.00 |
+------+
1 row in set (0.00 sec)

12.1.2 创建多个执行语句的触发器


mysql> CREATE TABLE test1(a1 INT);
Query OK, 0 rows affected (0.40 sec)

mysql> CREATE TABLE test2(a2 INT);
Query OK, 0 rows affected (0.34 sec)

mysql> CREATE TABLE test3(a3 INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
Query OK, 0 rows affected (0.32 sec)

mysql> CREATE TABLE test4(a4 INT NOT NULL AUTO_INCREMENT PRIMARY KEY,b4 INT DEFAULT 0);
Query OK, 0 rows affected (0.29 sec)

mysql> DELIMITER //
mysql> CREATE TRIGGER testref BEFORE INSERT ON test1
    -> FOR EACH ROW BEGIN
    -> INSERT INTO test2 SET a2=VALUES(NEW.a1);
    -> UPDATE test4 SET b4=b4+1 WHERE a4=NEW.a1;
    -> END //
Query OK, 0 rows affected (0.22 sec)

mysql> DELIMITER ;
mysql> INSERT INTO test3(a3) VALUES
    -> (NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL),(NULL);
Query OK, 10 rows affected (0.14 sec)
Records: 10  Duplicates: 0  Warnings: 0

mysql> INSERT INTO test4(a4) VALUES
    -> (0),(0),(0),(0),(0),(0),(0),(0),(0),(0);
Query OK, 10 rows affected (0.04 sec)
Records: 10  Duplicates: 0  Warnings: 0

12.2 查看触发器

  • SHOW TRIGGERS语句查看触发器信息
  • 在triggers表中查看触发器信息
SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE condition;

(这里的condition:trigger_name=‘ ’)

12.4 删除触发器
使用DROP TRIGGER语句。
例:

mysql> DROP TRIGGER ins_num;
Query OK, 0 rows affected (0.01 sec)

12.5 综合案例–触发器的使用
01 创建一个业务统计表persons


mysql> CREATE TABLE persons(name VARCHAR(40),num INT);
Query OK, 0 rows affected (0.35 sec)

02 创建一个销售额表sales

mysql> CREATE TABLE sales(name VARCHAR(40),sum INT);
Query OK, 0 rows affected (0.38 sec)

03 创建一个触发器

mysql> CREATE TRIGGER num_sum AFTER INSERT ON persons
    -> FOR EACH ROW INSERT INTO sales VALUES(NEW.name,7*NEW.num);
Query OK, 0 rows affected (0.17 sec)

04 向persons表中插入记录

mysql> INSERT INTO persons VALUES('xiaoxiao',20),('xiaohua',69);
Query OK, 2 rows affected (0.13 sec)
Records: 2  Duplicates: 0  Warnings: 0

05 查看表更新


mysql> SELECT * FROM persons;
+----------+------+
| name     | num  |
+----------+------+
| xiaoxiao |   20 |
| xiaohua  |   69 |
+----------+------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM sales;
+----------+------+
| name     | sum  |
+----------+------+
| xiaoxiao |  140 |
| xiaohua  |  483 |
+----------+------+
2 rows in set (0.00 sec)

你可能感兴趣的:(第12章 触发器)