Triggers
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.
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.
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.
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.
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