SHREE GANESH

SEMINAR ON:-

DATABASE TRIGGERS
SUBMMITED TO:-

PRIYANKA Ma’m

SUBMMITED BY

Kuldeep Kumar
Mca 3rd sem

1
Contents-

2
INTRODUCTION

A database trigger is
procedural code that is
automatically executed in
response to certain events
on a particular table or view
in a database.

3
The events that fire a trigger

Event 1

• DML statements

Event 2

• DDL statements

Event 3

• System events

Event 4

• User events
4
6
The need and the usage
Audit changes

Enforce business rules

Execute business rules.

Enhance performance

8
Major Features
triggers do not accept parameters
or arguments .

triggers cannot perform commit or
rollback operations .

triggers are normally slow

9
Type of trigger

Row
Triggers
and
Statement
Trigger

BEFORE
and
AFTER
Triggers

System
Events
and User
Events

10
Row Triggers and Statement Trigger

Row
Triggers

• A row trigger is fired
each time the table
is affected by the
triggering statement.

Statement
Triggers

• A statement trigger is
fired once on behalf
of the triggering
statement.

11
BEFORE and AFTER Triggers

Before

AFTER

BEFORE triggers run the trigger
action before the triggering
statement is run.
situations:
To eliminate unnecessary
processing
To derive specific column values.

• AFTER triggers run the trigger
action after the triggering statement
is run.

13
Triggers on System Events and User
events
System
Events

User
Events

Database
startup and
shutdown

User logon
and logoff

Data Guard
role
transitions

DDL
statements

Server error
message
events

DML
statements

Exp. DBMS_AQ Package database level
15
Parts of a Trigger

A
triggering
event or
statement

A trigger
restriction

A trigger
action

16
17
THE TRIGGERING EVENT OR STATMENT
A CREATE, ALTER, or DROP statement
on any schema object
An INSERT, UPDATE, or DELETE
statement on a specific table
A database startup or instance
shutdown
A specific error message or any error
message
A user logon or logoff

18
Trigger
Restriction
Return true and false

Exmp :new.parts_on_hand <
new.reorder_point

19
Trigger Action
Depends of true and false
of trigger restriction.
A trigger action is the procedure (PL/SQL block, Java
program, or C callout) that contains the SQL
statements and code to be run when the following
events occur.

20
Trigger Mode

Enabled

• An enabled trigger runs its trigger action
if a triggering statement is issued and the
trigger restriction (if any) evaluates to
true

Disabled

• A disabled trigger does not run its trigger
action, even if a triggering statement is
issued and the trigger restriction (if any)
would evaluate to false.

ALTER TRIGGER <Trigger Name> DISABLE|ENABLE
21
Creating a Database Trigger
Name of the trigger
Table to be associated with
When trigger is to be fired - before or
after
Command that invokes the trigger UPDATE, DELETE, or INSERT
Whether row-level trigger or not
Condition to filter rows.
PL/SQL block that is to be executed
when trigger is fired.

22
General Structure
CREATE [OR REPLACE]

TRIGGER trigger_name
BEFORE (or AFTER)
INSERT OR UPDATE [OF COLUMNS] OR DELETE
ON tablename

[FOR EACH ROW [WHEN (condition)]]
BEGIN
END;
23
Trigger Syntax
CREATE TRIGGER <triggerName>
BEFORE|AFTER INSERT|DELETE|UPDATE
[OF <columnList>] ON <tableName>|<viewName>
[REFERENCING [OLD AS <oldName>] [NEW AS
<newName>]]
[FOR EACH ROW] (default is “FOR EACH
STATEMENT”)
[WHEN (<condition>)]
<PSM body>;

24
Example 1:
CREATE OR REPLACE TRIGGER
PERSON_INSERT_BEFORE
BEFORE INSERT
ON EMP
BEGIN
DBMS_OUTPUT.PUT_LINE(’BEFORE INSERT OF
’ || :NEW.NAME);
END;

25
Example 2:
CREATE OR REPLACE TRIGGER Print_salary_changes
BEFORE DELETE OR INSERT OR UPDATE ON emp
WHEN (EMPNO > 0)
DECLARE
sal_diff number;
BEGIN
sal_diff := :NEW.SAL - :OLD.SAL;
dbms_output.put_line('Old salary: '
||:OLD.sal);
dbms_output.put_line('New salary: ' ||
:NEW.sal);
dbms_output.put_line(' Difference ' ||
sal_diff);
END; /
27
32

trigger dbms

  • 1.
    SHREE GANESH SEMINAR ON:- DATABASETRIGGERS SUBMMITED TO:- PRIYANKA Ma’m SUBMMITED BY Kuldeep Kumar Mca 3rd sem 1
  • 2.
  • 3.
    INTRODUCTION A database triggeris procedural code that is automatically executed in response to certain events on a particular table or view in a database. 3
  • 4.
    The events thatfire a trigger Event 1 • DML statements Event 2 • DDL statements Event 3 • System events Event 4 • User events 4
  • 5.
  • 6.
    The need andthe usage Audit changes Enforce business rules Execute business rules. Enhance performance 8
  • 7.
    Major Features triggers donot accept parameters or arguments . triggers cannot perform commit or rollback operations . triggers are normally slow 9
  • 8.
  • 9.
    Row Triggers andStatement Trigger Row Triggers • A row trigger is fired each time the table is affected by the triggering statement. Statement Triggers • A statement trigger is fired once on behalf of the triggering statement. 11
  • 10.
    BEFORE and AFTERTriggers Before AFTER BEFORE triggers run the trigger action before the triggering statement is run. situations: To eliminate unnecessary processing To derive specific column values. • AFTER triggers run the trigger action after the triggering statement is run. 13
  • 11.
    Triggers on SystemEvents and User events System Events User Events Database startup and shutdown User logon and logoff Data Guard role transitions DDL statements Server error message events DML statements Exp. DBMS_AQ Package database level 15
  • 12.
    Parts of aTrigger A triggering event or statement A trigger restriction A trigger action 16
  • 13.
  • 14.
    THE TRIGGERING EVENTOR STATMENT A CREATE, ALTER, or DROP statement on any schema object An INSERT, UPDATE, or DELETE statement on a specific table A database startup or instance shutdown A specific error message or any error message A user logon or logoff 18
  • 15.
    Trigger Restriction Return true andfalse Exmp :new.parts_on_hand < new.reorder_point 19
  • 16.
    Trigger Action Depends oftrue and false of trigger restriction. A trigger action is the procedure (PL/SQL block, Java program, or C callout) that contains the SQL statements and code to be run when the following events occur. 20
  • 17.
    Trigger Mode Enabled • Anenabled trigger runs its trigger action if a triggering statement is issued and the trigger restriction (if any) evaluates to true Disabled • A disabled trigger does not run its trigger action, even if a triggering statement is issued and the trigger restriction (if any) would evaluate to false. ALTER TRIGGER <Trigger Name> DISABLE|ENABLE 21
  • 18.
    Creating a DatabaseTrigger Name of the trigger Table to be associated with When trigger is to be fired - before or after Command that invokes the trigger UPDATE, DELETE, or INSERT Whether row-level trigger or not Condition to filter rows. PL/SQL block that is to be executed when trigger is fired. 22
  • 19.
    General Structure CREATE [ORREPLACE] TRIGGER trigger_name BEFORE (or AFTER) INSERT OR UPDATE [OF COLUMNS] OR DELETE ON tablename [FOR EACH ROW [WHEN (condition)]] BEGIN END; 23
  • 20.
    Trigger Syntax CREATE TRIGGER<triggerName> BEFORE|AFTER INSERT|DELETE|UPDATE [OF <columnList>] ON <tableName>|<viewName> [REFERENCING [OLD AS <oldName>] [NEW AS <newName>]] [FOR EACH ROW] (default is “FOR EACH STATEMENT”) [WHEN (<condition>)] <PSM body>; 24
  • 21.
    Example 1: CREATE ORREPLACE TRIGGER PERSON_INSERT_BEFORE BEFORE INSERT ON EMP BEGIN DBMS_OUTPUT.PUT_LINE(’BEFORE INSERT OF ’ || :NEW.NAME); END; 25
  • 22.
    Example 2: CREATE ORREPLACE TRIGGER Print_salary_changes BEFORE DELETE OR INSERT OR UPDATE ON emp WHEN (EMPNO > 0) DECLARE sal_diff number; BEGIN sal_diff := :NEW.SAL - :OLD.SAL; dbms_output.put_line('Old salary: ' ||:OLD.sal); dbms_output.put_line('New salary: ' || :NEW.sal); dbms_output.put_line(' Difference ' || sal_diff); END; / 27
  • 23.