1,触发器是由MYSQL的基本命令事件来触发某种特定操作,
触发器有两个重要因素:一是Action(动作)二是Condition(条件)
触发器就是在满足Conditoin条件时执行相应的动作,这个动作就是一系列的SQL语句
2,创建触发器
- mysql> create trigger autolog before insert
- -> on user for each row
- -> insert into logtime values(null,now());
- -> //
- Query OK, 0 rows affected (0.12 sec)
- mysql> insert into user values(null,'123','123',now())//
- Query OK, 1 row affected (0.06 sec)
- mysql> select * from logtime;
- -> //
- +----+---------------------+
- | id | savetime |
- +----+---------------------+
- | 1 | 2012-11-05 18:35:02 |
- +----+---------------------+
- 1 row in set (0.00 sec)
3,查看所有的触发器
- mysql> show triggers
- -> //
- +---------+--------+-------+----------------------------------------+--------+---------+------------
- ----------------------------------------------------+----------------+----------------------+-------
- ---------------+--------------------+
- | Trigger | Event | Table | Statement | Timing | Created | sql_mode
- | Definer | character_set_client | collat
- ion_connection | Database Collation |
- +---------+--------+-------+----------------------------------------+--------+---------+------------
- ----------------------------------------------------+----------------+----------------------+-------
- ---------------+--------------------+
- | autolog | INSERT | user | insert into logtime values(null,now()) | BEFORE | NULL | STRICT_TRAN
- S_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | root@localhost | utf8 | utf8_g
- eneral_ci | utf8_general_ci |
- +---------+--------+-------+----------------------------------------+--------+---------+------------
- ----------------------------------------------------+----------------+----------------------+-------
- ---------------+--------------------+
- 1 row in set (0.01 sec)
4,删除触发器
- mysql> drop trigger autolog//
- Query OK, 0 rows affected (0.01 sec)