CREATE
[DEFINER = user]
TRIGGER 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
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. tbl_name的表关联,该表必须引用永久表。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.不同架构中的触发器可以具有相同的名称。
This section describes 本节介绍CREATE TRIGGER syntax. CREATE TRIGGER语法。For additional discussion, see Section 25.3.1, “Trigger Syntax and Examples”.有关更多讨论,请参阅第25.3.1节,“触发器语法和示例”。
CREATE TRIGGER requires the TRIGGER privilege for the table associated with the trigger. CREATE TRIGGER要求与触发器关联的表具有TRIGGER权限。If the 如果存在DEFINER clause is present, the privileges required depend on the user value, as discussed in Section 25.6, “Stored Object Access Control”. DEFINER子句,则所需的权限取决于user值,如第25.6节,“存储对象访问控制”所述。If binary logging is enabled, 如果启用了二进制日志记录,则CREATE TRIGGER might require the SUPER privilege, as discussed in Section 25.7, “Stored Program Binary Logging”.CREATE TRIGGER可能需要SUPER权限,如第25.7节,“存储程序二进制日志记录”所述。
The DEFINER clause determines the security context to be used when checking access privileges at trigger activation time, as described later in this section.DEFINER子句确定在触发器激活时检查访问权限时要使用的安全上下文,如本节后面所述。
trigger_time is the trigger action time. trigger_time是触发动作时间。It can be 它可以是BEFORE or AFTER to indicate that the trigger activates before or after each row to be modified.BEFORE或AFTER,指示触发器在要修改的每一行之前或之后激活。
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.BEFORE触发器将不适合列类型的值转换为有效值。
trigger_event indicates the kind of operation that activates the trigger. trigger_event表示激活触发器的操作类型。These 允许使用以下trigger_event values are permitted:trigger_event值:
INSERT: The trigger activates whenever a new row is inserted into the table (for example, through :每当在表中插入新行时(例如,通过INSERT, LOAD DATA, and REPLACE statements).INSERT、LOAD DATA和REPLACE语句),触发器就会激活。
UPDATE: The trigger activates whenever a row is modified (for example, through :无论何时修改行(例如,通过UPDATE statements).UPDATE语句),触发器都会激活。
DELETE: The trigger activates whenever a row is deleted from the table (for example, through :每当从表中删除行时(例如,通过DELETE and REPLACE statements). DELETE和REPLACE语句),触发器就会激活。表上的DROP TABLE and TRUNCATE TABLE statements on the table do not activate this trigger, because they do not use DELETE. DROP TABLE语句和TRUNCATE TABLE语句不会激活此触发器,因为它们不使用DELETE。Dropping a partition does not activate 删除分区也不会激活DELETE triggers, either.DELETE触发器。
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. trigger_event并不表示激活触发器的SQL语句的文本类型,而是表示一种表操作类型。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触发器不仅为INSERT语句激活,而且还为LOAD DATA语句激活,因为这两个语句都将行插入表中。
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.NSERT INTO ... ON DUPLICATE KEY UPDATE ...语法:BEFORE INSERT触发器针对每一行激活,然后是AFTER INSERT触发器或BEFORE UPDATE和AFTER UPDATE触发器,具体取决于该行是否有重复键。
Cascaded foreign key actions do not activate triggers.级联外键操作不会激活触发器。
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. BEFORE UPDATE触发器。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. trigger_order子句,该子句指示follower或prefore以及具有相同触发器事件和操作时间的现有触发器的名称。With 使用FOLLOWS, the new trigger activates after the existing trigger. FOLLOWS时,新触发器在现有触发器之后激活。With 使用PRECEDES, the new trigger activates before the existing trigger.PRECEDES,新触发器在现有触发器之前激活。
trigger_body is the statement to execute when the trigger activates. trigger_body是触发器激活时要执行的语句。To execute multiple statements, use the 要执行多个语句,请使用BEGIN ... END compound statement construct. BEGIN ... END复合语句构造。This also enables you to use the same statements that are permitted within stored routines. 这还允许您使用存储例程中允许的相同语句。See Section 13.6.1, “BEGIN ... END Compound Statement”. 请参阅第13.6.1节,“BEGIN ... END复合语句”。Some statements are not permitted in triggers; see Section 25.8, “Restrictions on Stored Programs”.触发器中不允许使用某些语句;请参阅第25.8节,“存储程序的限制”。
Within the trigger body, you can refer to columns in the subject table (the table associated with the trigger) by using the aliases 在触发器主体中,可以使用别名OLD and NEW. OLD和NEW来引用从表(与触发器关联的表)中的列。OLD.col_name refers to a column of an existing row before it is updated or deleted. 在更新或删除现有行之前,引用该行的列。NEW.col_name refers to the column of a new row to be inserted or an existing row after it is updated.要插入的新行或更新后的现有行的列。
Triggers cannot use 触发器不能使用NEW. or use col_nameOLD. to refer to generated columns. col_nameNEW.或使用col_nameOLD.引用生成的列。col_nameFor information about generated columns, see Section 13.1.20.8, “CREATE TABLE and Generated Columns”.有关生成列的信息,请参阅第13.1.20.8节,“CREATE TABLE和生成的列”。
MySQL stores the MySQL存储在创建触发器时生效的系统变量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.sql_mode设置,并始终在该设置生效的情况下执行触发器主体,而不管触发器开始执行时的当前服务器sql模式如何。
The DEFINER clause specifies the MySQL account to be used when checking access privileges at trigger activation time. DEFINER子句指定在触发激活时检查访问权限时要使用的MySQL帐户。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(). DEFINER子句,则user值应为指定为'的MySQL帐户、user_name'@'host_name'CURRENT_USER或CURRENT_user()。The permitted 允许的user values depend on the privileges you hold, as discussed in Section 25.6, “Stored Object Access Control”. user值取决于您拥有的权限,如第25.6节,“存储对象访问控制”所述。Also see that section for additional information about trigger security.有关触发器安全性的更多信息,请参阅该部分。
If the 如果省略DEFINER clause is omitted, the default definer is the user who executes the CREATE TRIGGER statement. DEFINER子句,则默认的DEFINER是执行CREATE TRIGGER语句的用户。This is the same as specifying 这与显式指定DEFINER = CURRENT_USER explicitly.DEFINER=CURRENT_USER相同。
MySQL takes the MySQL在检查触发器权限时会考虑DEFINER user into account when checking trigger privileges as follows:DEFINER用户,如下所示:
At 在CREATE TRIGGER time, the user who issues the statement must have the TRIGGER privilege.CREATE TRIGGER时,发出语句的用户必须具有TRIGGER权限。
At trigger activation time, privileges are checked against the 在触发激活时,将根据DEFINER user. DEFINER用户检查权限。This user must have these privileges:此用户必须具有以下权限:
The 从表的TRIGGER privilege for the subject table.TRIGGER权限。
The 如果在触发器主体中使用SELECT privilege for the subject table if references to table columns occur using OLD. or col_nameNEW. in the trigger body.col_nameOLD.或col_nameNEW.引用表列,则为主题表提供选择权限。col_name
The 如果表列是触发器正文中UPDATE privilege for the subject table if table columns are targets of SET NEW. assignments in the trigger body.col_name = valueSET NEW.赋值的目标,则col_name = valueUPDATE权限用于从表。
Whatever other privileges normally are required for the statements executed by the trigger.触发器执行的语句通常需要的任何其他特权。
Within a trigger body, the 在触发器主体内,CURRENT_USER function returns the account used to check privileges at trigger activation time. CURRENT_USER函数返回用于在触发器激活时检查权限的帐户。This is the 这是DEFINER user, not the user whose actions caused the trigger to be activated. DEFINER用户,而不是其操作导致触发器被激活的用户。For information about user auditing within triggers, see Section 6.2.22, “SQL-Based Account Activity Auditing”.有关触发器内用户审核的信息,请参阅第6.2.22节,“基于SQL的帐户活动审核”。
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.LOCK TABLES锁定具有触发器的表,则触发器中使用的表也会被锁定,如锁定表和触发器中所述。
For additional discussion of trigger use, see Section 25.3.1, “Trigger Syntax and Examples”.有关触发器使用的更多讨论,请参阅第25.3.1节,“触发器语法和示例”。