触发器和事件
一、触发器
1、触发器的基本概念
触发器是和表相关的一种数据库对象,可以将他看作一种特殊的存储过程,不需要人为调动的存储过程。
- 关键字:trigger
- 基本作用:通过对表进行数据的插入、更新或删除等操作来触发,从而执行某些特定的操作。
2、触发器的创建
可以在MySql命令界面通过’? create trigger '命令查看创建触发器的格式。
mysql> ? create trigger
Description:
Syntax:
CREATE
[DEFINER = user] – 用户名
TRIGGER trigger_name – 触发器名
trigger_time --触发时机 trigger_event – 触发条件
ON tbl_name --表名 FOR EACH ROW – 对每一行
trigger_body – 触发器所做的事情
---------------------------------------------------------------------------------------------
trigger_time: { BEFORE | AFTER }trigger_event: { INSERT | UPDATE | DELETE }
trigger_order: { FOLLOWS | PRECEDES } other_trigger_name
This statement creates a new trigger. A trigger is a named database
object that is associated with a table, and that activates when a
particular event occurs for the table. The trigger becomes associated
with the table named tbl_name, which must refer to a permanent table.
You cannot associate a trigger with a TEMPORARY table or a view.Trigger names exist in the schema namespace, meaning that all triggers
must have unique names within a schema. Triggers in different schemas
can have the same name.
创建触发器的基本格式:
create TRIGGER <trigger_na