RDBMS Practical Assignment Solutions1
RDBMS Practical Assignment Solutions1
SET B
1. Using Bus Transportation Database
a. Write a procedure to list the drivers working on a particular date shift wise.Accept the date as an input parameter.
b. List the bus_no and drivers allocated to the buses which are running from ‘Nasik Road’ to ‘CBS’ on date .
SET C
1. Using Student Competition Database
a. Write a procedure to count the number of competitions which come under the type ‘Sports’ and no. of competitions which
come under the type ‘academics’.
b. Write a stored procedure which accepts year as input and gives a list of all competitions held in that year.
create table b_c_loan(bid int references branch(bid),cno int references customer(cno),lno int
references loan_app(lno));
Insert Values :-
insert into branch values(1,'aundh','nashik');
insert into branch values(2,'deccon','pune');
insert into branch values(3,'m.g.road','mumbai');
insert into branch values(4,'rk','nashik');
insert into branch values(5,'cbs','nashik');
a) Write a function that returns the total number of customers of a particular branch.( Accept
branch name as input parameter.)
create table prog_emp(pno int references project(pno),eno int references emp(eno),s_date date);
CREATE TABLE
Input Values:-
Write a function to accept project name as input and returns the number of employees working
on the project.
Write a function which accepts employee name and prints the details of the project which the
employee works on.
a) Write a stored function using cursors to accept a city from the user and to list all warehouses in the city.
create function fun2(nm varchar(20))returns void as '
declare c1 cursor for select wname from warehouse where city=nm;
wn char(30);
Begin
open c1;
loop
fetch c1 into wn;
exit when not found;
raise notice''warehouse name:-%'',wn;
end loop;
close c1;
end '
language 'plpgsql';
CREATE FUNCTION
b) Write a stored function using cursors to find the list of items whose cost is between Rs.5000 to 10000
create function fun4()returns void as '
declare c3 cursor for select ino,description from items where cost between 500
and 1000;
ino int;
d text;
begin
open c3;
loop
fetch c3 into ino,d;
exit when not found;
raise notice''item nos:%'',ino;
raise notice''description:%'',d;
end loop;
close c3;
end '
language 'plpgsql';
CREATE FUNCTION
SET B
Company –Person database
Company(Name varchar(30),address (50),city varchar(20), phone varchar(10), share _value money)
Person(pname varchar(30),pcity varchar (20))
Company_Person are related with M to M relationship with descriptive attribute No_of_shares. Integer
a) Write a stored function using cursors to transfer the shares owned by ‘Sachin ‘ to ‘Rahul’.
create function b11()returns void as '
declare cb1 cursor for select pname,nos_of_share from cp where pname=''sachin'';
p varchar(30);
n int;
begin
open cb1;
loop
fetch cb1 into p,n;
exit when not found;
update cp set nos_of_share=nos_of_share+n where pname=''Rahul'';
update cp set nos_of_share=nos_of_share-n where pname=p;
end loop;
close cb1;
end '
language 'plpgsql';
CREATE FUNCTION
b) Write a stored function using cursors to print the total number of distinct investors along with its total invested value.
SET C
Student –Marks database
Student (rollno integer,name varchar(30),address varchar(50),class varchar(10)) Subject(Scode varchar(10),subject
name varchar(20))
student and subject are related with M-M relationship with attributes marks scored. Create a RDB in 3NF for the above
and solve the following.
a) Write a stored function using cursors, to accept a address from the user and display the name,subject and the marks of
the students staying at that address.
b) Write a stored function using cursors which will calculate total and percentage of each student
create function c22()returns void as '
declare c21 cursor for select rno,count(scode),sum(marks) from ss group by rno;
r int;
s int;
m int;
p float;
begin
open c21;
loop
fetch c21 into r,s,m;
exit when not found;
p=(m*100)/(s*100);
raise notice''Roll Nos :% '',r;
raise notice''Total : %'',m;
raise notice''Percentage:%'',p;
end loop;
close c21;
end '
language 'plpgsql';
CREATE FUNCTION
b) Write a stored function using cursors to find the total number of berths not reserved for all the trains on 18-05-2009.
create function fu4()returns void as '
declare cf2 cursor for select count(pname) from passenger where pid in(select
pid from ticket where date=''2010-05-03'' and status=''w'');
cnt int;
begin
open cf2;
loop
fetch cf2 into cnt;
exit when not found;
raise notice ''Total: %'',cnt;
end loop;
close cf2;
end '
language 'plpgsql';
CREATE FUNCTION
CREATE FUNCTION
b) Write a stored function to increase the loan approved amount for all loans by 20%. In case the initial loan approved
amount was less than Rs 10000, then print a notice to the user, before updating the amount .
SET B
a) Write a stored function to accept project name as input and print the names of employees working on the project. Also
print the total number of employees working on that project. Raise an exception for an invalid project name.
create function SetA_a(varchar ) returns integer as $$
declare
rec record;
cur cursor for select * from project,prog_emp,emp where project.pno=prog_emp.pno and
emp.eno=prog_emp.eno;
cnt integer:=0;
name alias for $1;
begin
open cur;
loop
fetch cur into rec;
exit when not found;
if rec.pname=name then
raise notice '%',rec.ename;
cnt:=cnt+1;
end if;
end loop;
if cnt=0 then
raise exception 'Invalid Project name';
end if;
close cur;
return cnt;
end;
$$ language plpgsql;
CREATE FUNCTION
b) Write a stored function to decrease the Hours_worked by 2 hours, for all projects in which employees from department
no 2 is working. Raise an exception , in case the hours_worked becomes = 0 , after updation.
project4=# create or replace function f3() returns text as'
declare rec record;
n int;
begin
select hours into n from pe where pno in(select pno from project where
pname=''Electronics'');
update pe set hours=hours-2 where pno in(select pno from project where
pname=''Electronics'');
if(n-2=0)then
raise notice''hours are 0'';
end if;
raise notice''number : %'',n;
return'' '';
end'
language 'plpgsql';
CREATE FUNCTION
SET C
Using Bus transport Database
a) Write a stored function to print the names of drivers working on both shifts on ‘20/04/2014’.
b) Write a stored function to accept the bus_no and date and print its allotted drivers. Raise an exception in case of
invalid bus number.
Assignment 5 : Triggers.
SET A
Using Item_supplier Database
Item(itemno integer,Itemname varchar(20),quantity integer) Supplier(SupplierNo,Supplier name,address,city)
Item_sup(item_no integer ,Supplier_no integer,Rate Money)
Item and supplier are related with many to many relationship .Rate is descriptive attribute.
a. Write a trigger before update on rate field, If the difference in the old rate and new rate to be entered is more than Rs
2000/ . Raise an exception and display the corresponding message
b. Write a trigger before insert or update on rate field, If the rate to be entered is zero then. Raise an exception and
display the message “Zero rate not allowed”.
2)
supplier4=# create trigger T11 before insert or update on itemsupplier for each
row execute procedure a2();
CREATE TRIGGER
SET B
Student –Marks database
Student (rollno integer,name varchar(30),address varchar(50),class varchar(10)) Subject(Scode varchar(10),subject
name varchar(20))
student and subject are related with M-M relationship with attributes marks scored.
a) Write a trigger before deleting a student record from the student table. Raise a notice and display the message
“student record is being deleted”
create trigger ass5C_1 before insert or update on stud_sub for each row execute
procedure ass5C_1();
CREATE TRIGGER
create trigger ass5c_2 before delete on student for each row execute procedure
ass5c_2();
b) Write a trigger to ensure that the marks entered for a student, with respect to a subject is never < 10 and greater than
100.
student4=# create or replace function f6() returns trigger as '
begin
if new.marks<10 new.marks="" or="">100 then
raise exception'' Invalid %'',new;
end if;
return new;
end '
language 'plpgsql';
CREATE FUNCTION
student4=# create trigger t4 before insert or update on ss for each row execute
procedure f6();
CREATE TRIGGER
SET C
News paper database
Newspaper(name varchar(20), language varchar(20),Publisher varchar(20),cost money) Cities(pincode varchar(6), city
varchar(20), state varchar(20))
Newspaper & Cities M to M relationship with descriptive attribute daily_required integer
a) Calculate the length of pincode. Write a trigger which will fire before insert on the cities table which check that the
pincode must be of 6 digit. If it is more or less then it display the appropriate message.
1)
create trigger t1 before insert on cities for each row execute procedure t11();
CREATE TRIGGER
b) Write a trigger which will prevent deleting cities from Maharatra state.
2)
SET D
Using Railway Reservation Database
a) create a trigger to validate train arrival time must be less than train departure time.
railway4=# create or replace function t1()returns trigger as '
begin
if new.a_time
raise exception ''correct time %'',new;
end if;
return new;
end '
language 'plpgsql';
CREATE FUNCTION
b) Write a trigger which will be activated before changing the status field in the ticket table and print a message to the
user.
railway4=# create or replace function f1() returns trigger as'
begin
if old.status!=new.status then
raise exception ''Cannot change status %'',old;
end if;
return old;
end
'
language 'plpgsql';
CREATE FUNCTION
railway4=# create trigger t before insert or update on ticket for each row
execute procedure f1();
CREATE TRIGGER
railway4=# update ticket set status='c' where tno=104;
ERROR: Cannot change status (104,44,5,12,5,2010-05-03,4000.00,w,)
railway4=# update ticket set status='w' where tno=104;
UPDATE 1
SET E
Using Bus Transportation database
a) Define a trigger after insert or update the record of driver if the age is between 18 and 50 give the message “valid
entry” otherwise give appropriate message.
buss4=# create or replace function agep() returns trigger as '
begin
if new.d_age<18 new.d_age="" or="">50 then
raise exception''Invalid Age % '',new;
buss4=# create trigger t1 after insert or update on driver for each row execute
procedure agep();
CREATE TRIGGER
b) Define a trigger after delete the record of bus having capacity < 10. Display the message accordingly
2)
uss4=# create or replace function c1()returns trigger as '
begin
if old.capacity>10 then
raise exception ''invalid%'',old;
end if;
return old;
end '
language 'plpgsql';
CREATE FUNCTION
buss4=# create trigger t22 after delete on bus for each row execute procedure
c1();
CREATE TRIGGER
buss4=# insert into bus values(55,8,'abc',101);
INSERT 0 1
buss4=# select * from bus;
bno | capacity | dname | rno
-----+---------+-----------+----
22 | 30 | shivshakti | 102
33 | 50 | shiv | 103
44 | 9 | abc | 101
55 | 8 | abc | 101
(4 rows)