In this lesson, you learn what a database trigger is, and what it can be used for.
Triggers allow specified actions to be performed automatically within the database, without having to write any extra application code.
Triggers increase the power of the database, and the power of your application.
You will learn much more about triggers in the following lessons.
2. OBjectives
• Describe different types of triggers
• Describe database triggers and their use
• Create database triggers
• Remove database triggers
PUCIT- By BSEF12A003
2
3. What is a Trigger?
A trigger is procedural SQL code that is
automatically invoked by the RDBMS upon the
occurrence of a given data manipulation event. It is
useful to remember that. A trigger is associated
with a database table. Each database table may
have one or more triggers.
PUCIT- By BSEF12A003
3
4. What is a Trigger? (Cont..)
A database trigger:
Is a PL/SQL block associated with a specific action (an
event) on a database object such as a table or view.
Is stored in the database
Executes automatically whenever the associated action
occurs.
A trigger is invoked before or after a data row is
inserted, updated, or deleted.
4
5. Where we Should use Trigger?
Triggers play a critical role in making the database
truly useful, they also add processing power to the
RDBMS and to the database system as a whole.
Oracle recommends triggers for,
Auditing purposes (creating audit logs).
PUCIT- By BSEF12A003
5
6. Where we should use Trigger?
Automatic generation of derived column values.
Enforcement of business or security constraints.
Creation of replica tables for backup purposes.
Prevent invalid DML transactions from occurring
Triggers can be used to enforce constraints that cannot be
enforced at the DBMS design and implementation levels.
And many other purposes !
PUCIT- By BSEF12A003
6
7. How to implement a Trigger?
The syntax to create a trigger in Oracle is:
PUCIT- By BSEF12A003
7
CREATE OR REPLACE TRIGGER trigger_name
[BEFORE / AFTER] [DELETE / INSERT / UPDATE OF
column_name] ON table_name
[FOR EACH ROW]
[DECLARE]
[variable_namedata type[:=initial_value] ]
BEGIN
PL/SQL instructions;
..........
END;
8. Components of syntax of trigger
PUCIT- By BSEF12A003
8
The triggering timing: BEFORE or AFTER
The triggering event: the statement that causes the trigger to execute
(INSERT, UPDATE, or DELETE).
The triggering level: There are two types of triggers:
- A statement-level trigger is assumed if you omit the FOR EACH ROW
keywords. This type of trigger is executed once, before or after the
triggering statement is completed. This is the default case.
- A row-level trigger requires use of the FOR EACH ROW keywords. This type
of trigger is executed once for each row affected by the triggering
statement. (In other words, if you update 10 rows, the trigger executes 10
times.)
9. Components of syntax of trigger
The triggering action: The PL/SQL code enclosed between
the BEGIN and END keywords. Each statement inside the
PL/SQL code must end with a semicolon “;”.
PUCIT- BSEF12A003
9
10. Before Coding..
Part Description Possible Values
Trigger timing When the trigger fires in
relation to the
triggering event
BEFORE
AFTER
INSTEAD OF
Triggering event Which data manipulation
operation on the
table or view causes the
trigger to fire
INSERT
UPDATE
DELETE
Trigger type How many times the trigger
body
executes
Statement
Row
Trigger body What action the trigger
performs
Complete PL/SQL block
PUCIT- BSEF12A003
10
11. DML Trigger Components
Statement: The trigger body executes once for the
triggering event. This is the default. A statement
trigger fires once, even if no rows are affected at all.
Row: The trigger body executes once for each row
affected by the triggering event. A row trigger is not
executed if the triggering event affects no rows.
PUCIT- BSEF12A003
11
12. Example 1:
Creating Logging Records Automatically
The Database Administrator wants to keep an automatic record (in a
database table) of who logs onto the database, and when. He/she
could create the log table and a suitable trigger as follows:
PUCIT- By BSEF12A003
12
13. Example 2:
Create a trigger to restrict inserts into the EMPLOYEES table to certain business
hours, Monday through Friday.
Other wise Show Error message.
PUCIT- By BSEF12A003
13
16. Creating a DML Row Trigger
PUCIT- By BSEF12A003
16
SYNTAX:
CREATE [OR REPLACE] TRIGGER trigger_name
timing
event1 [OR event2 OR event3]
ON table_name
[REFERENCING OLD AS old | NEW AS new]
FOR EACH ROW
[WHEN (condition)]
trigger_body
19. Creating an INSTEAD OF Trigger
PUCIT- By BSEF12A003
19
SYNTAX:
CREATE [OR REPLACE] TRIGGER trigger_name
INSTEAD OF
event1 [OR event2 OR event3]
ON view_name
[REFERENCING OLD AS old | NEW AS new]
[FOR EACH ROW]
trigger_body