0% found this document useful (0 votes)
14 views23 pages

11 - Trigger

The document discusses database triggers. Triggers allow automatic execution of a SQL statement or stored procedure when a triggering event occurs like an INSERT, UPDATE or DELETE statement on a table. Triggers enforce business rules and data integrity. The document explains how to create, show and drop triggers in MySQL.

Uploaded by

mhdsyukronzakka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views23 pages

11 - Trigger

The document discusses database triggers. Triggers allow automatic execution of a SQL statement or stored procedure when a triggering event occurs like an INSERT, UPDATE or DELETE statement on a table. Triggers enforce business rules and data integrity. The document explains how to create, show and drop triggers in MySQL.

Uploaded by

mhdsyukronzakka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

TRIGGER

MATA KULIAH PEMROGRAMAN BASIS DATA


TAHUN AJARAN 2023/2024

arranged by Novi Prisma Yunita, M.Kom

Fakultas Ilmu Komputer


Trigger

INSERT INTO product_log


VALUES (OLD.product_id, ‘UPDATE’, NOW())

Is it possible to automatically run INSERT statement above when UPDATE statement in


products table is executed?

UPDATE products SET stock = 15 WHERE product_id = 7;


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

• The concept of trigger is enforcing a business rules and data integrity


The Use of Trigger

• 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

• Automate insert into a table

• Audit or data log of table or view

• Updating table that still related in business process : stock and order table

• Perform checks of values to be inserted into a table or to perform calculations on values


involved in an update.
Rule Must Considered Before Using Trigger

• 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

• Permission to create a trigger default to a table owner

• The names of DML trigger must follow the rule of identifiers because trigger are database
objects

• You can create a DML trigger in the current database only

• You can’t create a trigger on a temporary table

• A TRUNCATE table doesn’t cause a DELETE trigger to execute


The Different Between Trigger and Stored Procedure

• 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

• Statement-level trigger; A statement-level trigger is executed once for each transaction


regardless of how many rows are inserted, updated, or deleted.

MySQL only supports row-level trigger.


Statement for Trigger

• CREATE

• SHOW

• DROP
CREATE TRIGGER
Trigger

CREATE TRIGGER trigger_name

{BEFORE | AFTER} {INSERT | UPDATE | DELETE}

ON table_name

FOR EACH ROW

BEGIN

-- Trigger body (SQL statements)

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

Parameter NEW and OLD

Trigger Event OLD NEW

INSERT No Yes

UPDATE Yes 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

CREATE TABLE account (acct_num INT, amount DECIMAL(10,2));

CREATE TRIGGER ins_sum


BEFORE INSERT
ON account
FOR EACH ROW
SET @sum = @sum + NEW.amount;
How trigger is activated ?
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

All information about trigger can be obtained in information_schema.triggers, and


is stored mysql.triggers

SELECT TRIGGER_NAME, EVENT_MANIPULATION, EVENT_OBJECT_TABLE,


ACTION_STATEMENT
FROM INFORMATION_SCHEMA.TRIGGERS
WHERE TRIGGER_SCHEMA='dbname';
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

To see the detail of specific trigger, we use statement below:

SHOW CREATE TRIGGER trigger_name


DROP TRIGGER
Drop Trigger

When trigger are created, it is stored in mysql.triggers. Below is the basic


statement to drop a trigger:
DROP TRIGGER [IF EXISTS] [schema_name.]trigger_name

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 ?

You might also like