CREATE
[DEFINER = user]
TRIGGER [IF NOT EXISTS] trigger_name
trigger_time trigger_event
ON tbl_name FOR EACH ROW
[trigger_order]
trigger_body
trigger_time: { BEFORE | AFTER }
trigger_event: { INSERT | UPDATE | DELETE }
trigger_order: { FOLLOWS | PRECEDES } other_trigger_name
该语句创建一个新的触发器。触发器是一个命名的数据库对象,关联到表,当该表发生特定事件时激活。该触发器与名为 tbl_name 的表关联,必须指向永久表。你不能将触发器关联到TEMPORARY table 或 view。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.
IF NOT EXISTS在同一模式IF NOT EXISTS prevents an error from occurring
if a trigger having the same name, on the same table, exists in
the same schema.
本节描述了CREATE
TRIGGER 语法。如需进一步讨论,请参见 第 27.3.1 节,“触发语法和示例”。This section describes CREATE
TRIGGER syntax. For additional discussion, see
Section 27.3.1, “Trigger Syntax and Examples”.
CREATE TRIGGER需要TRIGGER与触发器相关联的表的权限。如果存在DEFINER子句,所需的权限取决于user值,如第 27.6 节,“存储对象访问控制”中所讨论的。如果启用了二进制日志,CREATE TRIGGER可能需要SUPER权限,如在第 27.7 节,“存储程序二进制日志”中讨论的.CREATE TRIGGER requires the
TRIGGER privilege for the table
associated with the trigger. If the DEFINER
clause is present, the privileges required depend on the
user value, as discussed in
Section 27.6, “Stored Object Access Control”. If binary logging is
enabled, CREATE TRIGGER might
require the SUPER privilege, as
discussed in Section 27.7, “Stored Program Binary Logging”.
DEFINER 条款决定了在触发激活时检查访问权限时使用的安全上下文,如本节后文所述。The DEFINER clause determines the security
context to be used when checking access privileges at trigger
activation time, as described later in this section.
trigger_time is the trigger action
time. It can be BEFORE或者AFTER to indicate that the trigger activates
before or after each row to be modified.trigger_time is the trigger action
time. It can be BEFORE or
AFTER to indicate that the trigger activates
before or after each row to be modified.
基本的列值检查发生在触发激活前,因此不能使用 BEFORE 触发器将不适合列类型的值转换为有效值。Basic column value checks occur prior to trigger activation, so
you cannot use BEFORE triggers to convert
values inappropriate for the column type to valid values.
trigger_event。表示激活触发器的操作类型。这些trigger_event值是允许的:trigger_event indicates the kind of
operation that activates the trigger. These
trigger_event values are permitted:
INSERT:每当向表中插入新行时,触发器就会激活(例如,通过INSERTLOAD DATA,以及REPLACE语句)。INSERT: The trigger activates whenever a new row is inserted into the table (for example, throughINSERT,LOAD DATA, andREPLACEstatements).UPDATE:每当行被修改时,触发器就会激活(例如,通过UPDATE语句)。UPDATE: The trigger activates whenever a row is modified (for example, throughUPDATEstatements).DELETE: The trigger activates whenever a row is deleted from the table (for example, throughDELETE以及REPLACEstatements).DROP TABLE以及TRUNCATE TABLEstatements on the table do 在多线程系统中 activate this trigger, because they do not useDELETE. Dropping a partition does not activateDELETEtriggers, either.DELETE: The trigger activates whenever a row is deleted from the table (for example, throughDELETEandREPLACEstatements).DROP TABLEandTRUNCATE TABLEstatements on the table do not activate this trigger, because they do not useDELETE. Dropping a partition does not activateDELETEtriggers, either.
该trigger_event does not represent a
literal type of SQL statement that activates the trigger so much
as it represents a type of table operation. For example, an
INSERT trigger activates not only
for INSERT statements but also
LOAD DATA statements because both
statements insert rows into a table.The trigger_event does not represent a
literal type of SQL statement that activates the trigger so much
as it represents a type of table operation. For example, an
INSERT trigger activates not only
for INSERT statements but also
LOAD DATA statements because both
statements insert rows into a table.
一个可能令人困惑的例子是 INSERT
INTO ... ON DUPLICATE KEY UPDATE ...语法:BEFORE INSERT 触发器对每行激活,随后是 AFTER INSERT 触发器,或者同时触发 BEFORE UPDATE 和 AFTER
UPDATE,具体取决于该行是否存在重复键。A potentially confusing example of this is the INSERT
INTO ... ON DUPLICATE KEY UPDATE ... syntax: a
BEFORE INSERT trigger activates for every row,
followed by either an AFTER INSERT trigger or
both the BEFORE UPDATE and AFTER
UPDATE triggers, depending on whether there was a
duplicate key for the row.
级联外键操作不会激活触发器。Cascaded foreign key actions do not activate triggers.
可以为给定的表定义多个具有相同触发事件和操作时间的触发器。例如,您可以为一张表创建两个BEFORE UPDATE触发器。默认情况下,具有相同触发事件和操作时间的触发器按照它们创建的顺序激活。要影响触发器的顺序,请指定一个trigger_order子句,该子句指示FOLLOWS或PRECEDES以及具有相同触发事件和操作时间的现有触发器的名称。使用FOLLOWS时,新触发器在现有触发器之后激活。使用PRECEDES时,新触发器在现有触发器之前激活。It is possible to define multiple triggers for a given table that
have the same trigger event and action time. For example, you can
have two BEFORE UPDATE triggers for a table. By
default, triggers that have the same trigger event and action time
activate in the order they were created. To affect trigger order,
specify a trigger_order clause that
indicates FOLLOWS or
PRECEDES and the name of an existing trigger
that also has the same trigger event and action time. With
FOLLOWS, the new trigger activates after the
existing trigger. With PRECEDES, the new
trigger activates before the existing trigger.
trigger_body是在触发器激活时要执行的语句。要执行多个语句,请使用BEGIN ... END复合语句构造。这也允许您使用存储例程中允许使用的相同语句。参见第 15.6.1 节,“BEGIN ... END 复合语句”。某些语句在触发器中不允许使用;请参见第 27.8 节,“存储程序的限制”。trigger_body is the statement to
execute when the trigger activates. To execute multiple
statements, use the
BEGIN ... END
compound statement construct. This also enables you to use the
same statements that are permitted within stored routines. See
Section 15.6.1, “BEGIN ... END Compound Statement”. Some statements are not permitted in
triggers; see Section 27.8, “Restrictions on Stored Programs”.
在触发器主体中,您可以使用别名OLD和NEW引用主题表(与触发器关联的表)中的列。OLD.指的是在更新或删除之前现有行的某一列。col_nameNEW.指的是要插入的新行的列或更新后的现有行的列。Within the trigger body, you can refer to columns in the subject
table (the table associated with the trigger) by using the aliases
col_nameOLD and NEW.
OLD. refers
to a column of an existing row before it is updated or deleted.
col_nameNEW. refers
to the column of a new row to be inserted or an existing row after
it is updated.col_name
触发器不能使用NEW.,也不能使用col_nameOLD.引用生成列。有关生成列的信息,请参见第 15.1.20.8 节,“CREATE TABLE 和生成列”。Triggers cannot use
col_nameNEW. or use
col_nameOLD. to
refer to generated columns. For information about generated
columns, see Section 15.1.20.8, “CREATE TABLE and Generated Columns”.col_name
MySQL 存储在创建触发器时生效的sql_mode系统变量设置,并始终在此设置生效的情况下执行触发器主体,无论触发器开始执行时当前服务器的 SQL 模式如何。MySQL stores the sql_mode system
variable setting in effect when a trigger is created, and always
executes the trigger body with this setting in force,
regardless of the current server SQL mode when the
trigger begins executing.
该DEFINER clause specifies the MySQL account
to be used when checking access privileges at trigger activation
time. If the DEFINER clause is present, the
user value should be a MySQL account
specified as
',
user_name'@'host_name'CURRENT_USER,或CURRENT_USER(). The permitted
user values depend on the privileges
you hold, as discussed in
第 27.6 节,“存储对象访问控制”中所讨论的. Also see that section
for additional information about trigger security.The DEFINER clause specifies the MySQL account
to be used when checking access privileges at trigger activation
time. If the DEFINER clause is present, the
user value should be a MySQL account
specified as
',
user_name'@'host_name'CURRENT_USER, or
CURRENT_USER(). The permitted
user values depend on the privileges
you hold, as discussed in
Section 27.6, “Stored Object Access Control”. Also see that section
for additional information about trigger security.
如果省略了DEFINER条款,默认定义者是执行CREATE
TRIGGER 语句。这与明确指定 DEFINER = CURRENT_USER。If the DEFINER clause is omitted, the default
definer is the user who executes the CREATE
TRIGGER statement. This is the same as specifying
DEFINER = CURRENT_USER explicitly.
MySQL 在检查触发权限时会考虑 DEFINER 用户,具体如下:MySQL takes the DEFINER user into account when
checking trigger privileges as follows:
在
CREATE TRIGGER时间),发出语句的用户必须拥有TRIGGER权限。AtCREATE TRIGGERtime, the user who issues the statement must have theTRIGGERprivilege.在触发激活时,权限会与
DEFINER。该用户必须拥有以下权限:At trigger activation time, privileges are checked against theDEFINERuser. This user must have these privileges:该
SELECTprivilege for the subject table if references to table columns occur usingOLD.或者col_nameNEW.in the trigger body.Thecol_nameSELECTprivilege for the subject table if references to table columns occur usingOLD.orcol_nameNEW.in the trigger body.col_name该
UPDATEprivilege for the subject table if table columns are targets ofSET NEW.assignments in the trigger body.Thecol_name=valueUPDATEprivilege for the subject table if table columns are targets ofSET NEW.assignments in the trigger body.col_name=value语句通常需要的其他权限。Whatever other privileges normally are required for the statements executed by the trigger.
在触发体内,CURRENT_USER函数返回触发激活时用于检查权限的账户。这是DEFINER 用户,而不是导致触发器激活的用户。有关触发器内用户审计的信息,请参见 第 8.2.23 节,“基于 SQL 的账户活动审计”。Within a trigger body, the
CURRENT_USER function returns the
account used to check privileges at trigger activation time. This
is the DEFINER user, not the user whose actions
caused the trigger to be activated. For information about user
auditing within triggers, see
Section 8.2.23, “SQL-Based Account Activity Auditing”.
如果你使用 LOCK TABLES 来锁定带有触发器的表,那么触发器内使用的表也会被锁定,如 LOCK TABLES 和 Triggers“。If you use LOCK TABLES to lock a
table that has triggers, the tables used within the trigger are
also locked, as described in
LOCK TABLES and Triggers.
关于触发器的更多讨论,请参见 第 27.3.1 节,”触发器语法和示例“。For additional discussion of trigger use, see Section 27.3.1, “Trigger Syntax and Examples”.