Sunteți pe pagina 1din 68

Assignment No :- 1

=============================================================================

Name :- Datkhile Shubhangi vilas


Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-
Consider the following entities and their relationship.
Customer (c_no, c_name, c_city, c_ph_no)
Ticket (t_no, booking_date, fare, traveling_date)
Relationship between Customer and Ticket is one-to-many.
Constraints: primary key, foreign key,c_name should not be null,fare should be greater than zero.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a procedure to display names of customer who have booked bus on given date.
2) Write a trigger that restricts insertion of ticket having traveling date smaller than booking date.(Raise
user defined exception and give appropriate message)
SQL > create table customer(cno int primary key,cname varchar(15),ccity varchar(15),cphno int);
Table created.

SQL> create table ticket(tno int primary key,bookingdate varchar(10),fare int , traveling_date varchar(10),
cno int)
Table created.
SQL> insert into customer values(101,'Pravin','Pune',5645655);
1 row created.

SQL> select * from customer;

CNO CNAME CCITY CPHNO


---------- --------------- --------------- ----------
1 lokesh daund 5545545
2 roshan hadapsar 855452
3 sashikant pune 2365844
4 pradip mumbai 5652656
5 omkar katraj 6849644
-------------------------------------------------------

SQL> insert into ticket values(101,'5/4/15',255,'12/5/15',1);


1 row created.

SQL> select * from ticket;

TNO BOOKINGDATE FARE TRAVELINGDATE CNO


---------- ---------- ---------- ---------- ----------
101 5/4/15 255 12/5/15 1
102 6/9/13 600 8/12/11 2
103 1/2/07 300 9/4/13 3
104 21/7/15 1000 10/9/15 4
105 25/2/14 254 3/5/14 5
------------------------------------------------------------

SQL>set serveroutput on

SQL> create or replace procedure p1(n in varchar) as cursor c1 is select cname,


bookingdate,travelingdate from customer,ticket where customer.cno=ticket.cno;
2 c c1%rowtype;
3 begin
4 open c1;
5 dbms_output.put_line('cname'||''||' bookingdate'||''||' travelingdate');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 if(c. bookingdate=n) then
10 dbms_output.put_line(c.cname||''||c. bookingdate||''||c. travelingdate);
11 end if;
12 end loop;
13 close c1;
14 end;
15 /
Procedure created.

SQL> begin
2 p1('5/4/15');
3 end;
4 /

cname bookingdate travelingdate


lokesh5/4/1512/5/15

PL/SQL procedure successfully completed.

SQL> set serveroutput on


SQL> create or replace trigger t1 before insert or update on ticket
2 for each row
3 begin
4 if(:new.travelingdate<:new.bookingdate)then
5 raise_application_error(-20001,'travelingdate>bookingdate');
6 end if;
7 end;
8 /

Trigger created.

SQL> insert into ticket values(103,'16/12/11',600,'8/12/11',2);


insert into ticket values(103,'16/12/11',600,'8/12/11',2)
*
ERROR at line 1: ,9632456754
Assignment no:-2

Name :- Datkhile Shubhangi vilas


Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.

Student (s_reg_no, s_name, s_class)


Competition (comp_no, comp_name, comp_type)

Relationship between Student and Competition is many-to-many with descriptive attribute rank and year.

Constraints:primary key, foreign key,primary key for third table(s_reg_no, comp_no, year),s_name and
comp_name should not be null,

comp_type can be sports or academic.

Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:

1) Write a function which will accept s_reg_no of student and returns total number of competition in which
student has participated in a given year.

2) Write a cursor which will display year wise details of competitions held.(Use parameterized cursor)

SQL> create table student1(sregno int primary key,sname varchar(15),sclass varchar(10));

Table created.

SQL> create table competition(compno int primary key,compname varchar(10) not null,comptype
varchar(10));

Table created.

SQL> create table student1competition(sregno int,compno int,rank int,year int);

Table created.

SQL> insert into student1 values(101,'Lokesh','sybca');

1 row created.

SQL> SELECT * FROM STUDENT1;

SREGNO SNAME SCLASS


---------- --------------- ----------
101 Lokesh sybca
102 Roshan sybca
103 Sashikant sybca
104 Ashish sybca
105 Omkar sybca
-----------------------------------------

SQL> insert into competition values(1,'Paint','academic');

1 row created.

SQL> SELECT * FROM competition;

COMPNO COMPNAME COMPTYPE


---------- ---------- ----------
1 Paint academic
2 Foot Ball Sports
3 Quiz academic
4 Running Sports
5 Chess academic
---------------------------------------------
SQL> insert into student1competition values(101,1,2,2013);

1 row created.

SQL> SELECT * FROM student1competition;


SREGNO COMPNO RANK YEAR
---------- ---------- ---------- ----------
101 1 2 2013
102 2 3 2012
103 3 1 2015
104 4 2 2011
105 5 1 2014
----------------------------------------------------

SQL> set serveroutput on

SQL> create or replace function fun1(nocomp in varchar) return number as nofcomp number;
2 begin
3 select count(competition.compno) into nofcomp from student1, competition, student1competition
where student1.sregno=student1competition.sregno and
competition.compno=student1competition.compno and student1.sregno=101;
4 if sql %found then
5 return(nofcomp);
6 else
7 return null;
8 end if;
9 end fun1;
10 /

Function created.

SQL> begin
2 dbms_output.put_line('no of competition-'||fun1(2015));
3 end;
4 /

no of competition-1
PL/SQL procedure successfully completed.

SQL>set serveroutput on
SQL>declare
2 cursor c1(yyyy student1competition.year%type)is select compname,comptype,year from
student1,competition,student1competition where student1.sregno=student1competition.sregno and
competition.compno=student1competition.compno order by year;
3 c c1%rowtype;
4 begin
5 open c1('&yyyy');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 dbms_output.put_line(c.compname||' '||c.comptype||' '||c.year);
10 end loop;
11 close c1;
12 end;
13 /
Enter value for yyyy: 2015
old 5: open c1('&yyyy');
new 5: open c1('2015');

Running Sports 2011


Foot Ball Sports 2012
Paint academic 2013
Chess academic 2014
Quiz academic 2015

PL/SQL procedure successfully completed.


=============================================================================
Assignment no:-3

Name :- Datkhile Shubhangi vilas


Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Owner (o_no, o_name, o_city, o_ph_no)
Estate (e_no, e_type, e_city, e_price)

Relationship between Owner and Estate is one-to-many.


Constraints : primary key, foreign key,o_name should not be null,e_type can be flat, bungalow or land.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:

1) Write a procedure which will accept owner number and display details of all estates of given owner
which belongs to pune city.
2) Write a cursor which will display type wise estate details. (Use parameterized cursor)

SQL> create table owner(ono int primary key,oname varchar(15) not null,ocity varchar(15),ophno int);
Table created.

SQL> create table estate(eno int primary key,etype varchar(15),ecity varchar(15),eprice int,ono int
references owner);
Table created.
SQL> insert into owner values(1,'Lokesh','daund',2549696469);
1 row created.

SQL> Select * from owner;


ONO ONAME OCITY OPHNO
---------- --------------- --------------- ----------
1 Lokesh daund 2549696469
2 roshan hadapsar 855452
3 sashikant pune 2365844

4 pradip mumbai 5652656


5 omkar katraj 6849644
-------------------------------------------------------------
SQL> insert into estate values(101,'flat','pune',65465,1);
1 row created.

SQL> Select * from estate;


ENO ETYPE ECITY EPRICE ONO
----------------------- --------------- --------------- ---------- ----------
101 flat pune 65465 1
102 land mumbai 353552 2
103 flat nagpur 68156652 3
104 bungalow nasik 84152424 4
105 bungalow sangli 6181241 5
------------------------------------------------------------------------
SQL> set serveroutput on
SQL> create or replace procedure p3(n in varchar) as cursor c1 is select etype,ecity,eprice from
owner,estate where owner.ono=estate.ono;
2 c c1%rowtype;
3 begin
4 open c1;
5 dbms_output.put_line('etype'||' '||'ecity'||' '||'eprice');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 if(c.ecity=n) then
10 dbms_output.put_line(c.etype||' '||c.ecity||' '||c.eprice);
11 end if;
12 end loop;
13 close c1;
14 end;
15 /
Procedure created.

SQL> begin
2 p3('pune');
3 end;
4 /

etype ecity eprice


flat pune 65465
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> declare
2 cursor c1(etyp estate.etype%type)is select etype,ecity,eprice from owner,estate where
owner.ono=estate.ono order by etype;
3 c c1%rowtype;
4 begin
5 open c1('&etyp');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 dbms_output.put_line(c.etype||' '||c.ecity||' '||c.eprice);
10 end loop;
11 close c1;
12 end;
13 /

Enter value for etyp: land


old 5: open c1('&etyp');

new 5: open c1('land');


bungalow sangli 6181241
bungalow nasik 84152424
flat pune 65465
flat nagpur 68156652
land mumbai 353552
PL/SQL procedure successfully completed.

=============================================================================
Assignment no:-4
Name :- Datkhile Shubhangi vilas
Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-
Consider the following entities and their relationship.
Bus(bus_no, capacity, source, destination)
Driver(driver_no, driver_name, license_no, addr, age, salary)

Relationship between Bus and Driver is many-to-many with descriptive attribute date_of_duty_allotted
and shift.

Constraints: primary key, foreign key,


primary key for third table (bus_no, driver_no,date_of_duty_allotted),
driver_name should not be null,
shift can be morning or evening.

Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a function which will return name of driver having maximum salary.
2) Write a cursor which will display date wise bus and their driver details.

SQL> create table bus(busno int primary key,capacity int,source varchar(10),destination varchar(10));

Table created.
SQL> create table driver(dno int primary key,dname varchar(10) not null,licenceno int,addr
varchar(10),age int,salary int);
Table created.

SQL> create table busdriver(busno int,dno int, dateofdutyallotted varchar(10), shift varchar(15));
Table created.

SQL> insert into bus values(101,14,'delhi','daund');


1 row created.

SQL> select * from bus;

BUSNO CAPACITY SOURCE DESTINATIO


---------- ---------- ---------- ----------
101 14 delhi daund
102 27 daund delhi
103 20 daund pune
104 15 nasik mumbai
105 30 hadapsar katraj
---------------------------------------------

SQL>insert into driver values(1,'ram',6464,'pune',25,4969);


1 row created.

SQL> select * from driver;


DNO DNAME LICENCENO ADDR AGE SALARY
---------- ---------- ---------- ---------- ---------- ----------
1 ram 6464 pune 25 4969
2 sam 5454 nasik 30 5535
3 shasi 4452 pune 21 5545
4 omkar 5554 jalgoan 35 6000
5 shubham 5448 nagpur 26 4444
----------------------------------------------------------------------

SQL> insert into busdriver values(101,1,'25/2/15','morning');


1 row created.
SQL> select * from busdriver;
BUSNO DNO DATEOFDUTY SHIFT
---------- ---------- ---------- ---------------
101 1 25/2/15 morning
102 2 21/7/15 morning
103 3 9/4/13 evening
104 4 6/9/13 evening
105 5 12/5/15 morning
----------------------------------------------
SQL> Set serveroutput on
SQL> Create or replace function fun(dn in varchar) return varchar as dnm varchar(10);
2 Begin
3 Select (dname) into dnm from driver where salary=(select max(salary) from driver);
4 If sql %found then
5 Return(dnm);
6 Else
7 Return null;
8 End if;
9 End;
10 /

Function created.

SQL> begin
2 dbms_output.put_line('name-'||fun('dname'));
3 end;
4 /

name-omkar
PL/SQL procedure successfully completed.
SQL> Set serveroutput on
SQL> Declare
2 Cursor c1 is select capacity,source,destination,dname,licenceno,age,salary from bus, driver, busdriver
where bus.busno=busdriver.busno and driver.dno=busdriver.dno;
3 C c1%rowtype;
4 Begin
5 Open c1;
6 Loop
7 Fetch c1 into c;
8 Exit when c1%notfound;
9 Dbms_output.put_line(c.capacity||' '||c.source||' '||c.destination||' '||c.dname||' '||c.licenceno||' '||
c.age||' '||c.salary);
10 End loop;
11 Close c1;
12 End;
13 /
14 delhi daund ram 6464 25 4969
27 daund delhi sam 5454 30 5535
20 daund pune shasi 4452 21 5545
15 nasik mumbai omkar 5554 35 6000
30 hadapsar katraj shubham 5448 26 4444
PL/SQL procedure successfully completed.
=============================================================================
Assignment no:-5
Name :- Datkhile Shubhangi vilas
Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Drug(d_no, d_name, company, price)
Medical _store(m_no, m_name, m_city, ph_no)
Relationship between Drug and Medical_Store is many-to-many with descriptive attribute quantity.
Constraints:primary key, foreign key,m_name and d_name should not be null,m_city can be pune or
pimpri.

Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a package, which consists of one procedure and one function. Pass drug number as a parameter
to procedure and display details of that drug. Pass city as a parameter to a function and return total
number of medical_store in given city.
2) Write a trigger that restricts insertion and updation of drug having price less than zero. (Raise user
defined exception and give appropriate message)
SQL> create table drug(dno int primary key,dname varchar(15) not null,company varchar(15),price int);
Table created.
SQL> create table medicalstore(mno int primary key,mname varchar(15) not null,mcity varchar(15),phno
int);
Table created.
SQL> create table drugmedicalstore(dno int,mno int,quantity int);
Table created.
SQL> insert into drug values(101,'insepton','malform',6545);
1 row created.
SQL> Select * from drug;
DNO DNAME COMPANY PRICE
---------- --------------- --------------- ----------
101 insepton malform 6545
102 cojka sector 1895
103 acen mankind 3652
104 syndron sector 5452
105 santin mankind 9824
------------------------------------------------------------

SQL> insert into medicalstore values(1,'new strom','daund',2549696469);


1 row created.
SQL> insert into medicalstore values(2,'matrix','pune',459632581);
1 row created.
SQL> insert into medicalstore values(3,'nitro','nasik',2584);
1 row created.
SQL> insert into medicalstore values(4,'cronicle','nagpur',54862);
1 row created.
SQL> insert into medicalstore values(5,'mediplus','mumbai',85487);
1 row created.
SQL> Select * from medicalstore;
MNO MNAME MCITY PHNO
--------- --------------- --------------- ----------
1 new strom daund 2549696469
2 matrix pune 459632581
3 nitro nasik 2584
4 cronicle nagpur 54862
5 mediplus mumbai 85487
------------------------------------------------------------
SQL> insert into drugmedicalstore values(101,1,250);
1 row created.
SQL> insert into drugmedicalstore values(102,2,544);
1 row created.
SQL> insert into drugmedicalstore values(103,3,800);
1 row created.
SQL> insert into drugmedicalstore values(104,4,600);
1 row created.
SQL> insert into drugmedicalstore values(105,5,620);
1 row created.
SQL> Select * from drugmedicalstore;
DNO MNO QUANTITY
---------- ---------- ----------
101 1 250
102 2 544
103 3 800
104 4 600

105 5 620
--------------------------------------
SQL> Set serveroutput on
SQL> Create or replace trigger t5 before insert or update on drug
2 For each row
3 Begin
4 If(:new.price<=0) then
5 Raise_application_error(-20001,'price>0');
6 End if;
7 End;
8 /

Trigger created.

SQL> insert into drug values(106,'nutri','dabur',0);


insert into drug values(106,'nutri','dabur',0)
*
ERROR at line 1:
ORA-20001: price>0
ORA-06512: at "SCOTT.T5", line 3
ORA-04088: error during execution of trigger 'SCOTT.T5'

================================================================================
Assignment no :-6
Name :- Datkhile Shubhangi vilas
Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Train(t_no, t_name)
Passenger (p_no, p_name, addr, age)
Relationship between Train and Passenger is many-to-many with descriptive attribute date, seat_no and
amt.
Constraints : primary key, foreign key,
primary key for third table (t_no, p_no, date),
t_name and p_name should not be null,
amt should be greater than zero.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a function which will display train details having maximum passenger for a given date.
2) Write a cursor which will display date wise train and their passenger details.
SQL> create table train(tno int primary key,tname varchar(15) not null,noofpassenger int);
Table created.
SQL> create table passenger(pno int primary key,pname varchar(15) not null,addr varchar(15),age int);
Table created.
SQL> create table trainpassenger(tno int,pno int,tpdate varchar(15), seatno int,amt int check(amt>0));
Table created.
SQL> insert into train values(101,'garib rath',2);
1 row created.
SQL> insert into train values(102,'shatabdhi',1);
1 row created.
SQL> insert into train values(103,'deccan',1);
1 row created.
SQL> insert into train values(104,'konark',1);
1 row created.
SQL> insert into train values(105,'duranto',1);
1 row created.
SQL> select * from train;
TNO TNAME NOOFPASSENGER
---------- --------------- -------------
101 garib rath 2
102 shatabdhi 1
103 deccan 1
104 konark 1
105 duranto 1
------------------------------------------

SQL> insert into passenger values(1,'lokesh','daund',18);

1 row created.
SQL> insert into passenger values(2,'harsh','delhi',18);
1 row created.
SQL> insert into passenger values(3,'vishal','daund',20);
1 row created.
SQL> insert into passenger values(4,'roshan','hadapsar',19);
1 row created.
SQL> insert into passenger values(5,'shashikant','pune',19);
1 row created.
SQL> insert into passenger values(6,'akash','daund',19);
1 row created.
SQL> select * from passenger;
PNO PNAME ADDR AGE
---------- --------------- --------------- ----------
1 lokesh daund 18
2 harsh delhi 18
3 vishal daund 20
4 roshan hadapsar 19
5 shashikant pune 19
6 akash daund 19
----------------------------------------------------
SQL> insert into trainpassenger values(101,1,'12/5/15',14,700);
1 row created.
SQL> insert into trainpassenger values(101,2,'12/5/15',15,700);
1 row created.
SQL> insert into trainpassenger values(102,3,'8/12/11',17,625);
1 row created.
SQL> insert into trainpassenger values(103,4,'10/9/15',31,500);
1 row created.
SQL> insert into trainpassenger values(104,5,'9/4/13',24,250);
1 row created.
SQL> insert into trainpassenger values(105,6,'3/5/14',45,300);
1 row created.
SQL> select * from trainpassenger;
TNO PNO TPDATE SEATNO AMT
---------- ---------- ------------ ---------- ----------
101 1 12/5/15 14 700
101 2 12/5/15 15 700
102 3 8/12/11 17 625
103 4 10/9/15 31 500
104 5 9/4/13 24 250
105 6 3/5/14 45 300
--------------------------------------------------------------
SQL> set serveroutput on
SQL> create or replace function fun1(pn in varchar) return varchar as maxpass varchar(15);
2 begin
3 select max(noofpassenger) into maxpass from train;
4 if sql %found then
5 return(maxpass);
6 else
7 return null;
8 end if;
9 end ;
10 /
Function created.
SQL> begin
2 dbms_output.put_line('no of competition-'||fun1('12/5/15'));
3 end;
4 /
no of competition-2
PL/SQL procedure successfully completed.

SQL> set serveroutput on


SQL> declare
2 cursor c2 is select tname,pname,addr,age,trainpassenger.tpdate from train, passenger,
trainpassenger where train.tno=trainpassenger.tno and passenger.pno=trainpassenger.pno order by
trainpassenger.tpdate;
3 c c2%rowtype;
4 begin
5 open c2;
6 loop
7 fetch c2 into c;
8 exit when c2%notfound;
9 dbms_output.put_line(c.tpdate||' '||c.tname||' '||c.pname||' '||c.addr||' '||c.age);
10 end loop;
11 close c2;
12 end;
13 /
10/9/15 deccan roshan hadapsar 19
12/5/15 garib rath lokesh daund 18
12/5/15 garib rath harsh delhi 18
3/5/14 duranto akash daund 19
8/12/11 shatabdhi vishal daund 20
9/4/13 konark shashikant pune 19
PL/SQL procedure successfully completed.

================================================================================
Assignment no :-7

Name :- Datkhile Shubhangi vilas


Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Route(route_no, source, destination, no_of_station)
Bus (bus_no, capacity, depot_name)
Relationship between Route and Bus is one-to-many
Constraints: primary key, foreign key,
depot_name should not be null,
bus capacity should be greater than 40.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a procedure which will display all bus details for a given route.
2) Write a trigger that restricts insertion of route having number of station less than zero. (Raise user
defined exception and give appropriate message)
SQL> create table route(rno int primary key,source varchar(10),destination varchar(10),noofstation int);
Table created.
SQL> create table bus2(bno int primary key,capacity int,depotname varchar(15),rno int);
Table created.
SQL> insert into route values(101,'delhi','daund',14);
1 row created.
SQL> insert into route values(102,'daund','delhi',14);
1 row created.
SQL> insert into route values(103,'daund','pune',4);
1 row created.
SQL> insert into route values(104,'nasik','mumbai',7);
1 row created.
SQL> insert into route values(105,'hadapsar','katraj',3);
1 row created.
SQL> select * from route;
RNO SOURCE DESTINATIO NOOFSTATION
---------- ---------- ---------- -----------
101 delhi daund 14
103 daund pune 4
104 nasik mumbai 7
105 hadapsar katraj 3
102 daund delhi 14
--------------------------------------------------
SQL> insert into bus2 values(1,64,'pune',101);
1 row created.
SQL> insert into bus2 values(2,54,'nasik',102);
1 row created.
SQL> insert into bus2 values(3,52,'pune',103);
1 row created.
SQL> insert into bus2 values(4,56,'jalgoan',104);
1 row created.
SQL> insert into bus2 values(5,48,'nagpur',105);
1 row created.
SQL> select * from bus2;
BNO CAPACITY DEPOTNAME RNO
---------- ---------- --------------- ----------
1 64 pune 101
2 54 nasik 102
3 52 pune 103
4 56 jalgoan 104
5 48 nagpur 105
-----------------------------------------------
SQL> set serveroutput on
SQL> create or replace procedure p1(n in varchar) as cursor c1 is select capacity, depotname, route.rno
from route, bus2 where route.rno=bus2.rno;
2 c c1%rowtype;
3 begin
4 open c1;
5 dbms_output.put_line('capacity'||' '||'depotname');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 if(c.rno=n) then
10 dbms_output.put_line(c. capacity||' '||c.depotname);
11 end if;
12 end loop;
13 close c1;
14 end;
15 /
Procedure created.
SQL> begin
2 p1('102');
3 end;
4 /
capacity depotname
54 nasik
PL/SQL procedure successfully completed.

SQL> set serveroutput on


SQL> create or replace trigger tt1 before insert or update on route
2 for each row
3 begin
4 if(:new.noofstation<=0)then
5 raise_application_error(-20001,'number of station should be greater than zero.');
6 end if;
7 end;
8 /
Trigger created.
SQL> insert into route values(106,'nasik','mumbai',0);
insert into route values(106,'nasik','mumbai',0)
*
ERROR at line 1:
ORA-20001: number of station should be greater than zero.
ORA-06512: at "SCOTT.TT1", line 3
ORA-04088: error during execution of trigger 'SCOTT.TT1'

================================================================================
Assignment no :-8

Name :- Datkhile Shubhangi vilas


Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


University (u_no, u_name, u_city)
College (c_no, c_name, c_city, year_of_establishment )
Relationship between University and College is one-to-many
Constraints: primary key, foreign key,
u_name and c_name should not be null.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a package, which consists of one procedure and one function. Pass university number as a
parameter to procedure and display details of that university. Pass city as a parameter to a function and
return total number of colleges in given city.
2) Write a cursor which will display university wise their college details. (Use parameterized cursor)

SQL> create table university(uno int primary key,uname varchar(20) not null,ucity varchar(15));
Table created.
SQL> create table college(cno int primary key,cname varchar(20) not null,ccity varchar(15),yearofest
int,uno int);
Table created.
SQL> insert into university values(101,'pune university','pune');
1 row created.
SQL> insert into university values(102,'delhi university ','delhi');
1 row created.
SQL> insert into university values(103,'mumbai university ','mumbai');
1 row created.
SQL> insert into university values(104,'nagpur university ','nagpur');
1 row created.
SQL> insert into university values(105,'sangli university ','sangli');
1 row created.
SQL> select * from university;
UNO UNAME UCITY
---------- -------------------- ---------------
101 pune university pune
102 delhi university delhi
103 mumbai university mumbai
104 nagpur university nagpur
105 sangli university sangli
-------------------------------------------------
SQL> insert into college values(1,'pune university','pune',1997,101);
1 row created.
SQL> insert into college values(2,'delhi university ','delhi',1997,102);
1 row created.
SQL> insert into college values(3,'mumbai university ','mumbai',1991,103);
1 row created.
SQL> insert into college values(4,'nagpur university ','nagpur',1968,104);
1 row created.
SQL> insert into college values(5,'sangli university ','sangli',1987,105);
1 row created.
SQL> select * from college;
CNO CNAME CCITY YEAROFEST UNO
---------- -------------------- --------------- ---------- ----------
1 pune university pune 1997 101
2 delhi university delhi 1997 102
3 mumbai university mumbai 1991 103
4 nagpur university nagpur 1968 104
5 sangli university sangli 1987 105
---------------------------------------------------------------------------
SQL> set serveroutput on
SQL> declare
2 cursor c2(unm university.uno%type) is select uname,cname,ccity,yearofest from university,college
where university.uno=college.uno and university.uno=102;
3 c c2%rowtype;
4 begin
5 open c2('&unm');
6 loop

7 fetch c2 into c;
8 exit when c2%notfound;
9 dbms_output.put_line(c.uname||''||c.cname||''||c.ccity||''||c.yearofest);
10 end loop;
11 close c2;
12 end;
13 /

Enter value for unm: 102


old 5: open c2('&unm');
new 5: open c2('102');
delhi university delhi university delhi1997
PL/SQL procedure successfully completed.

================================================================================
Assignment no :-9

Name :- Datkhile Shubhangi vilas


Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Patient (p_no, p_name, p_addr)
Doctor (d_no, d_name, d_addr, city)
Relationship between Patient and Doctor is many-to-many with descriptive attribute disease and
no_of_visits.
Constraints: primary key, foreign key,
primary key for third table(p_no, d_no, disease),
p_name and d_name should not be null.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a procedure which will display patient detail who has visited more than 3 times to the given
doctor for ‘Diabetes’.
2) Write a trigger which will restrict insertion or updation of doctor_patient details having no_of_visits
less than zero. (Raise user defined exception and give appropriate message)
SQL> create table patient(pno int primary key,pname varchar(15) not null,paddr varchar(15));
Table created.
SQL> create table doctor(dno int primary key,dname varchar(15) not null,daddr varchar(15),city
varchar(15));
Table created.
SQL> create table patientdoctor(pno int,dno int,disease varchar(15),noofvisits int);
Table created.
SQL> insert into patient values(101,'lokesh','daund');
1 row created.
SQL> insert into patient values(102,'harsh','delhi');
1 row created.
SQL> insert into patient values(103,'vishal','daund');
1 row created.
SQL> insert into patient values(104,'roshan','hadapsar');
1 row created.
SQL> insert into patient values(105,'shashikant','pune');
1 row created.
SQL> select * from patient;
PNO PNAME PADDR
---------- --------------- ---------------
101 lokesh daund
102 harsh delhi
103 vishal daund
104 roshan hadapsar
105 shashikant pune
---------------------------------------------
SQL> insert into doctor values(1,'lokesh','mordern','daund');
1 row created.
SQL> insert into doctor values(2,'harsh','modern','delhi');
1 row created.
SQL> insert into doctor values(3,'vishal','gopalwadi','daund');
1 row created.
SQL> insert into doctor values(4,'roshan','magarpatta','hadapsar');
1 row created.
SQL> insert into doctor values(5,'shashikant','dapodi','pune');
1 row created.
SQL> select * from doctor;
DNO DNAME DADDR CITY
---------- --------------- --------------- ---------------
1 lokesh mordern daund
2 harsh modern delhi
3 vishal gopalwadi daund
4 roshan magarpatta hadapsar
5 shashikant dapodi pune
---------------------------------------------------------
SQL> insert into patientdoctor values(101,1,'headache',4);
1 row created.
SQL> insert into patientdoctor values(102,2,'headache',4);
1 row created.
SQL> insert into patientdoctor values(103,3,'diabetes',7);
1 row created.
SQL> insert into patientdoctor values(104,4,'cold',3);
1 row created.
SQL> insert into patientdoctor values(105,5,'fever',5);
1 row created.
SQL> select * from patientdoctor;
PNO DNO DISEASE NOOFVISITS
---------- ---------- --------------- ----------
101 1 headache 4
102 2 headache 4
103 3 diabetes 7
104 4 cold 3
105 5 fever 5
--------------------------------------------------
SQL> set serveroutput on
SQL> create or replace procedure p2(n in varchar) as cursor c1 is select pname,paddr,disease from
patient,doctor, patientdoctor where patient.pno=patientdoctor.pno and doctor.dno=patientdoctor.dno and
noofvisits>3;
2 c c1%rowtype;
3 begin
4 open c1;
5 dbms_output.put_line('pname'||' '||'paddr');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 if(c.disease =n) then
10 dbms_output.put_line(c.pname||' '||c.paddr);
11 end if;
12 end loop;
13 close c1;
14 end;
15 /
Procedure created.
SQL> begin
2 p2('diabetes');
3 end;
4 /
pname paddr
vishal daund
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> create or replace trigger t1 before insert or update on patientdoctor
2 for each row
3 begin
4 if(:new.noofvisits<=0)then
5 raise_application_error(-20001,'number of visits should be greater than zero.');
6 end if;
7 end;
8 /
Trigger created.
SQL> insert into patientdoctor values(106,6,'cancer',0);
insert into patientdoctor values(106,6,'cancer',0)
*
ERROR at line 1:
ORA-20001: number of visits should be greater than zero.
ORA-06512: at "SCOTT.T1", line 3
ORA-04088: error during execution of trigger 'SCOTT.T1'
================================================================================
Assignment no :-10

Name :- Datkhile Shubhangi vilas


Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Crop (c_no, c_name, c_season, pesticides)
Farmer (f_no, f_name, f_location)
Relationship between Crop and Farmer is many-to-many with descriptive attribute year.
Constraints: primary key, foreign key,
primary key for third table(c_no, f_no, year),
c_name and f_name should not be null,
c_season can be rabi or kharif.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a function which will return total number of farmers harvesting given crop in a given year.
2) Write a cursor which will display season wise information of crops harvested by the farmers. (Use
parameterized cursor)
SQL> create table crop(cno int primary key,cname varchar(15) not null,season varchar(15),pesticides
varchar(15));
Table created.
SQL> create table farmer(fno int primary key,fname varchar(15) not null,location varchar(15));
Table created.
SQL> create table cropfarmer(cno int, fno int,year int);
Table created.
SQL> insert into crop values(101,'sugarcane','rabi','abc');
1 row created.
SQL> insert into crop values(102,'jowar','kharif','jkl');
1 row created.
SQL> insert into crop values(103,'rice','kharif','mno');
1 row created.
SQL> insert into crop values(104,' potato','rabi','pqr');
1 row created.
SQL> insert into crop values(10,'grounut','kharif','xyz');
1 row created.
SQL> select * from crop;
CNO CNAME SEASON PESTICIDES
---------- --------------- --------------- ---------------
101 sugarcane rabi abc
102 jowar kharif jkl
103 rice kharif mno
104 potato rabi pqr
105 grounut kharif xyz
------------------------------------------------------
SQL> insert into farmer values(1,'lokesh','ashok nagar');
1 row created.
SQL> insert into farmer values(2,'harsh','modern');
1 row created.
SQL> insert into farmer values(3,'vishal','gopalwadi');
1 row created.
SQL> insert into farmer values(4,'roshan','magarpatta');
1 row created.
SQL> insert into farmer values(5,'shashikant','dapodi');
1 row created.
SQL> select * from farmer;
FNO FNAME LOCATION
---------- --------------- ---------------
1 lokesh ashok nagar
2 harsh modern
3 vishal gopalwadi
4 roshan magarpatta
5 shashikant dapodi
-------------------------------------------
SQL> insert into cropfarmer values(101,1,2015);
1 row created.
SQL> insert into cropfarmer values(102,2,2015);
1 row created.
SQL> insert into cropfarmer values(103,3,2013);
1 row created.
SQL> insert into cropfarmer values(104,4,2012);
1 row created.
SQL> insert into cropfarmer values(105,5,2014);
1 row created.
SQL> select * from cropfarmer;
CNO FNO YEAR
---------- ---------- ----------
101 1 2015
102 2 2015
103 3 2013
104 4 2012
105 5 2014
----------------------------------
SQL> Set serveroutput on
SQL> Create or replace function fun(nof in varchar) return number as tf number;
2 Begin
3 Select count(fname) into tf from crop, farmer, cropfarmer where crop.cno=cropfarmer.cno and
farmer.fno=cropfarmer.fno and cname='jowar';
4 If sql %found then
5 Return(tf);
6 Else
7 Return null;
8 End if;
9 End;
10 /
Function created.
SQL> begin
2 dbms_output.put_line('total no of farmer-'||fun('2015'));
3 end;
4 /
total no of farmer-1
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> declare
2 cursor c1(swise crop.season%type)is select season,cname,pesticides,fname from
crop,farmer,cropfarmer where crop.cno=cropfarmer.cno and farmer.fno=cropfarmer.fno order by season;
3 c c1%rowtype;
4 begin
5 open c1('&swise');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 dbms_output.put_line(c.season||' '||c.cname||' '||c.pesticides||' '||c.fname);
10 end loop;
11 close c1;
12 end;
13 /

Enter value for swise: rabi


old 5: open c1('&swise');
new 5: open c1('rabi');
kharif jowar jkl harsh
kharif rice mno vishal
rabi potato pqr roshan
rabi sugarcane abc lokesh
PL/SQL procedure successfully completed.

================================================================================
Assignment no :-11

Name :- Datkhile Shubhangi vilas


Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Researcher (r_no, r_name, r_city)
Research_Paper (rp_no, rp_title, rp_subject, rp_level)
Relationship between Researcher and Research_Paper is many-to-many with descriptive attribute year.
Constraints: primary key, foreign key,
r_name and rp_title should not be null,
rp_subject can be computer, electronics or finance.
rp_level can be state, national or international.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a procedure which will display details of research paper of a given subject for a specified year.
2) Write a trigger before insert or update of each row of research_paper published after 2010 be
entered into table. (Raise user defined exception and give appropriate message)

SQL> create table researcher(rno int primary key,rname varchar(15) not null,rcity varchar(15));
Table created.
SQL> create table researchpaper(rpno int primary key,rptitle varchar(20) not null,rpsubject varchar(15),
rplevel varchar(15));
Table created.
SQL> create table researcherpaper(rno int, rpno int,year int);
Table created.
SQL> insert into researcher values(101,'lokesh','daund');
1 row created.
SQL> insert into researcher values(102,'harsh','delhi');
1 row created.
SQL> insert into researcher values(103,'vishal','daund');
1 row created.
SQL> insert into researcher values(104,'roshan','mumbai');
1 row created.
SQL> insert into researcher values(105,'shashikant','pune');
1 row created.
SQL> select * from researcher;
RNO RNAME RCITY
---------- --------------- ---------------
101 lokesh daund
102 harsh delhi
103 vishal daund
104 roshan mumbai
105 shashikant pune
--------------------------------------------

SQL> insert into researchpaper values(1,'hardware','computer','international');


1 row created.
SQL> insert into researchpaper values(2,'software','computer','international');
1 row created.
SQL> insert into researchpaper values(3,'LCD','electronics','national');
1 row created.
SQL> insert into researchpaper values(4,'shares','finance','national');
1 row created.
SQL> insert into researchpaper values(5,'TV','electronics','state');
1 row created.
SQL> select * from researchpaper;
RPNO RPTITLE RPSUBJECT RPLEVEL
---------- -------------------- --------------- ---------------
1 hardware computer international
2 software computer international
3 LCD electronics national
4 shares finance national
5 TV electronics state
-----------------------------------------------------------------
SQL> insert into researcherpaper values(101,1,2015);
1 row created.
SQL> insert into researcherpaper values(102,2,2015);
1 row created.
SQL> insert into researcherpaper values(103,3,2011);
1 row created.
SQL> insert into researcherpaper values(104,4,2012);
1 row created.
SQL> insert into researcherpaper values(105,5,2013);
1 row created.
SQL> select * from researcherpaper;
RNO RPNO YEAR
---------- ---------- ----------
101 1 2015
102 2 2015
103 3 2011
104 4 2012
105 5 2013
-----------------------------------
SQL> set serveroutput on
SQL> create or replace procedure p1(n in varchar) as cursor c1 is select rptitle,rpsubject,rplevel from
researchpaper,researcherpaper where researchpaper.rpno=researcherpaper.rpno and year=2015;
2 c c1%rowtype;
3 begin
4 open c1;
5 dbms_output.put_line('rptitle'||' '||'rpsubject'||' '||'rplevel');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 if(c.rpsubject=n) then
10 dbms_output.put_line(c.rptitle||' '||c. rpsubject ||' '||c.rplevel);
11 end if;
12 end loop;
13 close c1;
14 end;
15 /
Procedure created.
SQL> begin
2 p1('computer');
3 end;
4 /
rptitle rpsubject rplevel
hardware computer international
software computer international
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> create or replace trigger t1 before insert or update on researcherpaper
2 for each row
3 begin
4 if(:new.year>2010)then
5 raise_application_error(-20001,'publish must be before 2010');
6 end if;
7 end;
8 /
Trigger created.
SQL> insert into researcherpaper values(106,6,2013);
insert into researcherpaper values(106,6,2013)
*
ERROR at line 1:
ORA-20001: publish must be before 2010
ORA-06512: at "SCOTT.T1", line 3
ORA-04088: error during execution of trigger 'SCOTT.T1'

================================================================================
Assignment no :-12

Name :- Datkhile Shubhangi vilas


Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Donor (donor_no, donor_name, city)
Blood_donation_detail (bd_no, blood_group, qty, date_of_collection)
Relationship between Donor and Blood_donation_detail is one-to-many.
Constraints: primary key, foreign key,
donor_name should not be null,
blood_group can be A+, A-, B+, B-, AB+, AB-, O+, O-
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a function which will count total amount of blood collected for a given blood group on given
date.
2) Write a cursor which will display donor wise blood donation details. (Use parameterized cursor)
SQL> create table donor(dno int primary key,dname varchar(15) not null,city varchar(15));
Table created.
SQL> create table blooddonation(bno int primary key,bloodgroup varchar(10),qty int,dateofcoll
varchar(15),dno int);
Table created.
SQL> insert into donor values(101,'lokesh','daund');
1 row created.
SQL> select * from donor;
DNO DNAME CITY
---------- --------------- ------------
101 lokesh daund
102 harsh delhi
103 vishal daund
104 roshan hadapsar
105 shashikant pune
-----------------------------------------
SQL> insert into blooddonation values(1,'b+',1,'14/4/2015',101);
1 row created.

SQL> select * from blooddonation;


BNO BLOODGROUP QTY DATEOFCOLL DNO
---------- ---------- ---------- --------------- ----------
1 b+ 1 14/4/2015 101

2 b+ 1 14/4/2015 102

3 o+ 2 25/9/2013 103

4 a- 3 11/3/2012 104

5 ab+ 3 31/11/2014 105

-----------------------------------------------------------------

SQL> set serveroutput on


SQL> create or replace function fun1(tbl in varchar) return number as tb number;
2 begin
3 select sum(qty) into tb from donor, blooddonation where donor.dno=blooddonation.dno and
bloodgroup='b+';
4 if sql %found then
5 return(tb);
6 else
7 return null;
8 end if;
9 end fun1;
10 /
Function created.
SQL> begin
2 dbms_output.put_line('total blood-'||fun1('14/4/2015'));
3 end;
4 /
total blood-2
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> declare
2 cursor c1(dwise donor.dname%type)is select dname, bloodgroup, qty, dateofcoll from
donor,blooddonation where donor.dno=blooddonation.dno order by dname;
3 c c1%rowtype;
4 begin
5 open c1('&dwise');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 dbms_output.put_line(c.dname||' '||c.bloodgroup||' '||c.qty||' '||c.dateofcoll);
10 end loop;
11 close c1;
12 end;
13 /

Enter value for dwise: harsh


old 5: open c1('&dwise');
new 5: open c1('harsh');
harsh b+ 1 14/4/2015
lokesh b+ 1 14/4/2015
roshan a- 3 11/3/2012
shashikant ab+ 3 31/11/2014
vishal o+ 2 25/9/2013
PL/SQL procedure successfully completed.

================================================================================
Assignment no :-13
Name :- Datkhile Shubhangi vilas
Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Client (c_no, c_name, c_addr, birth_date)
Policy_info (p_no, p_name, maturity_amt, prem_amt, policy_term)
Relationship between Client and Policy_info is many-to-many with descriptive attribute date_of_purchase.
Constraints: primary key, foreign key,
c_name and p_name should not be null,
policy_term should be greater than zero.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a procedure which will display all policy details of given client for a given year.
2) Write a trigger which restricts insertion of policy_info having maturity amount less than premium
amount. (Raise user defined exception and give appropriate message)
SQL> create table client(cno int primary key,cname varchar(15) not null,addr varchar(15),birthdate
varchar(15));
Table created.
SQL> create table policyinfo(pno int primary key,pname varchar(15) not null,maturityamt int,premamt
int,policyterm int);
Table created.
SQL> create table clientpolicy(cno int, pno int,dateofpurchase varchar(15),year int);
Table created.
SQL> insert into client values(101,'lokesh','daund','27/7/97');
1 row created.
SQL> select * from client;
CNO CNAME ADDR BIRTHDATE
---------- --------------- --------------- ---------------
101 lokesh daund 27/7/97
102 harsh delhi 14/4/97
103 vishal daund 15/9/95
104 roshan hadapsar 21/6/96
105 shashikant pune 30/5/97
----------------------------------------------------------
SQL> insert into policyinfo values(1,'abc',60000,55000,5);

1 row created.
SQL> select * from policyinfo;
PNO PNAME MATURITYAMT PREMAMT POLICYTERM
---------- --------------- ----------- ---------- ----------
1 abc 60000 55000 5
2 jkl 50000 30000 4
3 mno 80000 49000 9
4 pqr 20000 25000 6
5 xyz 40000 45000 7
----------------------------------------------------------------
SQL> insert into clientpolicy values(101,1,'12/5/15',2015);
1 row created.
SQL> select * from clientpolicy;
CNO PNO DATEOFPURCHASE YEAR
---------- ---------- --------------- ----------
101 1 12/5/15 2015
102 2 8/12/11 2011
103 3 9/4/13 2013
104 4 10/9/15 2015
105 5 3/5/14 2014
------------------------------------------------------
SQL> set serveroutput on
SQL> create or replace procedure p2(n in varchar) as cursor c1 is select cname, pname, maturityamt,
premamt, policyterm, year from client, policyinfo, clientpolicy where client.cno=clientpolicy.cno and
policyinfo.pno=clientpolicy.pno and cname='harsh';
2 c c1%rowtype;
3 begin
4 open c1;
5 dbms_output.put_line('cname'||' '||'pname'||' '||'maturityamt '||''||'premamt '||''||'policyterm');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 if(c.year=n) then
10 dbms_output.put_line(c.cname||' '||c.pname||' '||c.maturityamt||' '||c.premamt||' '||c.policyterm);
11 end if;
12 end loop;
13 close c1;
14 end;
15 /
Procedure created.
SQL> begin
2
3 end;
4 /
cname pname maturityamt premamt policyterm
harsh jkl 50000 30000 4
PL/SQL procedure successfully completed.

SQL> set serveroutput on


SQL> create or replace trigger t1 before insert or update on policyinfo
2 for each row
3 begin
4 if(:new.maturityamt<:new.premamt)then
5 raise_application_error(-20001,'maturity amount should be greater than premium amount ');
6 end if;
7 end;
8 /
Trigger created.
SQL> insert into policyinfo values(6,'stu',20000,25000,5);
insert into policyinfo values(6,'stu',20000,25000,5)
*
ERROR at line 1:
ORA-20001: maturity amount should be greater than premium amount
ORA-06512: at "SCOTT.T1", line 3
ORA-04088: error during execution of trigger 'SCOTT.T1'

================================================================================
Assignment no :-14
Name :- Datkhile Shubhangi vilas
Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Company (c_no, c_name, c_city, c_share_value)
Person (p_no, p_name, p_city, p_ph_no)
Relationship between Company and Person is many-to-many with descriptive attribute no_of_shares.
Constraints: primary key, foreign key,
c_name and p_name should not be null,
no_of_shares should be greater than zero.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a function which will return name of person having maximum number of shares of given
company.
2) Write a cursor which will display person wise share details. (Use parameterized cursor)
SQL> create table company(cno int primary key, cname varchar(15) not null, ccity varchar(15),
sharevalue int);
Table created.
SQL> create table person(pno int primary key, pname varchar(15) not null, pcity varchar(15), pphno int);
Table created.
SQL> create table companyperson (cno int, pno int, noofshares int);
Table created.
SQL> insert into company values(101,'tcs','daund',4545545);
1 row created.
SQL> select * from company;
CNO CNAME CCITY SHAREVALUE
---------- --------------- --------------- ----------
101 tcs daund 4545545
102 microsoft hadapsar 855452
103 wipro pune 2365844
104 sys tech mumbai 5652656
105 ibm katraj 6849644
106 syspro mumbai 652656
----------------------------------------------------------
SQL> insert into person values(1,'lokesh','daund',4545545);
1 row created.
SQL> select * from person;
PNO PNAME PCITY PPHNO
---------- --------------- --------------- ----------
1 lokesh daund 4545545
2 harsh delhi 6849644
3 roshan hadapsar 855452
4 sashikant pune 2365844
5 pradip mumbai 5652656
6 harsh delhi 6849644
SQL> insert into companyperson values(101,1,14);
1 row created.
SQL> select * from companyperson;
CNO PNO NOOFSHARES
---------- ---------- ----------
101 1 14
102 2 14
103 3 8
104 4 6
105 5 7
106 6 27
----------------------------------
SQL> set serveroutput on
SQL> create or replace function fun1(pn in varchar) return varchar as maxshr varchar(15);
2 begin
3 select (pname) into maxshr from company, person, companyperson where
Company.cno=companyperson.cno and person.pno=companyperson.pno and noofshares=(select
max(noofshares) from person, companyperson where person.pno=companyperson.pno) group by
pname;
4 if sql %found then
5 return(maxshr);
6 else
7 return null;
8 end if;
9 end ;
10 /
Function created.
SQL> begin
2 dbms_output.put_line('max shares-'||fun1('syspro'));
3 end;
4 /
max shares-harsh
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> declare
2 cursor c1(pwise person.pname%type)is select pname,sharevalue,noofshares from company, person,
companyperson where company.cno=companyperson.cno and person.pno=companyperson.pno order by
pname;
3 c c1%rowtype;
4 begin
5 open c1('&pwise');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 dbms_output.put_line(c.pname||' '||c.sharevalue||' '||c.noofshares);
10 end loop;
11 close c1;
12 end;
13 /
Enter value for pwise: harsh
old 5: open c1('&pwise');
new 5: open c1('harsh');
harsh 652656 27
harsh 855452 14
lokesh 4545545 14
pradip 6849644 7
roshan 2365844 8
sashikant 5652656 6
PL/SQL procedure successfully completed.

================================================================================
Assignment no :-15

Name :- Datkhile Shubhangi vilas


Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Person (p_no, p_name, p_addr)
Investment (inv_no, inv_name, inv_date, inv_amt)
Relationship between Person and Investment is one-to-many.
Constraints: primary key, foreign key,
p_name and inv_name should not be null,
inv_amt should be greater than 10000.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a procedure which will display details of person, made investment on given date.
2) Write a trigger that restricts insertion or updation of investment having inv_date greater than current
date. (Raise user defined exception and give appropriate message)
SQL> create table person1(pno int primary key, pname varchar(15) not null, paddr varchar(15));
Table created.
create table investment(ino int primary key, iname varchar(15) not null, idate date, iamt int, pno int);
Table created.
SQL> insert into person1 values(101,'lokesh','daund');
1 row created.
SQL> select * from person1;
PNO PNAME PADDR
--------- --------------- ---------------
101 lokesh daund
102 harsh delhi
103 vishal daund
104 roshan hadapsar
105 shashikant pune
-------------------------------------------
SQL> insert into investment values(1,'abc','14 apr 2015',14000,101);
1 row created.
SQL> select * from investment;
INO INAME IDATE IAMT PNO
---------- --------------- --------- ---------- ----------
1 abc 14-APR-15 14000 101
2 def 14-APR-15 27000 102
3 jkl 25-NOV-13 15000 103
4 mno 11-JAN-12 15000 104
5 pqr 31-OCT-14 10000 105
--------------------------------------------------------------------

SQL> set serveroutput on


SQL> create or replace procedure p1(n in varchar) as cursor c1 is select pname, paddr, idate from
person1, investment where person1.pno=investment.pno;
2 c c1%rowtype;
3 begin
4 open c1;
5 dbms_output.put_line('pname'||' '||'paddr');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 if(c.idate=n) then
10 dbms_output.put_line(c.pname||' '||c.paddr);
11 end if;
12 end loop;
13 end;
14 /
Procedure created.
SQL> begin
2 p1('14/4/2015');
3 end;
4 /
pname paddr
lokesh daund
harsh delhi
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> create or replace trigger t1 before insert or update on investment
2 for each row
3 declare
4 d1 varchar(15);
5 d2 varchar(15);
6 begin
7 d1:=to_char(:new.idate,'dd-mm-yyyy');
8 d2:=to_char(sysdate,'dd-mm-yyyy');
9 if(d1>d2) then
10 raise_application_error(-20001,'investment date should be less than current date.');
11 end if;
12 end;
13 /
Trigger created.
SQL> insert into investment values(6,'xyz','26 nov 2016',27000,102);
insert into investment values(6,'xyz','26 nov 2016',27000,102)
*
ERROR at line 1:

ORA-20001: investment date should be less than current date.


ORA-06512: at "SCOTT.T1", line 8
ORA-04088: error during execution of trigger 'SCOTT.T1'

================================================================================
Assignment no :-16
Name :- Datkhile Shubhangi vilas
Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Customer (c_no, c_name, c_city, c_ph_no )
Ticket (t_no, booking_date, fare, traveling_date)
Relationship between Customer and Ticket is one-to-many.
Constraints: primary key, foreign key,
c_name should not be null,
fare should be greater than zero.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a function which will calculate and return total fare collected from customers on given date.
2) Write a cursor which will display date wise ticket booked by customer. (Use parameterized cursor)
SQL> create table customer(cno int primary key,cname varchar(15),ccity varchar(15),cphno int);
Table created.
SQL> create table ticket(tno int primary key,bookingdate varchar(10),fare int check(fare>0),travelingdate
varchar(10),cno int references customer);
Table created.
SQL> insert into customer values(1,'lokesh','daund',4545545);
1 row created.
SQL> select * from customer;
CNO CNAME CCITY CPHNO
---------- --------------- --------------- ----------
1 lokesh daund 5545545
2 roshan hadapsar 855452
3 sashikant pune 2365844
4 pradip mumbai 5652656
5 omkar katraj 6849644
-------------------------------------------------------
SQL> insert into ticket values(101,'5/4/15',255,'12/5/15',1);
1 row created.
SQL> select * from ticket;
TNO BOOKINGDATE FARE TRAVELINGDATE CNO
---------- ---------- ---------- ---------- ----------
101 5/4/15 255 12/5/15 1
102 6/9/13 600 8/12/11 2
103 1/2/07 300 9/4/13 3
104 21/7/15 1000 10/9/15 4
105 25/2/14 254 3/5/14 5
------------------------------------------------------------
SQL> set serveroutput on
SQL> create or replace function fun1(tfrc in varchar) return number as tfc number;
2 begin
3 select sum(fare) into tfc from customer, ticket where customer.cno=ticket.cno;
4 if sql %found then
5 return(tfc);
6 else
7 return null;
8 end if;
9 end fun1;
10 /
Function created.
SQL> begin
2 dbms_output.put_line('total fare collected-'||fun1('12/5/15'));
3 end;
4 /
total fare collected-2409
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> declare
2 cursor c1(dwise ticket.travelingdate%type)is select cname, bookingdate, fare, travelingdate from
customer, ticket where customer.cno=ticket.cno order by travelingdate;
3 c c1%rowtype;
4 begin
5 open c1('&dwise');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 dbms_output.put_line(c.cname||' '||c.bookingdate||' '||c.fare||' '||c.travelingdate);
10 end loop;
11 close c1;
12 end;
13 /
Enter value for dwise: 12/5/15
old 5: open c1('&dwise');
new 5: open c1('12/5/15');
pradip 21/7/15 1000 10/9/15
lokesh 5/4/15 255 12/5/15
omkar 25/2/14 254 3/5/14
roshan 6/9/13 600 8/12/11
sashikant 1/2/07 300 9/4/13
PL/SQL procedure successfully completed.

================================================================================
Assignment no :-17
Name :- Datkhile Shubhangi vilas
Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Student (s_reg_no, s_name, s_class)
Competition (comp_no, comp_name, comp_type)
Relationship between Student and Competition is many-to-many with descriptive attribute rank and year.
Constraints: primary key, foreign key,
primary key for third table(s_reg_no, comp_no, year)
s_name and comp_name should not be null,
comp_type can be sports or academic.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a procedure which will accept year and type of competition as an input and display details of
competition accordingly.
2) Write a trigger that restricts insertion of rank value greater than 3. (Raise user defined exception
and give appropriate message)

SQL> create table student1(sregno int primary key, sname varchar(15),sclass varchar(10));
Table created.
SQL> create table competition(compno int primary key, compname varchar(10) not null, comptype
varchar(10));
Table created.
SQL> create table student1competition(sregno int, compno int,rank int,year int);
Table created.
SQL> insert into student1 values(101,'Lokesh','sybca');
1 row created.
SQL> SELECT * FROM STUDENT1;
SREGNO SNAME SCLASS
---------- --------------- ----------
101 Lokesh sybca
102 Roshan sybca
103 Sashikant sybca
104 Ashish sybca
105 Omkar sybca
------------------------------------------
SQL> insert into competition values(1,'Paint','academic');
1 row created.
SQL> select * from competition;
COMPNO COMPNAME COMPTYPE
---------- ---------- ----------
1 Paint academic
2 Foot Ball Sports
3 Quiz academic
4 Running Sports
5 Chess academic
---------------------------------------------
SQL> insert into student1competition values(101,1,2,2013);
1 row created.
SQL> select * from student1competition;
SREGNO COMPNO RANK YEAR
---------- ---------- ---------- ----------
101 1 2 2013
102 2 3 2012
103 3 1 2015
104 4 2 2011
105 5 1 2014
SQL> set serveroutput on
SQL> create or replace procedure p1(n in varchar) as cursor c1 is select compname, comptype, year
from student1, competition, student1competition where student1.sregno=student1competition.sregno and
competition.compno=student1competition.compno and comptype='academic';
2 c c1%rowtype;
3 begin
4 open c1;
5 dbms_output.put_line('compname'||' '||'comptype');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 if(c.year=n) then
10 dbms_output.put_line(c.compname||' '||c.comptype);
11 end if;
12 end loop;
13 close c1;
14 end;
15 /
Procedure created.
SQL> begin
2 p1(2013);
3 end;
4 /
compname comptype
Paint academic
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> create or replace trigger t2 before insert or update on student1competition
2 for each row
3 begin
4 if(:new.rank>3) then
5 raise_application_error(-20001,'rank should be 1, 2 or 3.');
6 end if;
7 end;
8 /
Trigger created.
SQL> insert into student1competition values(106,6,4,2015);
insert into student1competition values(106,6,4,2015)
*
ERROR at line 1:
ORA-20001: rank should be 1, 2 or 3.
ORA-06512: at "SCOTT.T2", line 3
ORA-04088: error during execution of trigger 'SCOTT.T2'

================================================================================
Assignment no :-18
Name :- Datkhile Shubhangi vilas
Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Owner (o_no, o_name, o_city, o_ph_no)
Estate (e_no, e_type, e_city, e_price)
The Relationship between Owner and Estate is one-to-many.
Constraints: primary key, foreign key,
o_name should not be null,e_type can be flat, bungalow or land.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a function which will count and returns number of owners who have purchase estate in the
same city in which they live.
2) Write a trigger that restricts insertion or updation of estate having price less than 1 lakh. (Raise
user defined exception and give appropriate message)
SQL> create table owner(ono int primary key,oname varchar(15) not null,ocity varchar(15),ophno int);
Table created.
SQL> create table estate(eno int primary key,etype varchar(15),ecity varchar(15),eprice int,ono int
references owner);
Table created.
SQL> insert into owner values(1,'Lokesh','daund',2549696469);
1 row created.
SQL> select * from owner;
ONO ONAME OCITY OPHNO
---------- --------------- --------------- ----------
1 Lokesh daund 2549696469
2 roshan hadapsar 855452
3 sashikant pune 2365844
4 pradip mumbai 5652656
5 omkar katraj 6849644
6 harsh delhi 1514427797
-------------------------------------------------------------
SQL> insert into estate values(101,'flat','pune',65465,1);
1 row created.
SQL> select * from estate;
ENO ETYPE ECITY EPRICE ONO
---------- --------------- --------------- ---------- ----------
101 flat pune 65465 1
102 land mumbai 353552 2
103 flat nagpur 68156652 3
104 bungalow nasik 84152424 4
105 bungalow sangli 6181241 5
106 bungalow delhi 140000 6
------------------------------------------------------------------------
SQL> set serveroutput on
SQL> create or replace function fun1(noofown in varchar) return number as nofown number;
2 begin
3 select count(oname) into nofown from owner, estate where owner.ono=estate.ono and ocity='delhi';
4 if sql %found then
5 return(nofown);
6 else
7 return null;
8 end if;
9 end fun1;
10 /

Function created.
SQL> begin
2 dbms_output.put_line('no of owner- '||fun1('delhi'));
3 end;
4 /
no of owner- 1
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> create or replace trigger t4 before insert or update on estate
2 for each row
3 begin
4 if(:new.eprice<100000)then
5 raise_application_error(-20001,'price should be more than 1 lakh.');
6 end if;
7 end;
8 /
Trigger created.
SQL> insert into estate values(107,'land','delhi',14000,7);
insert into estate values(107,'land','delhi',14000,7)
*
ERROR at line 1:
ORA-20001: price should be more than 1 lakh.
ORA-06512: at "SCOTT.T4", line 3
ORA-04088: error during execution of trigger 'SCOTT.T4'

================================================================================
Assignment no :-19
Name :- Datkhile Shubhangi vilas
Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-
Consider the following entites and their relationship.
Bus (bus_no, capacity, source, destination)
Driver(driver_no, driver_name, license_no, addr, age, salary)
Relationship between Bus and Driver is many-to-many with descriptive attribute date_of_duty_allotted
and shift
Constraints: primary key, foreign key,
Primary key for third table (bus_no , driver_no, date_of_duty_allotted),
driver_name should not be null,
shift can be morning or evening.
Create a RDB in 3Nf and write PL/SQL blocks in Oracle for the following:
1) Write a procedure which will display bus_no and name of allotted driver on given date and shift.
2) Write a trigger that restricts insertion or updation of driver table if driver age is less than 18 or
greater than 50. (Raise user defined exception and give appropriate message.)

SQL> create table bus(busno int primary key,capacity int,source varchar(10),destination varchar(10));
Table created.
SQL> create table driver(dno int primary key,dname varchar(10) not null,licenceno int,addr
varchar(10),age int,salary int);
Table created.
SQL> create table busdriver(busno int,dno int, dateofdutyallotted varchar(10), shift varchar(15));
Table created.

SQL> insert into bus values(101,14,'delhi','daund');


1 row created.
SQL> select * from bus;
BUSNO CAPACITY SOURCE DESTINATIO
---------- ---------- ---------- ----------
101 14 delhi daund
102 27 daund delhi
103 20 daund pune
104 15 nasik mumbai
105 30 hadapsar katraj
---------------------------------------------
SQL>insert into driver values(1,'ram',6464,'pune',25,4969);
1 row created.
SQL> select * from driver;
DNO DNAME LICENCENO ADDR AGE SALARY
---------- ---------- ---------- ---------- ---------- ----------
1 ram 6464 pune 25 4969
2 sam 5454 nasik 30 5535
3 shasi 4452 pune 21 5545
4 omkar 5554 jalgoan 35 6000
5 shubham 5448 nagpur 26 4444
----------------------------------------------------------------------
SQL> insert into busdriver values(101,1,'25/2/15','morning');
1 row created.
SQL> select * from busdriver;
BUSNO DNO DATEOFDUTY SHIFT
---------- ---------- ---------- ---------------
101 1 25/2/15 morning
102 2 21/7/15 morning
103 3 9/4/13 evening
104 4 6/9/13 evening
105 5 12/5/15 morning
----------------------------------------------
SQL> set serveroutput on
SQL> create or replace procedure p1(n in varchar) as cursor c1 is select busdriver.busno, dname, shift,
dateofdutyallotted from bus, driver, busdriver where bus.busno=busdriver.busno and
driver.dno=busdriver.dno and dateofdutyallotted='25/2/15';
2 c c1%rowtype;
3 begin
4 open c1;
5 dbms_output.put_line('busno'||' '||'dname');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 if(c.shift=n) then
10 dbms_output.put_line(c.busno||' '||c.dname);
11 end if;
12 end loop;
13 close c1;
14 end;
15 /
Procedure created.
SQL> begin
2 p1('morning');
3 end;
4 /
busno dname
101 ram
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> create or replace trigger tt2 before insert or update on driver
2 for each row
3 begin
4 if(:new.age<18 or :new.age>50)then
5 raise_application_error(-20001,'drivers age must be greater than 18 and less than 50.');
6 end if;
7 end;
8 /
Trigger created.
SQL> insert into driver values(7,'harsh',6464,'pune',55,4969);
insert into driver values(7,'harsh',6464,'pune',55,4969)
*
ERROR at line 1:
ORA-20001: drivers age must be greater than 18 and less than 50.
ORA-06512: at "SCOTT.TT2", line 3
ORA-04088: error during execution of trigger 'SCOTT.TT2'
SQL> insert into driver values(8,'loki',6464,'pune',15,4969);
insert into driver values(8,'loki',6464,'pune',15,4969)
*
ERROR at line 1:
ORA-20001: drivers age must be greater than 18 and less than 50.
ORA-06512: at "SCOTT.TT2", line 3
ORA-04088: error during execution of trigger 'SCOTT.TT2'

================================================================================
Assignment no :-20
Name :- Datkhile Shubhangi vilas
Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Bus(bus_no, capacity, source, destination)
Driver(driver_no, driver_name, license_no, addr, age, salary)
Relationship between Bus and Driver is many-to-many with descriptive attribute date_of_duty_allotted
and shift
Constraints: primary key, foreign key,
primary key for third table (bus_no, driver_no, date_of_duty_allotted),
driver_name should not be null,
shift can be morning or evening.

Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a procedure which will display bus_no and name of allotted driver on given date and shift.
2) Write a trigger that restricts insertion or updation of driver table if driver age is less than 18 or
greater than 50. (Raise user defined exception and give appropriate message).
SQL> create table drug(dno int primary key,dname varchar(15) not null,company varchar(15),price int);
Table created.
SQL> create table medicalstore(mno int primary key,mname varchar(15) not null,mcity varchar(15),phno
int);
Table created.
SQL> create table drugmedicalstore(dno int,mno int,quantity int);
Table created.
SQL> insert into drug values(101,'insepton','malform',6545);
1 row created.
SQL> Select * from drug;
DNO DNAME COMPANY PRICE
--------- --------------- --------------- ----------
101 insepton malform 6545
102 cojka sector 1895
103 acen mankind 3652
104 syndron sector 5452
105 santin mankind 9824
------------------------------------------------------------
SQL> insert into medicalstore values(1,'new strom','daund',2549696469);
1 row created.
SQL> Select * from medicalstore;
MNO MNAME MCITY PHNO
--------- --------------- --------------- ----------
1 new strom daund 2549696469
2 matrix pune 459632581
3 nitro nasik 2584
4 cronicle nagpur 54862
5 mediplus mumbai 85487
------------------------------------------------------------

SQL> insert into drugmedicalstore values(101,1,250);


1 row created.
SQL> Select * from drugmedicalstore;
DNO MNO QUANTITY
---------- ---------- ----------
101 1 250
102 2 544
103 3 800
104 4 600
105 5 620
--------------------------------------
SQL> set serveroutput on
SQL> create or replace function fun1(nodrg in varchar) return number as nofdrg number;
2 begin
3 select count(quantity) into nofdrg from drug, medicalstore, drugmedicalstore where
drug.dno=drugmedicalstore.dno and medicalstore.mno=drugmedicalstore.mno;
4 if sql %found then
5 return(nofdrg);
6 else
7 return null;
8 end if;
9 end fun1;
10 /
Function created.
SQL> begin
2 dbms_output.put_line('no of drug- '||fun1('matrix'));
3 end;
4 /
no of drug- 5
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> declare
2 cursor c2 is select mname, dname, company, price from drug, medicalstore, drugmedicalstore where
drug.dno=drugmedicalstore.dno and medicalstore.mno=drugmedicalstore.mno order by mname;
3 c c2%rowtype;
4 begin
5 open c2;
6 loop
7 fetch c2 into c;
8 exit when c2%notfound;
9 dbms_output.put_line(c.mname||' '||c.dname||' '||c.company||' '||c.price);
10 end loop;
11 close c2;
12 end;
13 /
cronicle syndron sector 5452

matrix cojka sector 1895

mediplus santin mankind 9824

new strom insepton malform 6545

nitro acen mankind 3652

PL/SQL procedure successfully completed.

================================================================================
Assignment no :-21

Name :- Datkhile Shubhangi vilas


Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Train(t_no, t_name)
Passenger (p_no, p_name, addr, age)
Relationship between Train and Passenger is many-to-many with descriptive attribute date, seat_no and
amt.
Constraints : primary key, foreign key,
primary key for third table (t_no, p_no, date),
t_name and p_name should not be null,
amt should be greater than zero.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a procedure which will display passenger details of given train on ‘1 July 2014’.
2) Write a trigger that restricts insertion of train_passenger detail having date less than current
date. (Raise user defined exception and give appropriate message)
SQL> create table train(tno int primary key,tname varchar(15) not null,noofpassenger int);
Table created.
SQL> create table passenger(pno int primary key,pname varchar(15) not null,addr varchar(15),age int);
Table created.
SQL> create table trainpassenger(tno int,pno int,tpdate date, seatno int,amt int check(amt>0));
Table created.
SQL> insert into train values(101,'garib rath',2);
1 row created.
SQL> select * from train;
TNO TNAME NOOFPASSENGER
---------- --------------- -------------
101 garib rath 2
102 shatabdhi 1
103 deccan 1
104 konark 1
105 duranto 1
------------------------------------------
SQL> insert into passenger values(1,'lokesh','daund',18);
1 row created.
SQL> select * from passenger;
PNO PNAME ADDR AGE
---------- --------------- --------------- ----------
1 lokesh daund 18
2 harsh delhi 18
3 vishal daund 20
4 roshan hadapsar 19
5 shashikant pune 19
6 akash daund 19
----------------------------------------------------

SQL> insert into trainpassenger values(101,1,'1 july 14',14,700);


1 row created.
SQL> select * from trainpassenger;
TNO PNO TPDATE SEATNO AMT
---------- ---------- --------- ---------- ----------
101 1 01-JUL-14 14 700
101 2 12-JUL-14 15 700
102 3 08-DEC-11 17 625
103 4 10-NOV-15 31 500
104 5 09-MAY-13 24 250
105 6 03-SEP-14 45 300
--------------------------------------------------------------
SQL> create or replace procedure p1(n in varchar) as cursor c1 is select pname, addr, age, tpdate from
train, passenger, trainpassenger where train.tno=trainpassenger.tno and
passenger.pno=trainpassenger.pno;
2 c c1%rowtype;
3 begin
4 open c1;
5 dbms_output.put_line('pname'||' '||' addr'||' '||' age');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 if(c.tpdate=n) then
10 dbms_output.put_line(c.pname||' '||c.addr||' '||c.age);
11 end if;
12 end loop;
13 close c1;
14 end;
15 /
Procedure created.
SQL> begin
2 p1('1 july 14');
3 end;
4 /
pname addr age
lokesh daund 18
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> create or replace trigger t1 before insert or update on trainpassenger
2 for each row
3 declare
4 d1 varchar(15);
5 d2 varchar(15);
6 begin
7 d1:=to_char(:new.tpdate,'dd-mm-yyyy');
8 d2:=to_char(sysdate,'dd-mm-yyyy');
9 if(d1<d2) then
10 raise_application_error(-20001,'date should be greater than current date.');
11 end if;
12 end;
13 /
Trigger created.
SQL> insert into trainpassenger values(102,8,'02 feb 1997',14,900);
insert into trainpassenger values(102,8,'02 feb 1997',14,900)
*
ERROR at line 1:
ORA-20001: date should be greater than current date.
ORA-06512: at "SCOTT.T1", line 8
ORA-04088: error during execution of trigger 'SCOTT.T1'

================================================================================
Assignment no :22

Name :- Datkhile Shubhangi vilas


Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Route(route_no, source, destination, no_of_station)
Bus (bus_no, capacity, depot_name)
Relationship between Route and Bus is one-to-many
Constraints: primary key, foreign key,
depot_name should not be null,
bus capacity should be greater than 40.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a function which will return total number of routes having number of stations greater than 10.
2) Write a cursor which will display route wise bus details. (Use parameterized cursor)
SQL> create table route(rno int primary key,source varchar(10),destination varchar(10),noofstation int);
Table created.
SQL> create table bus2(bno int primary key,capacity int,depotname varchar(15),rno int);
Table created.
SQL> insert into route values(101,'delhi','daund',14);
1 row created.

SQL> select * from route;


RNO SOURCE DESTINATIO NOOFSTATION
---------- ---------- ---------- -----------
101 delhi daund 14
103 daund pune 4
104 nasik mumbai 7
105 hadapsar katraj 3
102 daund delhi 14
---------------------------------------------------
SQL> insert into bus2 values(1,64,'pune',101);
1 row created.
SQL> select * from bus2;
BNO CAPACITY DEPOTNAME RNO
---------- ---------- --------------- ----------
1 64 pune 101
2 54 nasik 102
3 52 pune 103
4 56 jalgoan 104
5 48 nagpur 105
-----------------------------------------------
SQL> set serveroutput on
SQL> create or replace function fun1(tr in varchar) return varchar as tnr varchar(15);
2 begin
3 select count(rno) into tnr from route where noofstation>10;
4 if sql %found then
5 return(tnr);
6 else
7 return null;
8 end if;
9 end ;
10 /
Function created.
SQL> begin
2 dbms_output.put_line('total no. of routes- '||fun1('rno'));
3 end;
4 /
total no. of routes- 2
PL/SQL procedure successfully completed.

SQL> set serveroutput on


SQL> declare
2 cursor c1(rwise route.rno%type)is select bus2.rno, capacity, depotname from route, bus2 where
route.rno=bus2.rno order by route.rno;
3 c c1%rowtype;
4 begin
5 open c1('&rwise');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 dbms_output.put_line(c.rno||' '||c.capacity||' '||c.depotname);
10 end loop;
11 close c1;
12 end;
13 /
Enter value for rwise: 101
old 5: open c1('&rwise');
new 5: open c1('101');
101 64 pune
102 54 nasik
103 52 pune
104 56 jalgoan
105 48 nagpur
PL/SQL procedure successfully completed.

================================================================================
Assignment no :-23
Name :- Datkhile Shubhangi vilas
Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


University (u_no, u_name, u_city)
College (c_no, c_name, c_city, year_of_establishment )
Relationship between University and College is one-to-many
Constraints: primary key, foreign key,
u_name and c_name should not be null.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a procedure which will display year wise details of colleges belongs to given University.
2) Write a trigger that restricts insertion of college having year of establishment greater than current
year. (Raise user defined exception and give appropriate message)

SQL> create table university(uno int primary key,uname varchar(20) not null,ucity varchar(15));
Table created.
SQL> create table college(cno int primary key,cname varchar(20) not null,ccity varchar(15),yearofest
int,uno int);
Table created.
SQL> insert into university values(101,'pune university','pune');
1 row created.
SQL> select * from university;
UNO UNAME UCITY
---------- -------------------- ---------------
101 pune university pune
102 delhi university delhi
103 mumbai university mumbai
104 nagpur university nagpur
105 sangli university sangli
-------------------------------------------------
SQL> insert into college values(1,'pune college ','pune','14 apr 1997',101);
1 row created.
SQL> select * from college;
CNO CNAME CCITY YEAROFEST UNO
--------- -------------------- --------------- --------- ----------
1 pune college pune 14-APR-97 101
2 delhi college delhi 25-NOV-91 102
3 mumbai college mumbai 11-JAN-93 103
4 nagpur college nagpur 31-OCT-68 104
5 sangli college sangli 27-JUL-87 105
--------------------------------------------------------------------------
SQL> set serveroutput on
SQL> create or replace procedure p1(n in varchar) as cursor c1 is select uname, cname, ccity, yearofest,
ucity from university,college where university.uno=college.uno;
2 c c1%rowtype;
3 begin
4 open c1;
5 dbms_output.put_line('uname'||' '||'cname'||' '||'ccity'||' '||'yearofest');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 if(c.ucity=n) then
10 dbms_output.put_line(c.uname||' '||c.cname||' '||c.ccity||' '||c.yearofest);
11 end if;
12 end loop;
13 end;
14 /
Procedure created.
SQL> begin
2 p1('delhi');
3 end;
4 /
uname cname ccity yearofest
delhi university delhi college delhi 25-NOV-91
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> create or replace trigger t3 before insert or update on college
2 for each row
3 declare
4 d1 varchar(10);
5 d2 varchar(10);
6 begin
7 d1:=to_char(:new.yearofest,'yyyy');
8 d2:=to_char(sysdate,'yyyy');
9 if(d1>d2) then
10 raise_application_error(-20001,'investment date should be less than current date.');
11 end if;
12 end;
13 /
Trigger created.
SQL> insert into college values(1,'pune university','pune','1 jan 2016',101);
insert into college values(1,'pune university','pune','1 jan 2016',101)*
ERROR at line 1:
ORA-20001: investment date should be less than current date.
ORA-06512: at "SCOTT.T3", line 8
ORA-04088: error during execution of trigger 'SCOTT.T3'

================================================================================
Assignment no :-24

Name :- Datkhile Shubhangi vilas


Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Patient (p_no, p_name, p_addr)
Doctor (d_no, d_name, d_addr, city)
Relationship between Patient and Doctor is many-to-many with descriptive attribute disease and
no_of_visits.
Constraints: primary key, foreign key,
primary key for third table(p_no, d_no, disease),
p_name and d_name should not be null.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a function which will count total number of patients visiting to given doctor for ‘Asthma’.
2) Write a cursor which will display doctor wise details of patients visited to them. (Use parameterized
cursor)
create table patient(pno int primary key,pname varchar(15) not null,paddr varchar(15));
Table created.
SQL> create table doctor(dno int primary key,dname varchar(15) not null,daddr varchar(15),city
varchar(15));
Table created.
SQL> create table patientdoctor(pno int,dno int,disease varchar(15),noofvisits int);
Table created.
SQL> insert into patient values(101,'lokesh','daund');
1 row created.
SQL> select * from patient;
PNO PNAME PADDR
---------- --------------- ---------------
101 lokesh daund
102 harsh delhi
103 vishal daund
104 roshan hadapsar
105 shashikant pune
--------------------------------------------
SQL> insert into doctor values(1,'lokesh','mordern','daund');
1 row created.
SQL> select * from doctor;
DNO DNAME DADDR CITY
---------- --------------- --------------- ---------------
1 lokesh mordern daund
2 harsh modern delhi
3 vishal gopalwadi daund
4 roshan magarpatta hadapsar
5 shashikant dapodi pune
--------------------------------------------------------
SQL> insert into patientdoctor values(101,1,'headache',4);
1 row created.
SQL> select * from patientdoctor;
PNO DNO DISEASE NOOFVISITS
---------- ---------- --------------- ----------
101 1 headache 4
102 2 headache 4
103 3 diabetes 7
104 4 cold 3
105 5 fever 5
--------------------------------------------------
SQL> set serveroutput on
SQL> create or replace function
2 fun1(nop in varchar) return number as nofp number;
3 begin
4 select count(pname) into nofp from patient, doctor, patientdoctor where patient.pno=patientdoctor.pno
and doctor.dno=patientdoctor.dno and disease='headache';
5 if sql %found then
6 return(nofp);
7 else
8 return null;
9 end if;
10 end fun1;
11 /
Function created.
SQL> begin
2 dbms_output.put_line('no of patients -'||fun1('harsh'));
3 end;
4 /
no of patients-2
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> declare
2 cursor c1(dwise doctor.dname%type)is select dname, pname, patient.paddr from patient, doctor, p
atientdoctor where patient.pno=patientdoctor.pno and doctor.dno=patientdoctor.dno order by dname;
3 c c1%rowtype;
4 begin
5 open c1('&dwise');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 dbms_output.put_line(c.dname||' '||c.pname||' '||c.paddr);
10 end loop;
11 close c1;
12 end;
13 /
Enter value for dwise: harsh
old 5: open c1('&dwise');
new 5: open c1('harsh');
harsh harsh delhi
lokesh lokesh daund
roshan roshan hadapsar
shashikant shashikant pune
vishal vishal daund
PL/SQL procedure successfully completed.

================================================================================
Assignment no :-25
Name :- Datkhile Shubhangi vilas
Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Crop (c_no, c_name, c_season, pesticides)
Farmer (f_no, f_name, f_location)
Relationship between Crop and Farmer is many-to-many with descriptive attribute year.
Constraints: primary key, foreign key,
primary key for third table(c_no, f_no, year),
c_name and f_name should not be null,
c_season can be rabi or kharif.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a procedure which will display crop detail harvested by given farmer in given year.
2) Write a trigger which will restricts insertion or updation of crop_farmer table having year greater than
current year. (Raise user defined exception and give appropriate message)
SQL> create table crop(cno int primary key,cname varchar(15) not null,season varchar(15),pesticides
varchar(15));
Table created.
SQL> create table farmer(fno int primary key,fname varchar(15) not null, location varchar(15));
Table created.
SQL> create table cropfarmer(cno int, fno int,year int);
Table created.
SQL> insert into crop values(101,'sugarcane','rabi','abc');
1 row created.
SQL> select * from crop;
CNO CNAME SEASON PESTICIDES
--------- --------------- --------------- ---------------
101 sugarcane rabi abc
102 jowar kharif jkl
103 rice kharif mno
104 potato rabi pqr
105 grounut kharif xyz
------------------------------------------------------
SQL> insert into farmer values(1,'lokesh','ashok nagar');
1 row created.
SQL> select * from farmer;
FNO FNAME LOCATION
--------- --------------- ---------------
1 lokesh ashok nagar
2 harsh modern
3 vishal gopalwadi
4 roshan magarpatta
5 shashikant dapodi
------------------------------------------
SQL> insert into cropfarmer values(101,1,'27 july 2015’);
1 row created.
SQL> select * from cropfarmer;
CNO FNO YEAR
--------- ---------- ----------
101 1 2015
102 2 2015
103 3 2013
104 4 2012
105 5 2014
---------------------------------
SQL> set serveroutput on
SQL> create or replace procedure p1(n in varchar) as cursor c1 is select cname, season, pesticides,
fname, year from crop, farmer, cropfarmer where crop.cno=cropfarmer.cno and
farmer.fno=cropfarmer.fno and year='14 apr 2015';
2 c c1%rowtype;
3 begin
4 open c1;
5 dbms_output.put_line('cname'||' '||'season'||' '||'pesticides');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 if(c.fname=n) then
10 dbms_output.put_line(c.cname||' '||c. season ||' '||c.pesticides);
11 end if;
12 end loop;
13 close c1;
14 end;
15 /
Procedure created.
SQL> begin
2 p1('harsh');
3 end;
4 /
cname season pesticides
jowar kharif jkl
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> create or replace trigger t4 before insert or update on cropfarmer
2 for each row
3 declare
4 d1 varchar(10);
5 d2 varchar(10);
6 begin
7 d1:=to_char(:new.year,'yyyy');
8 d2:=to_char(sysdate,'yyyy');
9 if(d1>d2) then
10 raise_application_error(-20001,'year should be less than current date.');
11 end if;
12 end;
13 /
Trigger created.
SQL> insert into cropfarmer values(106,6, '31 oct 2016');
insert into cropfarmer values(106,6, '31 oct 2016')
*
ERROR at line 1:
ORA-20001: year should be less than current date.
ORA-06512: at "SCOTT.T4", line 8
ORA-04088: error during execution of trigger 'SCOTT.T4'

================================================================================
Assignment no :-26
Name :- Datkhile Shubhangi vilas
Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Researcher (r_no, r_name, r_city)
Research_Paper (rp_no, rp_title, rp_subject, rp_level)
Relationship between Researcher and Research_Paper is many-to-many with descriptive attribute year.
Constraints: primary key, foreign key,
r_name and rp_title should not be null,
rp_subject can be computer or electronics or finance.
rp_level can be state, national or international.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a function which will return name of subject for which maximum numbers of national level
papers were presented by researcher in year 2013.
2) Write a cursor which will display rp_level wise and researcher wise details of research paper
presented by them.
SQL> create table researcher(rno int primary key,rname varchar(15) not null,rcity varchar(15));
Table created.
SQL> create table researchpaper(rpno int primary key,rptitle varchar(20) not null,rpsubject varchar(15),
rplevel varchar(15));

Table created.
SQL> create table researcherpaper(rno int, rpno int,year int);
Table created.
SQL> insert into researcher values(101,'lokesh','daund');
1 row created.
SQL> select * from researcher;
RNO RNAME RCITY
--------- --------------- ---------------
101 lokesh daund
102 harsh delhi
103 vishal daund
104 roshan mumbai
105 shashikant pune
-------------------------------------------
SQL> insert into researchpaper values(1,'hardware','computer','international');
1 row created.
SQL> select * from researchpaper;
RPNO RPTITLE RPSUBJECT RPLEVEL
---------- -------------------- --------------- ---------------
1 hardware computer international
2 software computer international
3 LCD electronics national
4 shares finance national
5 TV electronics state
-----------------------------------------------------------------
SQL> insert into researcherpaper values(101,1,2015);
1 row created.
SQL> select * from researcherpaper;
RNO RPNO YEAR
---------- ---------- ----------
101 1 2015
102 2 2015
103 3 2011
104 4 2012
105 5 2013
-----------------------------------
SQL> set serveroutput on
SQL> create or replace function fun1(ns in varchar) return varchar as nmofs varchar(15);
2 begin
3 select (rpsubject) into nmofs from researchpaper, researcherpaper where
researchpaper.rpno=researcherpaper.rpno and year=2012;
4 if sql %found then
5 return(nmofs);
6 else
7 return null;
8 end if;
9 end ;
10 /
Function created.
SQL> begin
2 dbms_output.put_line('subject is '||fun1('national'));
3 end;
4 /
subject is finance
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> declare
2 cursor c1(rwise researcher.rname%type)is select rptitle, rpsubject, rplevel, rname from researcher,
researchpaper, researcherpaper where researcher.rno=researcherpaper.rno and
researchpaper.rpno=researcherpaper.rpno order by rplevel;
3 c c1%rowtype;
4 begin
5 open c1('&rwise');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 dbms_output.put_line(c.rname||' '||c.rptitle||' '||c.rpsubject||' '||c.rplevel);
10 end loop;
11 close c1;
12 end;
13 /
Enter value for rwise: lokesh
old 5: open c1('&rwise');
new 5: open c1('lokesh');
lokesh hardware computer international
harsh software computer international
vishal LCD electronics national
roshan shares finance national
shashikant TV electronics state
PL/SQL procedure successfully completed.

================================================================================
Assignment no :-27
Name :- Datkhile Shubhangi vilas
Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Donor (donor_no, donor_name, city)
Blood_donation_detail (bd_no, blood_group, qty, date_of_collection)
Relationship between Donor and Blood_donation_detail is one-to-many.
Constraints: primary key, foreign key,
donor_name should not be null,
blood_group can be A+, A-, B+, B-, AB+, AB-, O+, O-
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a procedure which will display blood group wise total amount of quantity of blood available.
2) Write a trigger that restricts insertion of blood_donation_details having quantity greater than
300ml. (Raise user defined exception and give appropriate message)
SQL> create table donor(dno int primary key,dname varchar(15) not null,city varchar(15));
Table created.
SQL> create table blooddonation(bno int primary key,bloodgroup varchar(10),qty int,dateofcoll
varchar(15),dno int);
Table created.
SQL> insert into donor values(101,'lokesh','daund');
1 row created.
SQL> select * from donor;
DNO DNAME CITY
---------- --------------- ------------
101 lokesh daund
102 harsh delhi
103 vishal daund
104 roshan hadapsar
105 shashikant pune
-----------------------------------------
SQL> insert into blooddonation values(1,'b+',1,'14/4/2015',101);
1 row created.
SQL> select * from blooddonation;
BNO BLOODGROUP QTY DATEOFCOLL DNO
--------- ---------- ---------- --------------- ----------
1 b+ 1 14/4/2015 101
2 b+ 1 14/4/2015 102
3 o+ 2 25/9/2013 103
4 a- 3 11/3/2012 104
5 ab+ 3 31/11/2014 105
-----------------------------------------------------------------
SQL> set serveroutput on
SQL> create or replace procedure p3(n in varchar) as cursor c1 is select qty, bloodgroup from
blooddonation order by bloodgroup;
2 c c1%rowtype;
3 begin
4 open c1;
5 dbms_output.put_line('bloodgroup'||' '||'qty');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 dbms_output.put_line(c.bloodgroup||' '||c.qty);
10 end loop;
11 close c1;
12 end;
13 /
Procedure created.
SQL> begin
2 p3('bloodgroup');
3 end;
4 /
bloodgroup qty
a- 3
ab+ 3
b+ 1
b+ 1
o+ 2
PL/SQL procedure successfully completed.

SQL> set serveroutput on


SQL> create or replace trigger t6 before insert or update on blooddonation
2 for each row
3 begin
4 if(:new.qty>300)then
5 raise_application_error(-20001,'blood must be less than 300ml');
6 end if;
7 end;
8 /
Trigger created.
SQL> insert into blooddonation values(6,'ab+',350,'31/11/2014',105);
insert into blooddonation values(6,'ab+',350,'31/11/2014',105)
*
ERROR at line 1:
ORA-20001: blood must be less than 300ml
ORA-06512: at "SCOTT.T6", line 3
ORA-04088: error during execution of trigger 'SCOTT.T6'

================================================================================
Assignment no :-28
Name :- Datkhile Shubhangi vilas
Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Client (c_no, c_name, c_addr, birth_date)
Policy_info (p_no, p_name, maturity_amt, prem_amt, policy_term)
Relationship between Client and Policy_info is many-to-many with descriptive attribute date_of_purchase.
Constraints: primary key, foreign key,
c_name and p_name should not be null,
policy_term should be greater than zero.

Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a function which will return name of policy taken by minimum number of clients.
2) Write a cursor which will display client wise policy details. (Use parameterized cursor)
SQL> create table client(cno int primary key,cname varchar(15) not null,addr varchar(15),birthdate
varchar(15));
Table created.
SQL> create table policyinfo(pno int primary key,pname varchar(15) not null,maturityamt int,premamt
int,policyterm int);
Table created.
SQL> create table clientpolicy(cno int, pno int,dateofpurchase varchar(15),year int);
Table created.
SQL> insert into client values(101,'lokesh','daund','27/7/97');
1 row created.
SQL> select * from client;
CNO CNAME ADDR BIRTHDATE
---------- --------------- --------------- ---------------
101 lokesh daund 27/7/97
102 harsh delhi 14/4/97
103 vishal daund 15/9/95
104 roshan hadapsar 21/6/96
105 shashikant pune 30/5/97
----------------------------------------------------------
SQL> insert into policyinfo values(1,'abc',60000,55000,5);
1 row created.
SQL> select * from policyinfo;
PNO PNAME MATURITYAMT PREMAMT POLICYTERM
--------- --------------- ----------- ---------- ----------
1 abc 60000 55000 5
2 jkl 50000 30000 4
3 mno 80000 49000 9
4 pqr 20000 25000 6
5 xyz 40000 45000 7
----------------------------------------------------------------
SQL> insert into clientpolicy values(101,1,'12/5/15',2015);
1 row created.
SQL> select * from clientpolicy;
CNO PNO DATEOFPURCHASE YEAR
---------- ---------- --------------- ----------
101 1 12/5/15 2015
102 2 8/12/11 2011
103 3 9/4/13 2013
104 4 10/9/15 2015
105 5 3/5/14 2014
------------------------------------------------------
SQL> set serveroutput on
SQL> create or replace function fun1(pn in varchar) return varchar as minp varchar(15);
2 begin
3 select (pname) into minp from client, policyinfo, clientpolicy where client.cno=clientpolicy.cno and
policyinfo.pno=clientpolicy.pno and clientpolicy.cno=(select min(clientpolicy.cno) from clientpolicy);
4 if sql %found then
5 return(minp);
6 else
7 return null;
8 end if;
9 end ;
10 /
Function created.
SQL> begin
2 dbms_output.put_line('minimum no. of clients policy is'||fun1('pname'));
3 end;
4 /
minimum no. of clients policy is abc
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> declare
2 cursor c1(cwise client.cname%type)is select cname, pname, maturityamt, premamt, policyterm from
client, policyinfo, clientpolicy where client.cno=clientpolicy.cno and policyinfo.pno=clientpolicy.
pno;
3 c c1%rowtype;
4 begin
5 open c1('&cwise');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 dbms_output.put_line(c.cname||' '||c.pname||' '||c.maturityamt||' '||c.premamt||' '||c.policyterm);
10 end loop;
11 close c1;
12 end;
13 /
Enter value for cwise: harsh
old 5: open c1('&cwise');
new 5: open c1('harsh');
lokesh abc 60000 55000 5
harsh jkl 50000 30000 4
vishal mno 80000 49000 9
roshan pqr 20000 25000 6
shashikant xyz 40000 45000 7
PL/SQL procedure successfully completed.

================================================================================
Assignment no :-29

Name :- Datkhile Shubhangi vilas


Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Company (c_no, c_name, c_city, c_share_value)
Person (p_no, p_name, p_city, p_ph_no)
Relationship between Company and Person is many-to-many with descriptive attribute no_of_shares.
Constraints: primary key, foreign key,
c_name and p_name should not be null,
no_of_shares should be greater than zero.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a procedure which will display names of person who are shareholder of the given company.
2) Write a trigger which get activated when share value of company become less than Rs. 10. (Raise
user defined exception and give appropriate message)
SQL> create table company(cno int primary key, cname varchar(15) not null, ccity varchar(15),
sharevalue int);
Table created.
SQL> create table person(pno int primary key, pname varchar(15) not null, pcity varchar(15), pphno int);
Table created.
SQL> create table companyperson (cno int, pno int, noofshares int);
Table created.
SQL> insert into company values(101,'tcs','daund',4545545);
1 row created.
SQL> select * from company;
CNO CNAME CCITY SHAREVALUE
---------- --------------- --------------- ----------
101 tcs daund 4545545
102 microsoft hadapsar 855452
103 wipro pune 2365844
104 sys tech mumbai 5652656
105 ibm katraj 6849644
106 syspro mumbai 652656
----------------------------------------------------------
SQL> insert into person values(1,'lokesh','daund',4545545);
1 row created.
SQL> select * from person;
PNO PNAME PCITY PPHNO
--------- --------------- --------------- ----------
1 lokesh daund 4545545
2 harsh delhi 6849644
3 roshan hadapsar 855452
4 sashikant pune 2365844
5 pradip mumbai 5652656
6 harsh delhi 6849644
---------------------------------------------------------------
SQL> insert into companyperson values(101,1,14);
1 row created.
SQL> select * from companyperson;
CNO PNO NOOFSHARES
---------- ---------- ----------
101 1 14
102 2 14
103 3 8
104 4 6
105 5 7
106 6 27
----------------------------------
SQL> set serveroutput on
SQL> create or replace procedure p1(n in varchar) as cursor c1 is select cname, pname from company,
person, companyperson where company.cno=companyper
son.cno and person.pno=companyperson.pno;
2 c c1%rowtype;
3 begin
4 open c1;
5 dbms_output.put_line('cname'||' '||'pname');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 if(c.cname=n) then
10 dbms_output.put_line(c.cname||' '||c.pname);
11 end if;
12 end loop;
13 close c1;
14 end;
15 /
Procedure created.
SQL> begin
2 p1('microsoft');
3 end;
4 /
cname pname
microsoft harsh
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> create or replace trigger t33 before insert or update on company
2 for each row
3 begin
4 if(:new.sharevalue<10) then
5 raise_application_error(-20001,'Share value must be greater than Rs.10.');
6 end if;
7 end;
8 /
Trigger created.
SQL> insert into company values(107,'tcs','daund', 5);
insert into company values(107,'tcs','daund', 5)
*
ERROR at line 1:
ORA-20001: Share value must be greater than Rs.10.
ORA-06512: at "SCOTT.T33", line 3
ORA-04088: error during execution of trigger 'SCOTT.T33'

================================================================================
Assignment no :-30
Name :- Datkhile Shubhangi vilas
Class :-SY BBA (CA)
Subject :- Relational Database Management System
Assignment Name :-

Consider the following entities and their relationship.


Person (p_no, p_name, p_addr)
Investment (inv_no, inv_name, inv_date, inv_amt)
Relationship between Person and Investment is one-to-many.
Constraints: primary key, foreign key,
p_name and inv_name should not be null,
inv_amt should be greater than 10000.
Create a RDB in 3NF and write PL/SQL blocks in Oracle for the following:
1) Write a function which will return name of person having maximum total amount of investment.
2) Write a cursor which will display person wise details of investment. (Use parameterized cursor)
SQL> create table person1(pno int primary key, pname varchar(15) not null, paddr varchar(15));
Table created.
create table investment(ino int primary key, iname varchar(15) not null, idate date, iamt int, pno int);
Table created.
SQL> insert into person1 values(101,'lokesh','daund');
1 row created.

SQL> select * from person1;


PNO PNAME PADDR
---------- --------------- ---------------
101 lokesh daund
102 harsh delhi
103 vishal daund
104 roshan hadapsar
105 shashikant pune
-------------------------------------------
SQL> insert into investment values(1,'abc','14 apr 2015',14000,101);
1 row created.
SQL> select * from investment;
INO INAME IDATE IAMT PNO
---------- --------------- --------- ---------- ----------
1 abc 14-APR-15 14000 101
2 def 14-APR-15 27000 102
3 jkl 25-NOV-13 15000 103
4 mno 11-JAN-12 15000 104
5 pqr 31-OCT-14 10000 105
--------------------------------------------------------------------
SQL> set serveroutput on
SQL> create or replace function fun1(pn in varchar) return varchar as maxinv varchar(15);
2 begin
3 select (pname) into maxinv from person1, investment where person1.pno=investment.pno and
investment.pno=(select max(investment.pno) from investment);
4 if sql %found then
5 return(maxinv);
6 else
7 return null;
8 end if;
9 end ;
10 /
Function created.
SQL> begin
2 dbms_output.put_line('maximum no. of invested person is '||fun1('pname'));
3 end;
4 /
maximum no. of invested person is shashikant
PL/SQL procedure successfully completed.
SQL> set serveroutput on
SQL> declare
2 cursor c1(pwise person1.pname%type)is select pname, iname, idate, iamt from person1, investment
where person1.pno=investment.pno;
3 c c1%rowtype;
4 begin
5 open c1('&pwise');
6 loop
7 fetch c1 into c;
8 exit when c1%notfound;
9 dbms_output.put_line(c.pname||' '||c.iname||' '||c.idate||' '||c.iamt);
10 end loop;
11 close c1;
12 end;
13 /

Enter value for pwise: harsh


old 5: open c1('&pwise');
new 5: open c1('harsh');
lokesh abc 14-JUL-15 14000
harsh def 14-APR-15 27000
vishal jkl 25-NOV-13 15000
roshan mno 11-JAN-12 15000
shashikant pqr 31-OCT-14 10000
PL/SQL procedure successfully completed.

S-ar putea să vă placă și