0% found this document useful (0 votes)
12 views3 pages

Database Trigger

The document discusses database triggers, which are procedures that automatically execute in response to specific events in a database, such as DML operations. It outlines the types of triggers, their components, guidelines for designing them, and the firing sequence during DML operations. The document emphasizes best practices to avoid issues like recursion and unnecessary complexity in trigger logic.

Uploaded by

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

Database Trigger

The document discusses database triggers, which are procedures that automatically execute in response to specific events in a database, such as DML operations. It outlines the types of triggers, their components, guidelines for designing them, and the firing sequence during DML operations. The document emphasizes best practices to avoid issues like recursion and unnecessary complexity in trigger logic.

Uploaded by

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

Dynamic SQL

By Muhammad Raheem [email protected]

A Trigger can be either:


 Application trigger: Fires whenever an event occurs with a particular application
 Database trigger: Fires whenever a data event (such as DML) or system event (such as logon or
shutdown) occurs on a schema or database

Database Trigger

Database Triggers are procedures that are stored in the database and implicitly run, or fired, when
something happens.

Traditionally, triggers supported the execution of a PL/SQL block when an INSERT, UPDATE, or DELETE
occurred on a table or view. Starting with Oracle8i, triggers support system and other data events on
DATABASE and SCHEMA.

A trigger:
 Is a PL/SQL block or a PL/SQL procedure associated with a table, view, schema, or the database
 Executes implicitly whenever a particular event takes place

Database Triggers can be:


 DML triggers on tables.
 INSTEAD OF triggers on views.
 System triggers on DATABASE or SCHEMA:
 With DATABASE, triggers fire for each event for all users;
 With SCHEMA, triggers fire for each event for that specific user.

Guidelines for Designing Triggers

Use the following guidelines when designing your triggers:


 Use triggers to guarantee that when a specific operation is performed, related actions are
performed.
 Do not define triggers that duplicate features already built into Oracle. For example, do not define
triggers to reject bad data if you can do the same checking through declarative integrity
constraints.
 Limit the size of triggers. If the logic for your trigger requires much more than 60 lines of PL/SQL
code, it is better to include most of the code in a stored procedure and call the procedure from the
trigger.
 Use triggers only for centralized, global operations that should be fired for the triggering
statement, regardless of which user or database application issues the statement.
 Do not create recursive triggers. For example, creating an AFTER UPDATE statement trigger
on the Emp table that itself issues an UPDATE statement on Emp, causes the trigger to fire
recursively until it has run out of memory.
 Use triggers on DATABASE judiciously. They are executed for every user every time the event
occurs on which the trigger is created.

DML Trigger Components


A triggering statement contains:
 Trigger timing
 For table: BEFORE, AFTER
 For view: INSTEAD OF
 Triggering event: INSERT, UPDATE, or DELETE
 Table name: On table, view
 Trigger type: Row or statement
 WHEN clause: Restricting condition
 Trigger body: PL/SQL block

1
Dynamic SQL
By Muhammad Raheem [email protected]

TRIGGER TIMING
When should the trigger fire?
BEFORE: Execute the trigger body before the triggering DML event on a table.
AFTER: Execute the trigger body after the triggering DML event on a table.
INSTEAD OF: Execute the trigger body instead of the triggering statement. This is used for views
that are not otherwise modifiable.

TRIGGERING USER EVENT


Which DML statement causes the trigger to execute?
You can use any of the following:
 INSERT
 UPDATE
 DELETE

TRIGGER TYPE
Should the trigger body execute for each row the statement affects or only once?
You can specify that the trigger will be executed once for every row affected by the triggering statement
(such as a multiple row update) or once for the triggering statement, no matter how many rows it affects.
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.

TRIGGER BODY
What action should the trigger perform?
The trigger body is a PL/SQL block or a call to a procedure.

FIRING SEQUENCE
Use the following firing sequence for a trigger on a table, when a single row is manipulated:

DML statement

INSERT INTO dept (deptno,dname, loc)


VALUES (50, 'COMPUTER',’KARACHI’ );

1 row created.

Triggering action

Before Statement Trigger

DEPTNO DNAME LOC


---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

50 KARACHI LAHORE Before Row Trigger


After Row Trigger
After Statement Trigger

2
Dynamic SQL
By Muhammad Raheem [email protected]

FIRING SEQUENCE

Use the following firing sequence for a trigger on a table, when many rows are manipulated:

UPDATE emp
SET sal = sal * 1.1
WHERE deptno = 10

3 rows updated.

Before Statement Trigger


EMPNO ENAME SAL
---------- ---------- ---------- Before Row Trigger
7782 CLARK 3245 After Row Trigger

Before Row Trigger


7839 KING 6050 After Row Trigger

Before Row Trigger


7934 MILLER 1980 After Row Trigger

After Statement Trigger

You might also like