Monday, September 5, 2016

Triggers

Tiggers:
------------

A SQL trigger is a special type of stored procedure. It is special because it is not called directly like a stored procedure. The main difference between a trigger and a stored procedure is that a trigger is called automatically when a data modification event is made against a table whereas a stored procedure must be called explicitly.

- Trigger activation time can be BEFORE or AFTER. You must specify the activation time when you define a trigger. You use the BEFORE keyword if you want to process action prior to the change is made on the table and AFTER if you need to process action after the change is made.
- The trigger event can be INSERT, UPDATE or  DELETE. This event causes the trigger to be invoked. A trigger only can be invoked by one event. To define a trigger that is invoked by multiple events, you have to define multiple triggers, one for each event.
- A trigger must be associated with a specific table. Without a table trigger would not exist therefore you have to specify the table name after the ON keyword.

Before MySQL version 5.7.2, you can to define maximum six triggers for each table.
 - BEFORE INSERT – activated before data is inserted into the table.
 - AFTER INSERT – activated after data is inserted into the table.
 - BEFORE UPDATE – activated before data in the table is updated.
 - AFTER UPDATE – activated after data in the table is updated.
 - BEFORE DELETE – activated before data is removed from the table.
 - AFTER DELETE – activated after data is removed from the table.

Note: However, from MySQL version 5.7.2+, you can define multiple triggers for the same trigger event and action time.
 - Before MySQL version 5.7.2, you can only create one trigger for an event in a table e.g., you can only create one trigger for the BEFORE UPDATE or AFTER UPDATE event. MySQL 5.7.2+ lifts this limitation and allows you to create multiple triggers for the same event and action time in a table. The triggers will activate sequentially when the event occurs.
 - MySQL will invoke the triggers in the order that they were created. To change the order of triggers, you need to specify FOLLOWS or PRECEDES after the FOR EACH ROW clause.

Limitations:
----------------
 - The trigger cannot use the CALL statement to invoke stored procedures that return data to the client or that use dynamic SQL. (Stored procedures are permitted to return data to the trigger through OUT or INOUT parameters.)
 - The trigger cannot use statements that explicitly or implicitly begin or end a transaction, such as START TRANSACTION, COMMIT, or ROLLBACK. (ROLLBACK to SAVEPOINT is permitted because it does not end a transaction.).
 - Cannot Use SHOW, LOAD DATA, LOAD TABLE, BACKUP DATABASE, RESTORE, FLUSH and RETURN statements.

Note:  log_bin_trust_function_creators affects how MySQL enforces restrictions on stored function and trigger creation.

References:
http://www.mysqltutorial.org/mysql-triggers.aspx
http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html

No comments:

Post a Comment