MySQL tutorial: CREATE TRIGGER [EN]
top of page
CerebroSQL

MySQL: 

CREATE TRIGGER

Syntax:
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.
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. For additional
discussion, see
https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html.

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
https://dev.mysql.com/doc/refman/8.0/en/stored-objects-security.html.
If binary logging is enabled, CREATE TRIGGER might require the SUPER
privilege, as discussed in
https://dev.mysql.com/doc/refman/8.0/en/stored-programs-logging.html.

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 or AFTER to
indicate that the trigger activates before or after each row to be
modified.

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 indicates the kind of operation that activates the
trigger. These trigger_event values are permitted:

o INSERT: The trigger activates whenever a new row is inserted into the
table (for example, through INSERT, LOAD DATA, and REPLACE
statements).

o UPDATE: The trigger activates whenever a row is modified (for
example, through UPDATE statements).

o 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.

URL: https://dev.mysql.com/doc/refman/8.0/en/create-trigger.html

Example

bottom of page