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

RDBMS PRACTICALS

RDBMS

Uploaded by

Sid1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

RDBMS PRACTICALS

RDBMS

Uploaded by

Sid1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

ASSIGNMENT 1: PROCEDURES

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

Create table employee (empno,ename,dept,salary).


create table employee (empno int, ename varchar, dept varchar, salary int);

a. Write a procedure to insert values in employee table.


insert into employee
values(1,'MONU','SALES',10000);
insert into employee
values(2,'Rahul','MARKETING',11000);
insert into employee
values(3,'PRABHAT','SALES',10500);
insert into employee
values(4,'RAMU','MARKETING',11500);

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

Route(route_no, source, destination, no_of_station)


Bus (bus_no, capacity, depot_name)
Relationship between Route and Bus is one-to-many
create table Route (route_no int, source int, destination varchar, no_of_stations
int);
create table Bus (bus_no int, capacity int, dept_name varchar, route_no int);
insert into bus
(bus_no,capacity,dept_name,route_n)
values(1,25,'Baner',201); insert into
bus
(bus_no,capacity,dept_name,route_n)
values(2,30,'Aundh',202); insert into
bus
(bus_no,capacity,dept_name,route_no)
values(3,35,'Pashan',203);
insert into route
(route_no,source,destination,no_of_station)
values(201,101,'Baner',111); insert into
route
(route_no,source,destination,no_of_station)
values(202,102,'Pashan',222); insert into
route
(route_no,source,destination,no_of_stations)
values(203,103,'Aundh',333);

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();

b. Write a procedure to update source of route no 101201.


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();
ASSIGNMENT 2: STORED FUNCTIONS

Set A

Using If – Then - else,case,for,while and unconditional loops

1.Find maximum and minimum from two numbers.


create or replace function max_min() returns integer language plpgsql as $$ declare a integer;
b integer; begin a:=10; b:=20; if (a>b) then
raise notice '% is maximum', a; else
raise notice '% is minimum', b;
end if; end $$; select
max_min();

2.Check the number is positive, negative or zero.

create or replace function positive_negative() returns integer language plpgsql as $$


declare
n integer;
begin
n:=10;
if(n>0) then
raise notice 'Number is positive';
elseif(a<0) then
raise notice 'Number is negtaive';
else
raise notice 'Number is zero';
end if;
end $$;
select positive_negative();
3.Find maximum and minimum from three numbers.

create or replace function max_min() returns integer language plpgsql as $$


declare
a integer;
b integer;
c integer;
begin
a:=1;
b:=3;
c:=5;
if(a>b and a>c) then
raise notice 'a is maximum';
elseif(b>a and b>c) then
raise notice 'b is maximum';
else
raise notice 'c is maximum'; return a,b,c;
end if;
end $$;
select max_min();

4. Find number is even or odd.


create or replace function odd_even() returns integer language plpgsql as $$
declare
n integer;
begin
n:=10;
if(n%2=0) then
raise notice 'Number is EVEN';
else
raise notice 'Number is ODD';
end if;
end $$;
select odd_even();
5. Find sum of first 10 numbers(using unconditional loop)
create or replace function sum() returns integer language plpgsql as $$ declare n integer;
sum integer;
begin
sum:=0;
for n in 1..10 loop
sum:=sum+n;
end loop;
raise notice 'Sum of 10 numbers := %',SUM;
end $$;
select sum();

6. Display all odd numbers from 1 to 50.


create or replace function odd_numbers() returns integer language plpgsql as $ $
declare
n integer;
begin
for n in 1..50 loop
if(n%2!=0) then
raise notice 'The value of n is %',n;
end if;
end loop; end $$; select odd_numbers();

7. Find sum and average of first n numbers using conditional loop (while).

create or replace function sum_avg() returns integer language plpgsql as $$


declare
n integer;
i integer;
sum integer;
avg integer;
begin
n:=5;
i:=1;
while (i<=n) loop
sum:=sum+i;
i:=i+1;
avg:=sum/n;
end loop;
Raise notice 'Sum of n numbers:= %',sum;
Raise notice 'Average of n numbers:= %',avg;
end $$;

8.Count even numbers from given range (m to n)(for).

create or replace function even_number() returns integer language plpgsql as $$


declare
m integer;
n integer;
i integer;
even_numbers integer;
begin
m:=0;
n:=5;
even_numbers:=0;
for i in 0..5 loop
if (i%2=0) then
even_numbers:=even_numbers+1;
end if;
end loop;
Raise notice 'Even numbers from m to n:=%',even_numbers;
end $$; select even_number();
9.Search the given number is in given range.

create or replace function no_range(given_number int,start_range


int,end_range int)
returns boolean language plpgsql as $$
declare
i int;
begin
for i in start_range..end_range loop
if i=given_number then
return true;
end if;
end loop;
return false;
end $$;
select no_range(650,200,1000);

10. Display a number in word using (case) loop.


create or replace function even_no(a int,b int) returns integer
language plpgsql as $$
declare
even_count int:=0;
i int;
begin
for i in a..b loop
if(i%2=0) then
even_count:=even_count+1;
end if;
end loop;
return even_count;
end $$;
select even_no(2,22);
Set B

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.)

create or replace function total_customers_of_branch(BRNAME varchar)


returns integer language plpgsql as $$
declare
total_customers integer;
begin
select count(CNO) into total_customers from CUSTOMER
where city = BRNAME;
return total_customers;
end $$;
select total_customers_of_branch('AUNDH');

b) Write a function to find the maximum loan amount approved.

create or replace function max_loan_amount_approved()


returns money language plpgsql as $$
declare
max_loan_amount MONEY;
begin
select MAX(LAMTAPPROVED) into max_loan_amount
from LOAN_APPLICATION;
return max_loan_amount;
end $$;
select max_loan_amount_approved();
c) Create a function which returns the total number of customers who have applied for a
loanmore than Rs.100000. (Accept loan amount as input parameter)

create or replace function greaterloancustomers() returns money language plpgsql as $ $


declare
totalgreaterloancustomers money;
begin
select count(*) into totalgreaterloancustomers from LOAN_APPLICATION
where cast(LAMTAPPROVED as numeric)>100000;
return totalgreaterloancustomers;
end $$;
select greaterloancustomers();
Set E

Consider the following database


Student (roll_no integer, name varchar(30), address varchar(50), class varchar(10))
Subject (scodevarchar(10), subject_name varchar(20))
Student-Subject are related with M-M relationship with attributes marks_scored.
Create the above database in PostGreSQL and insert sufficient records.

create table Student


(roll_no integer PRIMARY KEY,
name varchar(30),
address varchar(50),
class varchar (10));
insert into Student
(roll_no,name,address,class)
values(1,'Aarya Mane','Baner','9A');
insert into Student
(roll_no,name,address,class)
values(2,'Anand Made','Aundh','9B');
insert into Student
(roll_no,name,address,class)
values(3,'Arti Bade','Pashan','9C');
create table Subject
(scode varchar(10) PRIMARY KEY,
subject_name varchar(20));
insert into subject
(scode,subject_name)
values('MATH101','Mathematics');
insert into subject
(scode,
subject_name)
values('SCI102','Science');
insert into subject
(scode,subject_name)
values('ENG103','English');
create table Student_Subject
(roll_no integer,
scode varchar(10),
marks_scored integer,
FOREIGN KEY(roll_no) REFERENCES Student(roll_no),
FOREIGN KEY (scode) REFERENCES Subject(scode),
PRIMARY KEY (roll_no,scode));
insert into Student_Subject
(roll_no,scode,marks_scored)
values(1,'MATH101',95),
(1,'SCI102',85),
(1,'ENG103',95),
(2,'MATH101',80),
(2,'SCI102',88),
(2,'ENG103',92),
(3,'MATH101',92),
(3,'SCI102',89),
(3,'ENG103',93));
a.Write a function which will accept the roll no and print all the details of that student.
create or replace function stude() returns varchar language plpgsql as $$
declare
sname Student.name%type;
saddress Student.address%type;
sclass Student.class%type;
begin
select name,address,class into sname,saddress,sclass
from Student where roll_no=2;
raise notice 'Name = %', sname;
raise notice 'Address = %', saddress;
raise notice 'Class = %', sclass;
return sname,saddress,sclass;
end $$;
select stude();

b.Write a function to accept student name as input and displays details of that student.

create or replace function stud() returns varchar language plpgsql as $$


declare
srn Student.roll_no%type;
saddress Student.address%type;
sclass Student.class%type;
begin
select roll_no,address,class into srn,saddress,sclass
from Student where name='Aarya Mane';
raise notice 'Roll No = %', srn;
raise notice 'Address = %', saddress;
raise notice 'Class = %', sclass;
return srn,saddress,sclass;
end $$;
select stud();

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.

create or replace function tname(subname varchar)returns varchar language plpgsql as $$


declare
c1 cursor for select *from teacher where t_no=(select t_no from subject where
s_name=subname);
trec teacher %rowtype;
begin
open c1;
loop
fetch c1 into trec;
exit when not found;
raise notice 't_name=%',trec.t_name;
end loop;
close c1;
end$$;
select tname('Economics')

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 $$;

B) Write a cursor to display the names of persons living in urban area.


create or replace function income() returns varchar language plpgsql as $$
declare
c1 cursor for select*from person where income between 40000 and 100000;
personrec Person %rowtype;
begin
open c1;
loop
fetch c1 into personrec;
exit when not found;
raise notice '%',personrec.name;
end loop;
close c1;
end $$;
select income();
Set D:

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

Movie – Actor Database


Consider the following database
Movie (m_name varchar (25), release_year integer, budget money) Actor (a_namevarchar(30), role
varchar(30), charges money, a_address varchar(30) ) Movie and Actor are related with many to many
relationship. Create the above database in PostGreSQL and insert sufficient records.

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';

You might also like