Free Training


C Language  |  CSS  |  MainFrame  |  VBScript  |  PHP  |  XML  |  C++ Tutorials  |  Ajax  |  JavaScript  |  CSS3  |  UML  |  jQuery  |  Microsoft AJAX

Oracle 9i Tutorials

 
Home Tutorials Oracle 9i
 

Tutorial 14: Oracle 9i : Database Triggers

 

Tutorial 14: Oracle 9i : Database Triggers

Page 1 of 2
This Weeks tutorial explains about Oracle Triggers, Building a Trigger, Trigger Execution, Trigger Restrictions and Testing Mutating Tables.

Database Triggers


A database triggers is stored PL/SQL program unit associated with a specific database table or view. The code in the trigger defines the action the database needs to perform whenever some database manipulation (INSERT, UPDATE, DELETE) takes place.



Unlike the stored procedure and functions, which have to be called explicitly, the database triggers are fires (executed) or called implicitly whenever the table is affected by any of the above said DML operations.


Till oracle 7.0 only 12 triggers could be associated with a given table, but in higher versions of Oracle there is no such limitation. A database trigger fires with the privileges of owner not that of user


A database trigger has three parts


  1. A triggering event
  2. A trigger constraint (Optional)
  3. Trigger action

A triggering event can be an insert, update, or delete statement or a instance shutdown or startup etc. The trigger fires automatically when any of these events occur A trigger constraint specifies a Boolean expression that must be true for the trigger to fire. This condition is specified using the WHEN clause. The trigger action is a procedure that contains the code to be executed when the trigger fires.


Types of Triggers


The following are the different types of triggers.


Row triggers and statement triggers


A Row trigger fires once for each row affected. It uses FOR EACH ROW clause. They are useful if trigger action depends on number of rows affected.


Statement Trigger fires once, irrespective of number of rows affected in the table. Statement triggers are useful when triggers action does not depend on


Before and afterTriggers


While defining the trigger we can specify whether to perform the trigger action (i.e. execute trigger body) before or after the triggering statement. BEFORE and AFTER triggers fired by DML statements can only be defined on tables.


BEFORE triggers The trigger action here is run before the trigger statement.


AFTER triggers The trigger action here is run after the trigger statement.


INSTEAD of Triggers provide a way of modifying views that can not be modified directly using DML statements.


LOGON triggers fires after successful logon by the user and LOGOFF trigger fires at the start of user logoff.


Points to ponder


  • A trigger cannot include COMMIT, SAVEPOINT and ROLLBACK.
  • We can use only one trigger of a particular type .
  • A table can have any number of triggers.
  • We use correlation names :new and :old can be used to refer to data in command line and data in table respectively.

Triggers on DDL statements


DDL trigger are of the following types


BEFORE CREATE OR AFTER CREATE trigger is fired when a schema object is created.
BEFORE OR AFTER ALTER trigger is fired when a schema object is altered.
BEFORE OR AFTER DROP trigger is fired when a schema object is dropped.


A trigger can be enabled means can be made to run or it can disabled means it cannot run. A trigger is automatically enabled when it is created. We need re-enable trigger for using it if it is disabled. To enable or disable a trigger using ALTER TRIGGER command, you must be owner of the trigger or should have ALTER ANY TRIGGER privilege. To create a trigger you must have CREATE TRIGGER privilege, which is given to as part of RESOURCE privilege at the time of user creation.


Following figures give more understanding about triggers









Next Page: Tutorial 14: Oracle 9i : Database Triggers - Page 2


Read Next: Tutorial 15: Oracle 9i : Oracle Packages



 

 

Comments


juturik said:

  Yes.

This is very good site to beginers.

Thanx a lot
July 8, 2005, 6:45 am

maran said:

  It is really good place for a beginner to learn. This article would be very fruitful, if you could give a strategy to avoid the mutating issue.

Thanks for a wonderful basics.
August 24, 2005, 3:04 pm

kiranc said:

  Good and sufficient content
August 18, 2006, 11:17 pm

Skhan said:

  Hi,
Its a good place to begin with triggers. It provides solid funda. Please add the advanced info on triggers.
Thanks!
Sami
May 16, 2007, 4:33 am

shivam said:

  Nice, short and excellent description of triggers. Good for beginners. Writtern in easy and lucid language and good illustrations too.
May 17, 2007, 10:39 pm

shivam said:

  Nice description and good illustrations. Excellent for beginners.
May 17, 2007, 10:41 pm

bhanucs said:

  I find it very useful for beginners. THX
June 14, 2007, 1:03 am

koumi said:

  hi dis is the good way of learning course
June 28, 2007, 2:11 am

arulkumar said:

  hi trigger concepts r very good also easy and simple
August 3, 2007, 6:22 am

Murali_Vadoni said:

  It is really helpfull in refreshing the topic

Thanks
September 10, 2007, 6:42 am

AJAY K said:

  This Document is rally good.
September 17, 2007, 2:04 am

KayKay said:

  I was looking for some 1 minute intro to Triggers.....and I got it here the way I wanted.
December 2, 2007, 11:50 am

tharanath said:

  hi. this document is really good and excellent for beginners.....
April 29, 2008, 7:40 am

Prasad.Gandra said:

  Excellent Information for starters... Really easy to Understand
May 16, 2008, 12:02 am

mohan12 said:

  very nice,
but need to add more examples..
May 28, 2008, 7:36 am

Lokesh Devanga said:

  These is very Nice,
but i need some more Examples.
June 17, 2008, 1:33 am

Gayatri Mukherji said:

  Really its too good...Simple but descriptive
July 18, 2008, 2:28 am

lehroy said:

  hey this is good one for getting the conecpt as a beginner level
July 22, 2008, 1:26 pm

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe