11 - Trigger
11 - 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
• A trigger is a special kind of stored procedure that automatically executes when an event
occurs in the database server.
• The event occurs when a user tries to modify data through a data manipulation language
(DML): INSERT, UPDATE, DELETE, and any combination of that
• Run the process that related to data integrity that couldn’t be execute by other process (ex
constraint), example : business process checking to ensure data stay valid
• Updating table that still related in business process : stock and order table
• The CREATE TRIGGER statement must be the first statement in a batch. All other statement in
a batch are interpreted as the definition of the trigger
• The names of DML trigger must follow the rule of identifiers because trigger are database
objects
• A trigger fires in response to an event. It can’t be executed directly using a CALL statement.
By contrast, a stored procedure is executed using a CALL statement and can’t respond to a
database event.
• A stored procedure may have one or more parameters. A trigger can’t have parameters
• A stored procedure has a return value by using OUT parameter. A trigger has no return value
Two Types of Trigger
• Row-level trigger; A row-level trigger is activated for each row that is inserted, updated, or
deleted
• CREATE
• SHOW
• DROP
CREATE TRIGGER
Trigger
ON table_name
BEGIN
END;
Create Trigger
Explanation:
• trigger_name: Is the name of the trigger. A trigger name must conform to the rules for
identifiers and must be unique within the database.
• BEFORE | AFTER: Specifies when the trigger should be executed.
• INSERT | UPDATE | DELETE: Specifies the type of operation that activates the trigger.
• table_name: Is the table or view on which the trigger is executed and is sometimes called the
trigger table or trigger view
• trigger body: section where SQL statements are defined
Create Trigger
INSERT No Yes
DELETE Yes No
Create Trigger
• Trigger can have the same name within the local server but can’t within the same database
• One table can be associated with multiple triggers that have same trigger time and event,
and are activated in the order they were created
• Modify trigger order (that have the same event and time) using the keyword: FOLLOWS and
PRECEDES
• FOLLOWS: news trigger activated after the existing trigger
• PRECEDES: news trigger activated before the existing trigger
Create Trigger
Trigger is activated when event is executed. As for the example above: the event is
INSERT into account table. We do nothing for the name of trigger.
SET @sum = 0;
INSERT INTO account VALUES(137,14.98),(141,1937.50),(97,-100.00);
SELECT @sum AS 'Total amount inserted';
SHOW LIST OF TRIGGER
Show List of Trigger
Other way we can use the statement below to list all triggers
SHOW TRIGGERS
[{FROM | IN} db_name]
[LIKE 'pattern' | WHERE expr]
Show Create Trigger Statement
The schema_name is optional. User must have TRIGGER privilege for dropping trigger,
also privilege to drop table associated with the trigger.
If table associated with trigger is dropped, the trigger automatically dropped.
QUESTION ?