文档首页
MySQL 8.4 参考手册
8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

15.1.22 CREATE TRIGGER 语句15.1.22 CREATE TRIGGER Statement

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, through INSERT, LOAD DATA, and REPLACE statements).

  • UPDATE:每当行被修改时,触发器就会激活(例如,通过 UPDATE 语句)。UPDATE: The trigger activates whenever a row is modified (for example, through UPDATE statements).

  • DELETE: The trigger activates whenever a row is deleted from the table (for example, through DELETE以及REPLACE statements). DROP TABLE以及TRUNCATE TABLE statements on the table do 在多线程系统中 activate this trigger, because they do not use DELETE. Dropping a partition does not activate DELETE triggers, either.DELETE: The trigger activates whenever a row is deleted from the table (for example, through DELETE and REPLACE statements). DROP TABLE and TRUNCATE TABLE statements on the table do not activate this trigger, because they do not use DELETE. Dropping a partition does not activate DELETE triggers, 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.

Note

级联外键操作不会激活触发器。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_name指的是在更新或删除之前现有行的某一列。NEW.col_name指的是要插入的新行的列或更新后的现有行的列。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.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.

触发器不能使用NEW.col_name,也不能使用OLD.col_name引用生成列。有关生成列的信息,请参见第 15.1.20.8 节,“CREATE TABLE 和生成列”。Triggers cannot use NEW.col_name or use OLD.col_name to refer to generated columns. For information about generated columns, see Section 15.1.20.8, “CREATE TABLE and Generated Columns”.

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 权限。At CREATE TRIGGER time, the user who issues the statement must have the TRIGGER privilege.

  • 在触发激活时,权限会与 DEFINER。该用户必须拥有以下权限:At trigger activation time, privileges are checked against the DEFINER user. This user must have these privileges:

    • TRIGGER 权限。The TRIGGER privilege for the subject table.

    • 该SELECT privilege for the subject table if references to table columns occur using OLD.col_name或者NEW.col_name in the trigger body.The SELECT privilege for the subject table if references to table columns occur using OLD.col_name or NEW.col_name in the trigger body.

    • 该UPDATE privilege for the subject table if table columns are targets of SET NEW.col_name = value assignments in the trigger body.The UPDATE privilege for the subject table if table columns are targets of SET NEW.col_name = value assignments in the trigger body.

    • 语句通常需要的其他权限。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”.