MySQL笔记之触发器的应用

(转载)http://www.jb51.net/article/36360.htm

触发器是由事件来触发某个操作,这些事件包括INSERT语句,UPDATE语句和DELETE语句。
创建触发器

(1)创建只有一个执行语句的触发器

CREATE TRIGGER 触发器名 BEFORE|AFTER 触发事件

ON 表名 FOR EACH ROW 执行语句

其中,触发器名参数指要创建的触发器的名字

BEFORE和AFTER参数指定了触发执行的时间,在事件之前或是之后

FOR EACH ROW表示任何一条记录上的操作满足触发事件都会触发该触发器

mysql> select * from student;

+------+------+

| age  | name |

+------+------+

|   23 | Rose |

|   28 | Mike |

+------+------+

2 rows in set (0.04 sec)



mysql> select * from thetime;

+---------------------+

| t                   |

+---------------------+

| 2013-04-28 01:05:39 |

+---------------------+

1 row in set (0.00 sec)



mysql> create trigger t1 after insert on student for each row

    -> insert into thetime values(now());

Query OK, 0 rows affected (0.31 sec)



mysql> insert into student values(57, 'Jack');

Query OK, 1 row affected (0.10 sec)



mysql> select * from thetime;

+---------------------+

| t                   |

+---------------------+

| 2013-04-28 01:05:39 |

| 2013-04-28 01:11:45 |

+---------------------+

2 rows in set (0.00 sec)



mysql>

上面创建了一个名为t1的触发器,一旦在student中有插入动作,就会自动往thetime表里插入当前日期和时间

 

(2)创建有多个执行语句的触发器

CREATE TRIGGER 触发器名 BEFORE|AFTER 触发事件

ON 表名 FOR EACH ROW

BEGIN

执行语句列表

END

其中,BEGIN与END之间的执行语句列表参数表示需要执行的多个语句,不同语句用分号隔开

tips:一般情况下,mysql默认是以 ; 作为结束执行语句,与触发器中需要的分行起冲突

   为解决此问题可用DELIMITER,如:DELIMITER ||,可以将结束符号变成||

   当触发器创建完成后,可以用DELIMITER ;来将结束符号变成;

mysql> delimiter &&

mysql> create trigger t2 before delete on student for each row                                   



    -> begin

    -> insert into thetime values(now());                                                        



    -> insert into thetime values(now());

    -> end

    -> &&

Query OK, 0 rows affected (0.04 sec)



mysql> delete from student where age=23 &&

Query OK, 1 row affected (0.02 sec)



mysql> select * from thetime &&

+---------------------+

| t                   |

+---------------------+

| 2013-04-28 01:05:39 |

| 2013-04-28 01:11:45 |

| 2013-04-28 01:23:14 |

| 2013-04-28 01:23:14 |

+---------------------+

4 rows in set (0.00 sec)



mysql> delimiter ;

上面的语句中,开头将结束符号定义为&&,中间定义一个触发器,一旦有满足条件的删除操作

就会执行BEGIN和END中的语句,接着使用&&结束

最后使用DELIMITER ; 将结束符号还原。

 

查看触发器

(3)show triggers语句查看触发器信息

mysql> show triggers \G

*************************** 1. row ***************************

             Trigger: t1

               Event: INSERT

               Table: student

           Statement: insert into thetime values(now())

              Timing: AFTER

             Created: NULL

            sql_mode:

             Definer: root@localhost

character_set_client: utf8

collation_connection: utf8_general_ci

  Database Collation: latin1_swedish_ci

*************************** 2. row ***************************

             Trigger: t2

               Event: DELETE

               Table: student

           Statement: begin

insert into thetime values(now());

insert into thetime values(now());

end

              Timing: BEFORE

             Created: NULL

            sql_mode:

             Definer: root@localhost

character_set_client: utf8

collation_connection: utf8_general_ci

  Database Collation: latin1_swedish_ci

2 rows in set (0.01 sec)



mysql>

结果会显示所有触发器的基本信息

tips:SHOW TRIGGERS语句无法查询指定的触发器

 

(4)在tirggers表中查看触发器信息

mysql> select * from information_schema.triggers \G

*************************** 1. row ***************************

           TRIGGER_CATALOG: def

            TRIGGER_SCHEMA: test

              TRIGGER_NAME: t1

        EVENT_MANIPULATION: INSERT

      EVENT_OBJECT_CATALOG: def

       EVENT_OBJECT_SCHEMA: test

        EVENT_OBJECT_TABLE: student

              ACTION_ORDER: 0

          ACTION_CONDITION: NULL

          ACTION_STATEMENT: insert into thetime values(now())

        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: latin1_swedish_ci

*************************** 2. row ***************************

           TRIGGER_CATALOG: def

            TRIGGER_SCHEMA: test

              TRIGGER_NAME: t2

        EVENT_MANIPULATION: DELETE

      EVENT_OBJECT_CATALOG: def

       EVENT_OBJECT_SCHEMA: test

        EVENT_OBJECT_TABLE: student

              ACTION_ORDER: 0

          ACTION_CONDITION: NULL

          ACTION_STATEMENT: begin

insert into thetime values(now());

insert into thetime values(now());

end

        ACTION_ORIENTATION: ROW

             ACTION_TIMING: BEFORE

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: latin1_swedish_ci

*************************** 3. row ***************************

           TRIGGER_CATALOG: def

            TRIGGER_SCHEMA: view

              TRIGGER_NAME: product_af_insert

        EVENT_MANIPULATION: INSERT

      EVENT_OBJECT_CATALOG: def

       EVENT_OBJECT_SCHEMA: view

        EVENT_OBJECT_TABLE: product

              ACTION_ORDER: 0

          ACTION_CONDITION: NULL

          ACTION_STATEMENT: INSERT INTO operate VALUES(null, 'Insert product', now())

        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: latin1_swedish_ci

3 rows in set (0.35 sec)



mysql>

结果显示了所有触发器的详细信息,同时,该方法可以查询制定触发器的详细信息

(5)查询指定触发器

mysql> select * from information_schema.triggers where trigger_name='t2' \G

*************************** 1. row ***************************

           TRIGGER_CATALOG: def

            TRIGGER_SCHEMA: test

              TRIGGER_NAME: t2

        EVENT_MANIPULATION: DELETE

      EVENT_OBJECT_CATALOG: def

       EVENT_OBJECT_SCHEMA: test

        EVENT_OBJECT_TABLE: student

              ACTION_ORDER: 0

          ACTION_CONDITION: NULL

          ACTION_STATEMENT: begin

insert into thetime values(now());

insert into thetime values(now());

end

        ACTION_ORIENTATION: ROW

             ACTION_TIMING: BEFORE

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: latin1_swedish_ci

1 row in set (0.04 sec)



mysql>

tips:所有触发器信息都存储在information_schema数据库下的triggers表中

   可以使用SELECT语句查询,如果触发器信息过多,最好通过TRIGGER_NAME字段指定查询

(6)删除触发器

mysql> drop trigger t1;

Query OK, 0 rows affected (0.02 sec)



mysql>

删除触发器之后最好使用上面的方法查看一遍

同时,也可以使用database.trig来指定某个数据库中的触发器

tips:如果不需要某个触发器时一定要将这个触发器删除,以免造成意外操作

你可能感兴趣的:(mysql)