dbms-Triggers&Expetion Handling
dbms-Triggers&Expetion Handling
A trigger is a special kind of stored procedure that automatically executes when an event occurs in the
database server. DML triggers execute when a user tries to modify data through a data manipulation
language (DML) event. DML events are INSERT, UPDATE, or DELETE statements on a table or view
Benefits of Triggers
Triggers can be written for the following purposes
Generating some derived column values automatically
Enforcing referential integrity
Event logging and storing information on table access
Auditing
Synchronous replication of tables
Imposing security authorizations
Preventing invalid transactions
Creating Triggers
The syntax for creating a trigger is
Where,
1. BEFORE Trigger : BEFORE trigger execute before the triggering DML statement (INSERT,
UPDATE, DELETE) execute. Triggering SQL statement is may or may not execute,
depending on the BEFORE trigger conditions block.
2. AFTER Trigger : AFTER trigger execute after the triggering DML statement (INSERT,
UPDATE, DELETE) executed. Triggering SQL statement is execute as soon as followed by
the code of trigger before performing Database operation.
3. ROW Trigger : ROW trigger fire for each and every record which are performing INSERT,
UPDATE, DELETE from the database table. If row deleting is define as trigger event, when
trigger file, deletes the five rows each times from the table.
4. Statement Trigger : Statement trigger fire only once for each statement. If row deleting is
define as trigger event, when trigger file, deletes the five rows at once from the table.
5. Combination Trigger : Combination trigger are combination of two trigger type,
1. Before Statement Trigger : Trigger fire only once for each statement before the
triggering DML statement.
2. Before Row Trigger : Trigger fire for each and every record before the triggering
DML statement.
3. After Statement Trigger : Trigger fire only once for each statement after the
triggering DML statement executing.
4. After Row Trigger : Trigger fire for each and every record after the triggering DML
statement executing.
EXCEPTION HANDLING:
An exception is an error condition during a program execution. PL/SQL supports programmers to catch such
conditions using EXCEPTION block in the program and an appropriate action is taken against the error
condition. There are two types of exceptions
System-defined exceptions
User-defined exceptions
Syntax for Exception Handling
The general syntax for exception handling is as follows. Here you can list down as many exceptions as you
can handle. The default exception will be handled using WHEN others THEN
DECLARE
<declarations section>
BEGIN
<executable command(s)>
EXCEPTION
<exception handling goes here >
WHEN exception1 THEN
exception1-handling-statements
WHEN exception2 THEN
exception2-handling-statements
WHEN exception3 THEN
exception3-handling-statements
........
WHEN others THEN
exception3-handling-statements
END;
2)predefined exception:
PL/SQL provides many pre-defined exceptions, which are executed when any database rule is
violated by a program. For example, the predefined exception NO_DATA_FOUND is raised when a
SELECT INTO statement returns no rows. The following table lists few of the important pre-defined
exceptions
Oracle
Exception SQLCODE Description
Error
It is raised when a null object is automatically
ACCESS_INTO_NULL 06530 -6530
assigned a value.
It is raised when none of the choices in the WHEN
CASE_NOT_FOUND 06592 -6592 clause of a CASE statement is selected, and there is
no ELSE clause.
It is raised when attempts are made to make a cursor
INVALID_CURSOR 01001 -1001 operation that is not allowed, such as closing an
unopened cursor.
It is raised when the conversion of a character string
INVALID_NUMBER 01722 -1722 into a number fails because the string does not
represent a valid number.
It is raised when a program attempts to log on to the
LOGIN_DENIED 01017 -1017
database with an invalid username or password.
It is raised when a SELECT INTO statement returns
NO_DATA_FOUND 01403 +100
no rows.
It is raised when a database call is issued without
NOT_LOGGED_ON 01012 -1012
being connected to the database.
PROGRAM_ERROR 06501 -6501 It is raised when PL/SQL has an internal problem.
It is raised when an attempt is made to divide a
ZERO_DIVIDE 01476 1476
number by zero.