Академический Документы
Профессиональный Документы
Культура Документы
By Nidhi Bhatnagar
Muhammad Fahim Khan
Objectives
Trigger – Concept
Trigger – Requirement
Trigger - Uses
Trigger – Types
Referencing columns :OLD Vs :New
Trigger – Important points
Muhammad Fahim Khan
Triggers – Concept
Triggers are special PL/SQL construct similar to
procedures.
Triggers – Concept
Triggers – Why
Triggers provides support for following
activities in database:
– Validation : There are certain validations on table
and its data that we cannot provide using constraints
so for all such cases we use triggers for validations.
Eg:
In a bank if we want to provide some validation for
withdrawal of a money in transaction table by
checking the balance in the Account master table.
Muhammad Fahim Khan
Triggers – Why
Eg:
Triggers can be written on a table emp, dept etc to
check and store the name of the person , date and
time of any operation and type of operation
happening on these tables in some other audit table .
Muhammad Fahim Khan
Triggers – Why
Eg:
Trigger can be written on Emp table to store the
empno , name and salary for future use in some back
table, if somebody deletes the record in the emp.
Muhammad Fahim Khan
Triggers – Why
Eg:
Trigger can be written on Emp table to disallow or
prohibit anybody from deleting or modifying any
record in Emp table on Sat and Sun.
Muhammad Fahim Khan
Triggers – Types
Triggers – Types
System level triggers :
– Database level Trigger : Triggers on startup and shutdown of
database.
– Schema level Trigger :
• Triggers on Logon and Logoff from schema.
• DDL Trigger : Triggers on CREATE , ALTER , DROP
Triggers
Note :
In current session we will deal with DML
triggers only as they are used for table
level operations. Other trigger requires
Triggers – Syntax
CREATE [OR REPLACE] TRIGGER <TRIGGER_NAME>
{TRIGGERING_TIME}
{TRIGGERING_EVENT} ON <TABLE_NAME>
[FOR EACH ROW [WHEN (<TRIGGER_CONDITION>)] ]
DECLARE
<Variable_Declaration>
BEGIN
<CODE>
EXCEPTION
<Exception_Handlers>
END;
Muhammad Fahim Khan
Triggers – Syntax
TRIGGERING_TIME
– Before : Specify before if database should
fire trigger (execute trigger) before triggering
event.
Triggers – Syntax
TRIGGERING_EVENT
– INSERT : If trigger should be fired or executed for Insert
operation.
Triggers – Syntax
TABLE_NAME
– Name of the table for which trigger should execute.
Remember one DML trigger is always associated with
one table i.e. a same DML trigger cannot be written for
two different tables.
Triggers – Syntax
NOTE:
For practice of trigger examples.
Please make your own copy of table and
give different name for triggers preferably
with your own employee no else you will
over ride others persons triggers or two same
type of triggers on same table will cause
ambiguity and errors.
Muhammad Fahim Khan
Triggers – Syntax
FOR EACH ROW [WHEN (<TRIGGER_CONDITION>)]
Triggers – Syntax
ROW Level Vs Statement Level
– Example : Suppose there is a Before Update trigger on emp table.
So if we give a update query on SQL prompt then trigger will be
executed automatically by oracle.
– Query is nothing but its like a statement and this update query can
update many rows in a table Emp here.
– If we want trigger to execute only once before update for all those
rows then we will use statement level i.e. without for each row.
Triggers – Syntax
Triggers – Syntax
Trigger – Important
Trigger – Important
Trigger – Important
Mutating Table : "Mutating" means "changing".
A mutating table is a table that is currently being
modified by an update, delete, or insert statement. When
a trigger tries to refer a table that is in state of flux (being
changed), it is considered "mutating" and raises an error
Another way this error can occur is if the trigger has
statements to change the primary, foreign or unique key
columns of the table off which it fires. If we write some
code in trigger that can cause ambiguity with that table :
Example If we write a Insert query on the table in a
Insert trigger of that table itself which is not allowed then
at runtime it will give Mutation error.
Muhammad Fahim Khan
Trigger – Important
:OLD Vs :NEW
:OLD Vs :NEW
:OLD :NEW
:OLD.Col_Name :NEW.Col_Name
UDATE
:OLD.Deptno 10
:OLD.Dname Admin
:OLD.Loc Pune
:NEW.Deptno 10
:NEW.Dname Admin
:NEW.Loc Mumbai
Muhammad Fahim Khan
Trigger – Important
One table we can have all types of trigger each with unique name i.e.
Emp can have 12 triggers
– Before Update Statement level
– Before Update Row level
– After Update Row level
– After Update Statement level
– Before Insert Statement level
– Before Insert Row level
– After Insert Row level
– After Insert Statement level
– Before Delete Statement level
– Before Delete Row level
– After Delete Row level
– After Delete Statement level
Muhammad Fahim Khan
Trigger – Important
Cont….
Muhammad Fahim Khan
Trigger – Important
Update Emp set Sal = Sal + 500 where Deptno = 10;
Suppose this query affects 3 rows.
Trigger – Important
Trigger – Important
Trigger – Important
Example
Create following tables for examples Tables.SQL
Example 51 Sample51.SQL
Example 52 Sample52.SQL
Example 53 Sample53.SQL
Example 54 Sample54.SQL
Example 55 Sample55.SQL
Example 56 Sample56.SQL
Muhammad Fahim Khan
USER_TRIGGERS
Disable/Enable TRIGGERS
Example:
Alter Trigger Emp_Up_1 Disable;
Alter Trigger Emp_Up_1 Enable;
Muhammad Fahim Khan
Deleting TRIGGERS
To delete trigger
Example:
Drop Trigger Emp_Up_1;
Muhammad Fahim Khan
Thank you !!
Next Session:
Object types in
Oracle