18.3. Using Triggers

MySQL 5.0

18.3. Using Triggers

Support for triggers is included beginning with MySQL 5.0.2. This section discusses how to use triggers and some limitations regarding their use. Additional information about trigger limitations is given in Section I.1, “Restrictions on Stored Routines and Triggers”.

A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table. Some uses for triggers are to perform checks of values to be inserted into a table or to perform calculations on values involved in an update.

A trigger is associated with a table and is defined to activate when an , , or statement for the table executes. A trigger can be set to activate either before or after the triggering statement. For example, you can have a trigger activate before each row that is deleted from a table or after each row that is updated.

To create a trigger or drop a trigger, use the or statement. The syntax for these statements is described in Section 18.1, “ Syntax”, and Section 18.2, “ Syntax”.

Here is a simple example that associates a trigger with a table for statements. It acts as an accumulator to sum the values inserted into one of the columns of the table.

The following statements create a table and a trigger for it:

mysql> 
mysql> 
    -> 

The statement creates a trigger named that is associated with the table. It also includes clauses that specify the trigger activation time, the triggering event, and what to do with the trigger activates:

  • The keyword indicates the trigger action time. In this case, the trigger should activate before each row inserted into the table. The other allowable keyword here is .

  • The keyword indicates the event that activates the trigger. In the example, statements cause trigger activation. You can also create triggers for and statements.

  • The statement following defines the statement to execute each time the trigger activates, which occurs once for each row affected by the triggering statement In the example, the triggered statement is a simple that accumulates the values inserted into the column. The statement refers to the column as which means “the value of the column to be inserted into the new row.

To use the trigger, set the accumulator variable to zero, execute an statement, and then see what value the variable has afterward:

mysql> 
mysql> 
mysql> 
+-----------------------+
| Total amount inserted |
+-----------------------+
| 1852.48               |
+-----------------------+

In this case, the value of after the statement has executed is , or .

To destroy the trigger, use a statement. You must specify the schema name if the trigger is not in the default schema:

mysql> 

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.

In addition to the requirement that trigger names be unique for a schema, there are other limitations on the types of triggers you can create. In particular, you cannot have two triggers for a table that have the same activation time and activation event. For example, you cannot define two triggers or two triggers for a table. This should rarely be a significant limitation, because it is possible to define a trigger that executes multiple statements by using the compound statement construct after . (An example appears later in this section.)

The and keywords enable you to access columns in the rows affected by a trigger. ( and are not case sensitive.) In an trigger, only can be used; there is no old row. In a trigger, only can be used; there is no new row. In an trigger, you can use to refer to the columns of a row before it is updated and to refer to the columns of the row after it is updated.

A column named with is read-only. You can refer to it (if you have the privilege), but not modify it. A column named with can be referred to if you have the privilege for it. In a trigger, you can also change its value with = if you have the privilege for it. This means you can use a trigger to modify the values to be inserted into a new row or that are used to update a row.

In a trigger, the value for an column is 0, not the automatically generated sequence number that will be generated when the new record actually is inserted.

and are MySQL extensions to triggers.

By using the construct, you can define a trigger that executes multiple statements. Within the block, you also can use other syntax that is allowed within stored routines such as conditionals and loops. However, just as for stored routines, if you use the mysql program to define a trigger that executes multiple statements, it is necessary to redefine the mysql statement delimiter so that you can use the statement delimiter within the trigger definition. The following example illustrates these points. It defines an trigger that checks the new value to be used for updating each row, and modifies the value to be within the range from 0 to 100. This must be a trigger because the value needs to be checked before it is used to update the row:

mysql> 
mysql> 
    -> 
    -> 
    ->     
    ->         
    ->     
    ->         
    ->     
    -> 
mysql> 

It can be easier to define a stored procedure separately and then invoke it from the trigger using a simple statement. This is also advantageous if you want to invoke the same routine from within several triggers.

There are some limitations on what can appear in statements that a trigger executes when activated:

  • The trigger cannot use the statement to invoke stored procedures that return data to the client or that use dynamic SQL. (Stored procedures are allowed to return data to the trigger through or parameters.)

  • The trigger cannot use statements that explicitly or implicitly begin or end a transaction such as , , or .

  • Prior to MySQL 5.0.10, triggers cannot contain direct references to tables by name.

MySQL handles errors during trigger execution as follows:

  • If a trigger fails, the operation on the corresponding row is not performed.

  • An trigger is executed only if the trigger (if any) and the row operation both execute successfully.

  • An error during either a or trigger results in failure of the entire statement that caused trigger invocation.

  • For transactional tables, failure of a trigger (and thus the whole statement) should cause rollback of all changes performed by the statement. For non-transactional tables, such rollback cannot be done, so although the statement fails, any changes performed prior to the point of the error remain in effect.