- A.5.1. 我在哪里可以找到 MySQL 8.4 触发器的文档? A.5.1. Where can I find the documentation for MySQL 8.4 triggers?
- A.5.2. 是否有关于 MySQL 触发器的讨论论坛? A.5.2. Is there a discussion forum for MySQL Triggers?
- A.5.3. MySQL 是否有语句级或行级触发器? A.5.3. Does MySQL have statement-level or row-level triggers?
- A.5.4. 是否有默认触发器? A.5.4. Are there any default triggers?
- A.5.5. MySQL 中的触发器如何管理? A.5.5. How are triggers managed in MySQL?
- A.5.6. 有没有方法查看指定数据库中的所有触发器? A.5.6. Is there a way to view all triggers in a given database?
- A.5.7. 触发器存储在哪里? A.5.7. Where are triggers stored?
- A.5.8. 触发器可以调用存储过程吗? A.5.8. Can a trigger call a stored procedure?
- A.5.9. 触发器可以访问表吗? A.5.9. Can triggers access tables?
- A.5.10. 一个表可以有多个具有相同触发事件和动作时间的触发器吗? A.5.10. Can a table have multiple triggers with the same trigger event and action time?
- A.5.11. 触发器可以更新远程服务器上的表吗? A.5.11. Is it possible for a trigger to update tables on a remote server?
- A.5.12. 触发器在复制中有效吗? A.5.12. Do triggers work with replication?
- A.5.13. 通过触发器在源上执行的操作如何复制到副本? A.5.13. How are actions carried out through triggers on a source replicated to a replica?
A.5.1. | 我在哪里可以找到 MySQL 8.4 触发器的文档?Where can I find the documentation for MySQL 8.4 triggers? |
A.5.2. | MySQL 触发器有讨论论坛吗?Is there a discussion forum for MySQL Triggers? |
有。可以在 https://forums.mysql.com/list.php?99 访问。Yes. It is available at https://forums.mysql.com/list.php?99. | |
A.5.3. | MySQL 是否有语句级或行级触发器?Does MySQL have statement-level or row-level triggers? |
所有触发器都是 | |
A.5.4. | 有默认触发器吗?Are there any default triggers? |
没有明确说明。MySQL 确实对一些 | |
A.5.5. | MySQL 中如何管理触发器?How are triggers managed in MySQL? |
触发器可以使用 有关触发器的信息可以通过查询 | |
A.5.6. | 有没有办法查看指定数据库中的所有触发器?Is there a way to view all triggers in a given database? |
有。您可以使用对 SELECT TRIGGER_NAME, EVENT_MANIPULATION, EVENT_OBJECT_TABLE, ACTION_STATEMENT
FROM INFORMATION_SCHEMA.TRIGGERS
WHERE TRIGGER_SCHEMA='有关此表的更多信息,请参阅 第 28.3.44 节,“INFORMATION_SCHEMA TRIGGERS 表”。For more information about this table, see Section 28.3.44, “The INFORMATION_SCHEMA TRIGGERS Table”. 您也可以使用 | |
A.5.7. | 触发器存储在哪里?Where are triggers stored? |
触发器存储在 | |
A.5.8. | 触发器可以调用存储过程吗?Can a trigger call a stored procedure? |
可以。Yes. | |
A.5.9. | 触发器可以访问表吗?Can triggers access tables? |
触发器可以访问其自身表中的旧数据和新数据。触发器也可以影响其他表,但不允许修改已经被调用该函数或触发器的语句用于读取或写入的表。A trigger can access both old and new data in its own table. A trigger can also affect other tables, but it is not permitted to modify a table that is already being used (for reading or writing) by the statement that invoked the function or trigger. | |
A.5.10. | 一个表可以有多个具有相同触发事件和动作时间的触发器吗?Can a table have multiple triggers with the same trigger event and action time? |
在 MySQL 8.4 中,可以为给定表定义多个具有相同触发事件和动作时间的触发器。例如,你可以为一个表设置两个 | |
A.5.11. | 触发器能否更新远程服务器上的表?Is it possible for a trigger to update tables on a remote server? |
是的。可以使用 | |
A.5.12. | 触发器在复制中能工作吗?Do triggers work with replication? |
能。然而,它们的工作方式取决于你使用的是 MySQL 的 “经典” 基于语句的复制还是基于行的复制格式。Yes. However, the way in which they work depends whether you are using MySQL's “classic” statement-based or row-based replication format. 在使用基于语句的复制时,副本上的触发器由在源服务器上执行并复制到副本上的语句触发。When using statement-based replication, triggers on the replica are executed by statements that are executed on the source (and replicated to the replica). 在使用基于行的复制时,由于在源上执行的语句随后被复制到副本,所以触发器不会在副本上执行。相反,在使用基于行的复制时,在源上执行触发器所引起的更改会应用到副本上。When using row-based replication, triggers are not executed on the replica due to statements that were run on the source and then replicated to the replica. Instead, when using row-based replication, the changes caused by executing the trigger on the source are applied on the replica. 有关更多信息,请参见 第 19.5.1.36 节,“复制和触发器”。For more information, see Section 19.5.1.36, “Replication and Triggers”. | |
A.5.13. | 通过源上的触发器执行的操作如何复制到副本?How are actions carried out through triggers on a source replicated to a replica? |
同样,这取决于您是使用基于语句的复制还是基于行的复制。Again, this depends on whether you are using statement-based or row-based replication. 基于语句的复制。 首先,源端存在的触发器必须在副本服务器上重新创建。完成后,复制流程就像处理任何其他参与复制的标准 DML 语句一样。例如,考虑一个表
基于行的复制。 当你使用基于行的复制时,在源端执行触发器所引起的更改会应用到副本上。然而,在基于行的复制下,触发器本身实际上并不会在副本上执行。这是因为,如果源端和副本都应用了源端的更改,并且触发这些更改的触发器也在副本上执行,则这些更改实际上会在副本上应用两次,从而导致源端和副本的数据不一致。Row-based replication. When you use row-based replication, the changes caused by executing the trigger on the source are applied on the replica. However, the triggers themselves are not actually executed on the replica under row-based replication. This is because, if both the source and the replica applied the changes from the source and, in addition, the trigger causing these changes were applied on the replica, the changes would in effect be applied twice on the replica, leading to different data on the source and the replica. 在大多数情况下,行级复制和语句级复制的结果是相同的。然而,如果源端和副本上使用了不同的触发器,就无法使用行级复制。(这是因为行级格式会将源端触发器执行所产生的更改复制到副本,而不是复制导致触发器执行的语句,而且副本上的对应触发器不会被执行。)相反,任何导致此类触发器执行的语句必须使用语句级复制进行复制。In most cases, the outcome is the same for both row-based and statement-based replication. However, if you use different triggers on the source and replica, you cannot use row-based replication. (This is because the row-based format replicates the changes made by triggers executing on the source to the replicas, rather than the statements that caused the triggers to execute, and the corresponding triggers on the replica are not executed.) Instead, any statements causing such triggers to be executed must be replicated using statement-based replication. 有关更多信息,请参见 第 19.5.1.36 节,“复制和触发器”。For more information, see Section 19.5.1.36, “Replication and Triggers”. |