Download - trigger dbms
SHREE GANESH
SEMINAR ON:-
DATABASE TRIGGERS
SUBMMITED TO:- SUBMMITED BYKuldeep Kumar
PRIYANKA Ma’m Mca 3rd sem
1
Contents-
2
INTRODUCTION
A database trigger is
procedural code that isautomatically executed inresponse to certain eventson a particular table or view
in a database.
3
The events that fire a trigger
Event 1
Event 2
Event 3
Event 4
• DML statements
• DDL statements
• System events
• 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 parametersor arguments .
triggers cannot perform commit orrollback operations .
triggers are normally slow
9
Type of trigger
RowTriggers
andStatement
Trigger
BEFOREand
AFTERTriggers
SystemEvents
and UserEvents
10
Row Triggers and Statement Trigger
• A row trigger is firedRow
Triggerseach time the tableis affected by the
triggering statement.
• A statement trigger isStatement fired once on behalf
Triggers of the triggeringstatement.
11
BEFORE and AFTER Triggers
BEFORE triggers run the triggeraction before the triggering
Before
AFTER
statement is run.situations:
To eliminate unnecessaryprocessing
To derive specific column values.
• AFTER triggers run the triggeraction after the triggering statement
is run.
13
Triggers on System Events and Userevents
SystemEvents
Databasestartup andshutdown
Data Guardrole
transitions
Server errormessage
events
UserEvents
User logonand logoff
DDLstatements
DMLstatements
Exp. DBMS_AQ Package database level15
Parts of a Trigger
Atriggering A trigger A triggerevent or restriction action
statement
16
17
THE TRIGGERING EVENT OR STATMENT
A CREATE, ALTER, or DROP statementon any schema object
An INSERT, UPDATE, or DELETEstatement on a specific table
A database startup or instanceshutdown
A specific error message or any errormessage
A user logon or logoff
18
TriggerRestriction
Return true and false
Exmp :-
new.parts_on_hand <new.reorder_point
19
Trigger Action
Depends of true and falseof trigger restriction.
A trigger action is the procedure (PL/SQL block, Javaprogram, or C callout) that contains the SQL
statements and code to be run when the followingevents occur.
20
Trigger Mode
• An enabled trigger runs its trigger actionif a triggering statement is issued and the
Enabledtrigger restriction (if any) evaluates to
true
• A disabled trigger does not run its triggeraction, even if a triggering statement isissued and the trigger restriction (if any)
Disabled would evaluate to false.
ALTER TRIGGER <Trigger Name> DISABLE|ENABLE21
Creating a Database Trigger
Name of the trigger
Table to be associated with
When trigger is to be fired - before orafter
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 executedwhen trigger is fired.
22
General Structure
CREATE [OR REPLACE]
TRIGGER trigger_name
BEFORE (or AFTER)
INSERT OR UPDATE [OF COLUMNS] OR DELETEON tablename
[FOR EACH ROW [WHEN (condition)]]
BEGIN
END;23
Trigger SyntaxCREATE 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 EACHSTATEMENT”)
[WHEN (<condition>)]<PSM body>;
24
Example 1:
CREATE OR REPLACE TRIGGERPERSON_INSERT_BEFORE
BEFORE INSERT
ON EMP
BEGINDBMS_OUTPUT.PUT_LINE(’BEFORE INSERT OF
’ || :NEW.NAME);END;
25
Example 2:
CREATE OR REPLACE TRIGGER Print_salary_changesBEFORE DELETE OR INSERT OR UPDATE ON emp
WHEN (EMPNO > 0)
DECLAREsal_diff number;
BEGINsal_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