You are on page 1of 4

TRIGGERS

What is trigger?
A Trigger is a PL/SQL Block or PL/SQL procedure associated with a table, view, schema, or the
database.
A Trigger executes Implicitly whenever a particular event takes place.
A trigger can either be:
Application Trigger: Fires implicitly whenever an event Occurs with a particular application.
Database Triggering:Fires implicitly whenever a data event (such as DML) or system event (such as
logon or shut down) occurs on a Schema or database.

Note:
-Database tables can be defined on tables and on views.
-The excessive use of triggers can result in complex interdependencies which may be difficult to
maintain in large applications.
-If the logic of trigger is very lengthy, create the stored procedure with logic and invoke them in the
trigger body.
-Database triggers fire for every user each time the events occurs on which the trigger is created.

When should we design Triggers?


We design Triggers to :
-perform related action.
-Centralize global operations

When should we not design Triggers?


We don't design Triggers to :
Where functionality is already built into the Oracle Server.
-That Duplicate others triggers

What are the parts of a database trigger?


The parts of a trigger are:
-A triggering event or statement
-A trigger restriction
-A trigger action

What are the various types of database triggers?


There are 12 types of triggers, they are combination of:
Insert, Delete and Update Triggers.
Before and After Triggers.
Row and Statement Triggers.
(3*2*2=12)

What is the advantage of a stored procedure over a database trigger?


We have control over the firing of a stored procedure but we have no control over the firing of a trigger.
What is the maximum no of statements that can be specified in a trigger statement?
One.
Can views be specified in a trigger statement?
No
What are the values of :new and :old in Insert/Delete/Update Triggers ?
INSERT : new = new value, old = NULL
DELETE : new = NULL, old = old value
UPDATE : new = new value, old = old value

What are cascading triggers? What is the maximum no of cascading triggers at a time?
When a statement in a trigger body causes another trigger to be fired, the triggers are said to be
cascading. Max = 32.

What are mutating triggers?


A trigger giving a SELECT on the table on which the trigger is written.

What are constraining triggers?


A trigger giving an Insert/Update on a table having referential integrity constraint on the triggering
table.
Can database trigger written on synonym of a table and if it can be then what would be the effect
if original table is accessed.
Yes, database trigger would fire.

Is the After report trigger fired if the report execution fails?


Yes.

Can U disable database trigger? How?


Yes. With respect to table
ALTER TABLE TABLE_NAME
DISABLE all_trigger

Can you use a commit statement within a database trigger.


No

What is INSTEAD OF Trigger?


We use INSTEAD OF Triggers to modify data in which the DML statement has been issued against an
non- updatable view. These Triggers are called INSTEAD OF Triggers, because unlike other triggers, the oracle
server fires the trigger Instead of executing the triggering statement. The Trigger is used to perform Insert,
Update, Delete statement against a view, and the Instead of Trigger works invisibly in the background to make
the right action take place.
Why do we INSTEAD OF Trigger?
A view can not be modified by normal DML statements if the view query contains set of operators, group
functions, clauses such as GROUP BY, CONNECT BY, START, The DISTINCT OPERATOR or Joins. For
example if the view contains more than one table, an insert to the view may entail an insertion into one table and
an update to another. So, we write an INSTEAD OF Trigger that fires when we write an insert against the view.
Instead of the original insertion, the trigger body executes, which results in an insertion of data into one table and
an update to another table.

Note:
-If the view is inherently updatable and has INSTEAD OF TRIGGERs, the triggers take precedence. INSTEAD
OF Triggers are row triggers.
-The CHECK OPTION for views is not enforced when insertions or updates to the views are performed by using
INSTEAD OF triggers. The INSTEAD OF trigger body must ensure the check.
- If the INSTEAD OF triggers can only be row trigger, if it is omitted, the trigger is still defined as a row trigger.

TRIGGERING Event that can cause trigger to fire:-


• A data definition statement on an object in the database or schema.
• A specific user (or any user) logging on or off.
• A database shut down or start up.
• A specific or any error that occurs.

DDL trigger fires only if the object being created is a cluster, index, package, procedure, role, sequence,
synonyms, table, tablespace, trigger, type, view or user.

AFTER SERVERERROR: Causes the oracle server to fire the trigger whenever a server error message is
logged.

AFTER LOGON: Causes the oracle server to fire the trigger whenever a user logs onto the database.

BEFORE LOGGOFF: Causes the oracle server to fire the trigger whenever a user logs Off the database.

AFTER STARTUP: Causes the oracle server to fire the trigger whenever the database is opened.

BEFORE SHUTDOWN: Causes the oracle server to fire the trigger whenever the database is shutdown.

What is the use of CALL statement?


A call statement enables you to call a stored procedure, rather than calling the PL/SQL body in the trigger itself.
The procedure can be implemented in PL/SQL, JAVA, C.

There is no semicolon at the end of the CALL statement.

What is Mutating table?


A Mutating table is a table that is currently being modified by an UPDATE, DELETE or INSERT statement, or a
table that might need to be updated by the effects of a declarative DELETE CASCADE referential integrity
action. A table is not considered mutating for STATEMENT triggers.

The triggered table itself is a mutating table, as well as table referencing it with the FOREIGN KEY constraint.
This restriction prevents a row trigger from seeing an inconsistent set of data.
Why do we use trigger?
Trigger can be used for:
• Security
• Auditing
• Data Integrity
• Referential Integrity
• Table Replication
• Computing derived data automatically
• Event logging

You might also like