RDBMS PRACTICALS
RDBMS PRACTICALS
Set A
a. Write a procedure to display sum and product of two numbers using IN and OUT>
create or replace procedure sum_product(IN a int,IN b int,OUT sum int,OUT product int)
language plpgsql as $$
begin sum:=a+b; product:=a*b;
raise notice'sum is %',sum; raise
notice'product is %',product;
end $$;
call sum_product(30,40,null,null);
b. Write a procedure to display division of two numbers use raise to display error message.
create or replace procedure division(IN a int,IN b int,OUT division int)
language plpgsql as $$
begin
division:=a/b;
raise notice 'division is %',division;
end $$;
call division(975,5,null);
Set B
b. Write a procedure to accept dept and display all empolyees working in that dept.
create or replace procedure dept() language plpgsql as $$
declare
sempno employee.empno%type;
sename employee.ename%type;
ssalary employee.salary%type;
begin
select empno,ename,salary into sempno,sename,ssalary
from employee where dept ='SALES'; raise notice 'Emp No : %',sempno;
raise notice 'Emp Name : %',sename;
raise notice 'Salary : %',ssalary;
end $$;
call dept();
Set C
a. Write a procedure which will display all bus details for a given route.
Create or replace procedure update () language
plpgsql as $$
declare c1 cursor for select * from Route where
route_no=201; begin update route
set source=002 where route_no=201;
for d1 in c1
loop
raise notice 'route_no is %',d1,route_no;
raise notice 'source is %',d1,source;
end loop;
end $$; call
update();
Set A
7. Find sum and average of first n numbers using conditional loop (while).
Consider the following database maintained by a Bank. The Bank maintains information about its
branches, customers and their loan applications.
Following are the tables:
BRANCH (BID INTEGER, BRNAME CHAR (30), BRCITY CHAR (10))
CUSTOMER (CNO INTEGER, CNAME CHAR (20), CADDR CHAR (35), CITY CHAR(20))
LOAN_APPLICATION (LNO INTEGER, LAMTREQUIRED MONEY, LAMTAPPROVED
MONEY, L_DATE DATE)
The relationship is as follows: BRANCH, CUSTOMER, LOAN_APPLICATION are related
with ternary relationship. TERNARY (BID INTEGER, CNO INTEGER, LNO INTEGER).
a) Write a function that returns the total number of customers of a particular branch.( Accept
branch name as input parameter.)
b.Write a function to accept student name as input and displays details of that student.
c. Write a stored function using cursors, to accept class from the user and display the
details of the students of that class.
create or replace function student() returns varchar language plpgsql as $$
declare
c1 cursor for select roll_no,name,address
from Student where class='9C';
srn Student.roll_no%type;
sname Student.name%type;
saddress Student.address%type;
begin
open c1;
loop
fetch c1 into srn,sname,saddress;
exit when not found;
raise notice 'Roll No = %', srn;
raise notice 'Name = %', sname;
raise notice 'Address = %', saddress;
return srn,sname,saddress;
end loop;
close c1;
end $$;
select student();
ASSIGNMENT 3: CURSOR
Set B
Create the above database in PostGreSQL and insert sufficient records. a. Write a stored
function using cursor which will accept the subject name and print the names of all teachers
teaching that subject.
OUTPUT: t_name=Swanandi
1) Write a cursor to accept the subject's name from the user as an input and display names of all
teachers teaching that student.
do $$
declare
c1 cursor (subname varchar)for select*from Teacher where
t_no=(select t_no from Subject where s_name=subname);
trec Teacher %rowtype;
begin
open c1('Economics');
loop
fetch c1 into trec;
exit when not found;
raise notice 't_name=%',trec.t_name;
end loop;
close c1;
end $$;
OUTPUT: t_name=Swanandi NOTICE: t_name=Gayatri Do
Set C:
A) Create the above database in PostGreSQL and insert sufficient records. a. Write a cursor to
accept a month as an input parameter from the user and display the names of persons
whose birthday falls in that particular month.
do $$
declare
c1 cursor for select*from person where
pno=(select pno from area where area_type='Urban');
personrec Person %rowtype;
begin
open c1;
loop
fetch c1 into personrec;
exit when not found;
raise notice 'Name=%',personrec.name;
end loop;
close c1; end $$;
1. a) Write a cursor which will display year wise details of competitions held. (Use
parameterized cursor)
do $$
declare
c1 cursor for select s_reg_no,count(comp_no)as total_competition from
student_competition group by s_reg_no;
stud_comprec s_comp %rowtype;
begin open c1;
loop
fetch c1 into stud_comprec;
exit when not found;
raise notice 'Student ID=%,total_competition:
%',stud_comprec.s_reg_no,stud_comprec.comp_no;
end loop;
close c1; end $$;
b) Write a cursor which will display student wise total count of competition
participated.
do $$
declare
c1 cursor (year_para varchar) for select S_reg_of,comp_no,rank,year from
s_class where year=year_para;
stu_comprec s_class %rowtype;
begin
open c1(2005);
loop
fetch c1 into stu_comprec;
exit when not found;
raise notice 's_reg_no=%',stu_comprec.s_reg_no;
raise notice 'comp_no=%',stu_comprec.comp_no;
raise notice 'rank=%',stu_comprec.rank;
raise notice 'year=%',stu_comprec.year;
end loop;
close c1;
end $$;
OUTPUT: s_reg_no=1
Comp_no=11
Rank=first
Year=2005
ASIGNMENT 4: TRIGGERS
SET A
a. Write a trigger which will be executed whenever an actor is deleted from the actor table,
display appropriate message.
create table Actor (a_name varchar(30), role varchar(30), charges money, a_address
varchar(30) );
insert into actor values('aditya',2015,50000);
create function delactor1() returns trigger language plpgsql as $$
begin
raise notice 'actor % is deleted',old.a_name; return old;
end $$;
create or replace trigger actortrigger after delete on actor for each row execute procedure
delactor1();
b. Write a trigger which will be executed whenever a movie is deleted from the movie table,
display appropriate message.
create table Movie (m_name varchar (25), release_year integer, budget money);
insert into Movie values('animal',2024,50000);
create function delmovie() returns trigger language plpgsql as $$
begin
raise notice 'movie % is deleted',old.m_name;
return old;
end $$;
create or replace trigger movietrig after delete on movie for each row execute procedure
delmovie();
delete from movie where m_name = 'animal';
c. Write a trigger which will be executed whenever insertion is made to the movie table. If the
budget is less than 1,00,000 do not allow the insertion. Give appropriate message.
create function insertbudget()
returns trigger language plpgsql as $$
begin
if new.budget<10000 then
raise otice ‘budget must be min 100000’;
end if;
end $$;
create or replace trigger movietrigger
before insert on movie
for each row
execute procedure insertbudget();
SET B: DOCTOR
Hospital Database
Consider the following database Doctor (d_no int, d_namevarchar(30), specialization
varchar(35), charges int) Hospital (h_no int, h_namevarchar(20), city varchar(10)) Doctor and
Hospital are related with many to one relationship. Create the above database in PostGreSQL
and insert sufficient records.
a. Write a trigger before insert/update on Doctor table. Raise exception if charges are <0.
create table Doctor
(d_no int, d_name varchar(30),
specialization varchar(35),
charges int);
create function insertdoctor()
returns trigger language plpgsql as $$
begin
if new.charges
raise exception '% cannot have negative charges',new.charges;
end if;
end $$;
create or replace trigger doctortrigger
before insert on doctor
for each row
execute procedure insertdoctor();
insert into doctor
values(1,'ketan','physiotherapy',-252);
b. Write a trigger that restricts insertion of charges value greater than 500.
create function insertdoctor1()
returns trigger language plpgsql as $$
begin
if new.charges>500 then
raise exception '% does not this charges',new.charges;
end if;
end $$;
create or replace trigger doctortrigger
before insert on doctor
for each row
execute procedure insertdoctor1();
insert into doctor
values(2,'mohit','psychologist',600);
SET C:
Student – Subject database
Consider the following database :
Student (rollno integer, name varchar(30),city varchar(50),class varchar(10))
Student and subject are related with M-M relationship with attributes marks_scored.
Create the above database in PostGreSQL and insert sufficient records
a. Write a trigger before insert/update the marks_scored. Raise exception if Marks are
negative.
create function check_marks()
returns trigger language plpgsql as $$
begin
if new.marks_scored<0 then
raise exception 'marks cannot be negative!';
end if;
return new;
end $$;
create trigger stud_marks1
before insert on student_subject
for each row
execute procedure check_marks();
insert into student_subject
values (12,'english',-5);
b. Write a trigger which is executed when insertion is made in the student-subject table. If
marks_scored is less than 0, give appropriate message and do not allow the insertion.
create function check_insertion()
returns trigger language plpgsql as $$
begin
if new.marks_scored<0 then
raise exception '% cannot be negative,insertation aborted',new.marks_scored;
end if;
end $$;
create trigger insert_stud_sub
before insert on stydent_subject
for each row
execute procedure check_insertion();
insert into student_subject
values(22,’MATHS’,-1);
c. Write a trigger which will prevent deleting students from ‘Pune’ city.
create function delstudents()
returns trigger language plpgsql as $$
begin
delete from student where city='pune';
raise exception '% students cannot be deleted',old.city;
end $$;
create trigger delpune
before insert on student
for each row
execute procedure delstudents();
insert into student
values (22,'ketan','pune','fymcs');
delete from student where city = 'pune';