0% found this document useful (0 votes)
12 views

Dbms 9

The document describes implementing PL/SQL functions, procedures and triggers in PostgreSQL. It includes examples of creating various PL/SQL functions like a factorial function and average salary function. It also provides examples of creating triggers that fire after delete, insert and update operations on a table to log the changes.

Uploaded by

Nishant Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Dbms 9

The document describes implementing PL/SQL functions, procedures and triggers in PostgreSQL. It includes examples of creating various PL/SQL functions like a factorial function and average salary function. It also provides examples of creating triggers that fire after delete, insert and update operations on a table to log the changes.

Uploaded by

Nishant Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Name of Roll No

thestudent

Experiment No 09 Date

Title of To implement PL/SQL func ons 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 de nes a new func on. CREATE OR REPLACE FUNCTION will either create a new
func on, or replace an exis ng de ni on. To be able to de ne a func on, the user must have the
USAGE privilege on the language. If a schema name is included, then the func on is created in the
speci ed schema. Otherwise it is created in the current schema. The name of the new func on
must not match any exis ng func on with the same input argument types in the same schema.
However, func ons 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
ti
fi
ti
ti
fi
ti
ti
ti
ti
fi
ti
ti
fi
ti
ti
ti
plpgsqlAs
$$
BEGIN
return
num*num;End;
$$
To access func on:

Select squareFunc(6);
If you drop and then recreate a func on, the new func on is not the same en ty as the old; you
will have to drop exis ng rules, views, triggers, etc. that refer to the old func on. Use CREATE OR
REPLACE FUNCTION to change a func on de ni on without breaking objects that refer to the
func on.

The trigger can be speci ed to re before the opera on is a empted on a row (before constraints
are checked and the INSERT, UPDATE, or DELETE is a empted); or a er the opera on has completed
(a er constraints are checked and the INSERT, UPDATE, or DELETE has completed); or instead of the
opera on (in the case of inserts, updates or deletes on a view). If the trigger res before or instead
of the event, the trigger can skip the opera on for the current row, or change the row being inserted
(for INSERT and UPDATE opera ons only). If the trigger res a er 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 ( condi on ) ] EXECUTE PROCEDURE func on_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
haveEXECUTE privilege on the trigger func on. Use DROP TRIGGER to remove a trigger.

Implementa on:
1. Write a function to find factorial of a
numberQuery :

create or replace function Factorial (num


integer)returns integer
language
plpgsqlAs
$$
declare
i integer;
fact
integer:=1;
begin
for i in
1..numloop
ft
ti
ti
ti
ti
ti
fi
ti
fi
ti
ti
ti
ti
fi
ti
tt
ti
ti
ti
fi
tt
ft
ft
ti
fi
ti
ti
ti
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
plpgsqlas $$
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,
insertthe 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
tabledisplaying 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
rowexecute 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
rowexecute 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
rowexecute 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
rowexecute 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