Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Dbms Exp9

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Name of the Nishant Patil Roll No 9629

student

Experiment 09 Date
No
Title of To implement PL/SQL funcAons and triggers
the
Experime
nt
Related Course outcome :
CO4: Formulate SQL queries

Related Lab outcome :


LO4: Use PL/SQL Constructs.

Objec&ve:

1. To implement PL/SQL procedures, functions and triggers


Theory
CREATE FUNCTION defines a new funcAon. CREATE OR REPLACE FUNCTION will either create a new
funcAon, or replace an exisAng definiAon. To be able to define a funcAon, the user must have the
USAGE privilege on the language. If a schema name is included, then the funcAon is created in the
specified schema. Otherwise it is created in the current schema. The name of the new funcAon
must not match any exisAng funcAon with the same input argument types in the same schema.
However, funcAons of different argument types can share a name (this is called overloading).

Syntax to create procedure:


CREATE [OR REPLACE] PROCEDURE procedure_name
[(parameter1
datatype1, parameter2
datatype2,
. . .)
] AS
PL/SQL Bloc

Syntax to create procedure:


CREATE [OR REPLACE] FUNCTION
function_name
[(parameter1
datatype1, parameter2
datatype2,
. . .)]
RETURNS datatype
AS
PL/SQL Block;

To access function use the select statement


Select function_name/procedure_name (argument_value)

Example:
create or replace function squareFunc(num
numeric) returns numeric
Language
plpgsql As
$$
BEGIN
return num*num;
End; $$
To access funcAon:

Select squareFunc(6);
If you drop and then recreate a funcAon, the new funcAon is not the same enAty as the old; you
will have to drop exisAng rules, views, triggers, etc. that refer to the old funcAon. Use CREATE OR
REPLACE FUNCTION to change a funcAon definiAon without breaking objects that refer to the
funcAon.

The trigger can be specified to fire before the operaAon is aVempted on a row (before constraints
are checked and the INSERT, UPDATE, or DELETE is aVempted); or aXer the operaAon has
completed (aXer constraints are checked and the INSERT, UPDATE, or DELETE has completed); or
instead of the operaAon (in the case of inserts, updates or deletes on a view). If the trigger fires
before or instead of the event, the trigger can skip the operaAon for the current row, or change the
row being inserted (for INSERT and UPDATE operaAons only). If the trigger fires aXer the event, all
changes, including the effects of other triggers, are "visible" to the trigger.

Syntax of Trigger

CREATE [ CONSTRAINT ] TRIGGER name

{ BEFORE | AFTER | INSTEAD OF } { event [ OR ... ] } ON table

[ FROM referenced_table_name ]

[ NOT DEFERRABLE | [ DEFERRABLE ]

{ INITIALLY IMMEDIATE | INITIALLY DEFERRED } ]

[ FOR [ EACH ] { ROW | STATEMENT } ] [ WHEN ( condiAon ) ] EXECUTE PROCEDURE funcAon_name


( arguments )

where event can be one of: INSERT UPDATE [ OF column_name [, ... ] ] DELETE TRUNCATE To create
a trigger on a table, the user must have the TRIGGER privilege on the table. The user must also have
EXECUTE privilege on the trigger funcAon. Use DROP TRIGGER to remove a trigger.

Implementa&on:
1. Write a function to find factorial of a number
Query :

create or replace function Factorial (num


integer) returns integer
language
plpgsql As
$$ declare
i integer;
fact
integer:=1;
begin
for i in
1..num loop
fact:=fact*i;
end loop; return
fact; end; $$ select
Factorial(5);

2. Create table emp(id,name,salary) and insert 3 records in it.

Query:

create table emp(id integer,name


varchar(10),salary integer); select * from emp;
insert into emp values(1,'Jack',42000); insert into
emp
values(3,'Rohan',44000); insert into emp
values(4,'Rahul',48000);

3. Write a function to find average salary from emp table.

Query:

create or replace
function AvgSal()
returns numeric
language plpgsql as $$
declare avg numeric;
begin
select avg(salary) into avg from
emp; return avg; end $$ select
AvgSal();
4. Write a trigger that would fire after delete operation performed on emp table, insert
the deleted tuple in emp1(emp-id,name,salary) table and display the appropriate
message. Query:

create or replace function


insertrow() returns trigger
as $ $ begin insert into emp1 values (old.id,
old.name,old.salary); raise notice 'Deleted from
emp inserted in emp1'; return old; end; $$ language
plpgsql;

create trigger deletetrigger after


delete on emp for each row
execute procedure insertrow();

5. Write a trigger that would fire after insert/update/delete operations performed on emp
table displaying the date on which data manipulation performed.

Query:

1.Insert : create trigger


inserttrig after insert on emp for
each row execute procedure
insertrow(); create or replace
function insertrow() returns
trigger as $$ declare a
date; begin select
current_date into a; raise
notice 'Date: %',a; return
new; end; $$ language
plpgsql;

create trigger inserttrig after insert on


emp for each row execute procedure
insertrow(); insert into emp
values(5,'John',80000);
2.Update :
create or replace function
updaterow() returns
trigger as $$ declare a
date; begin select
current_date into a; raise
notice 'Date: %',a; return
new; end; $$ language
plpgsql;

create trigger updatetrig after


update on emp for each row
execute procedure
updaterow();
UPDATE emp set name = 'Luke' where id = 1;

3.Delete create or replace


function deleterow()
returns trigger as $$
declare a date; begin
select current_date into
a; raise notice 'Date:
%',a; return old; end; $$
language plpgsql;

create trigger deletetrig after


delete on emp for each row
execute procedure
deleterow(); delete from
emp where id=2;

language plpgsql;

delete from emp where id=3;

3.Update
create trigger updatetriggger1
before update on emp for each
row execute procedure
updaterow(); create or replace
function updaterow() returns
trigger as $$ begin raise
notice 'updation not allowed';
return null; end; $$ language
plpgsql;

UPDATE emp set name = 'Abhishek' where id = 4;

You might also like