Professional Documents
Culture Documents
Procedures and Triggers1
Procedures and Triggers1
Procedures and Triggers1
Discuss PL/SQL
Discuss
procedures
Create a
procedure
Discuss trigger
Create a trigger
What is PL/SQL and what it can do?
PL/SQL program block
Declaration section :- necessary variables are declared
in this section
Begin : this part of block consist of SQL and PL/SQL
statements that assign values,controle execution and
manipulate data
Exception :this part contains code that deals with
exceptions that may be raised during the execution of
code in the executable part
EXAMPLE
DECLARE
A NUMBER(6);
B NUMBER(6);
BEGIN
A := 23;
B := A * 5;
IF A < B THEN
DBMS_OUTPUT.PUT_LINE(’Ans: ’ || A || ’ is less
than ’ || B);
END IF;
END;
ROW TRIGGER :-
This trigger is fired each time a row in in the table is affercted
by triggering statement
STATEMENT TRIGGER:-
Statement trigger is fired once on behalf of the triggering
statement independent of number of rows the triggering
staement effect
BEFORE TRIGGER:-
before trigger executes trigger action before triggering statement
it is used when trigger action determine wether or not
triggering statement should be allowed to complete.
by ussing before trigger user can eleminate
unecessary processing of trigger statement
AFTER TRIGGER:-
after trigger executes the trigger action after triggering
statement is executed
if before trigger is present after trigger can perform different
action on same trigger
Syntax for creating trigger
CREATE OR REPLACE TRIGGER[schema] <trigger
name>{before,after}{DELETE,INSERT,UPDATE[ of
column,….]} ON [schema] <table name>[REFERENCING
{old as old, New as new}][for each row [when condition]]
DECLARE
<variable declaration>;
<constant declaration>;
BEGIN
<PL/SQL subprogram body>
EXCEPTION
<Exception PL/SQL block>;
END;
REPLACE –recreates the
procedure if it already exist Explanation
SCHEMA – is the schema to
contain procedure CREATE OR REPLACE
TRIGGER NAME – Name of TRIGGER[schema] <trigger
trigger to be created name>{before,after}
{DELETE,INSERT,UPDATE[ of
BEFORE – indicates trigger is fired column,….]} ON [schema] <table
before execution of trigger name>[REFERENCING {old as
AFTER - indicates trigger is fired old, New as new}][for each row
after execution of trigger [when condition]]
DELETE – indicate trigger is fired DECLARE
when DELETE statement removes
row from table <variable declaration>;
INSERT - indicate trigger is fired <constant declaration>;
when INSERT statement removes BEGIN
row from table <PL/SQL subprogram body>
UPDATE-indicate trigger if fires EXCEPTION
when UPDATE changes value in
one of the specified column in the <Exception PL/SQL block>;
OF clause. Else trigger is fired END;
whenever update statement is
executed
CREATE OR REPLACE
ON – specifies the schema
TRIGGER[schema] <trigger
and name of the table on name>{before,after}
which trigger is to be {DELETE,INSERT,UPDATE[
of column,….]} ON [schema]
executed <table name>[REFERENCING
REFERENCING- specifies {old as old, New as new}][for
corelation name each row [when condition]]
DECLARE
FOR EACH ROW-
<variable declaration>;
designates to be a trigger <constant declaration>;
row BEGIN
WHEN – specifies the <PL/SQL subprogram
trigger restriction body>
PL/SQL BLOCK- it is the EXCEPTION
block executed by database <Exception PL/SQL
block>;
wwhen trigger is fired END;
Example
CREATE or REPLACE TRIGGER IncreaseDiscount
AFTER INSERT on Orders
FOR EACH ROW
BEGIN
UPDATE Customers SET discnt = discnt+.1
WHERE Customers.cid=:new.cid;
END;
/
:new and :old represent reserved words for Oracle’s PL/SQL. In this example,
:new represents the cid of the new row in the orders table that was just
inserted.
Deleting a trigger