【转】MYSQL入门学习之十一:触发器的基本操作

时间:2024-10-17 20:33:32

转载地址:http://www.2cto.com/database/201212/176781.html

触发器是MySQL响应以下任意语句而自动执行的一条MySQL语句(或位于BEGIN和END语句之间的一组语句):  www.2cto.com

            DELETE;
            INSERT;
            UPDATE;
            使用触发器,需要MySQL5或之后的版本支持。
一、触发器基本操作
1、创建触发器
        创建触发器时,需要给出4条信息:
        唯一的触发器名;(虽然MySQL5允许不同的表上的触发器名称相同,但一般最好不要这么做。)  www.2cto.com  
        触发器关联的表;
        触发器响应的事件;
        触发器何时执行;
        语法结构:
        create trigger trigger_name (BEFORE|AFTER) (delete|update|insert) on table_name
        for each row
        BEGIN
        要触发的sql语句;
        END;
        示例:
[sql] 
mysql> delimiter |  
mysql> create trigger t_trig before insert on t_goods for each row  
    -> begin  
    ->     set NEW.add_date = current_date();  
    -> end;  
    -> |  
mysql> delimiter ;  
mysql> insert into t_goods(id,goods_name,quantity)  
    -> values(1,'apple',50);  
    -> |  
mysql> select * from t_goods;  
    -> |  
+------+------------+----------+------------+  
| id   | goods_name | quantity | add_date   |  
+------+------------+----------+------------+  
|    1 | apple      |       50 | 2012-12-12 |  
+------+------------+----------+------------+  
2、删除触发器
        DROP TRIGGER [schema_name.]trigger_name;  
        示例:
        mysql> DROP TRIGGER t_trig;
3、查看触发器
        SHOW TRIGGERS [ FROM DBNAME [ like '' ] ];
        示例:
[sql]   www.2cto.com  
mysql> show triggers;  
+---------+--------+---------+------------------------------------------------------------------------------------------  
| Trigger | Event  | Table   | Statement  
+---------+--------+---------+------------------------------------------------------------------------------------------  
| t_trig  | INSERT | t_goods | begin  
    set NEW.add_date = current_date();  
end | BEFORE | NULL    |          | root@localhost | latin1               | latin1_swedish_ci    | latin1_swedish_ci  |  
+---------+--------+---------+------------------------------------------------------------------------------------------  
mysql> show triggers from test;  
+---------+--------+---------+------------------------------------------------------------------------------------------  
| Trigger | Event  | Table   | Statement  
+---------+--------+---------+------------------------------------------------------------------------------------------  
| t_trig  | INSERT | t_goods | begin  
    set NEW.add_date = current_date();  
end | BEFORE | NULL    |          | root@localhost | latin1               | latin1_swedish_ci    | latin1_swedish_ci  |  
+---------+--------+---------+------------------------------------------------------------------------------------------  
mysql> show triggers from test like 't%';  
+---------+--------+---------+------------------------------------------------------------------------------------------  
| Trigger | Event  | Table   | Statement  
+---------+--------+---------+------------------------------------------------------------------------------------------  
| t_trig  | INSERT | t_goods | begin  
    set NEW.add_date = current_date();  
end | BEFORE | NULL    |          | root@localhost | latin1               | latin1_swedish_ci    | latin1_swedish_ci  |  
+---------+--------+---------+------------------------------------------------------------------------------------------  
二、注意事项
1、只有表支持触发器,视图及临时表都不支持;
2、每个表最多支持6个触发器;
3、单一触发器不能与多个事件或多个表关联;
4、对于INSERT而言,新增的行用NEW来表示,行中的每一列的值,用NEW.列名来表示;
        对于DELETE而言,删除的行用OLD来表示;

对于UPDATE而言,更新前的行用OLD来表示,更新后的行用NEW来表示