0% found this document useful (0 votes)
14 views31 pages

PL SQL ASSIGNMENT - 4 Solution

The document contains a series of PL/SQL assignments focused on database operations, including displaying student information based on various criteria, managing account transactions, and updating master-detail relationships in tables. Each assignment includes a PL/SQL block that utilizes cursors to retrieve and manipulate data, along with exception handling for cursor-related errors. The document also outlines the creation of tables and insertion of data, demonstrating practical applications of PL/SQL in database management.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views31 pages

PL SQL ASSIGNMENT - 4 Solution

The document contains a series of PL/SQL assignments focused on database operations, including displaying student information based on various criteria, managing account transactions, and updating master-detail relationships in tables. Each assignment includes a PL/SQL block that utilizes cursors to retrieve and manipulate data, along with exception handling for cursor-related errors. The document also outlines the creation of tables and insertion of data, demonstrating practical applications of PL/SQL in database management.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

PL SQL ASSIGNMENT-4

1. Write PL/SQL Block do display the information of


student living in given city.

declare

cursor c_stud is select * from


student where city = '&city';
begin
for m_stud in c_stud
loop
dbms_output.put_line(
m_stud.rollno || ' ' || m_stud.name || ' ' || m_stud.dob
|| ' ' || m_stud.city || ' ' || m_stud.marks);
end loop;
exception
when cursor_already_open then
dbms_output.put_line('
cursor is alredy opened ');
end;
/

2. Write PL/SQL Block to display the mark-sheet of


student based on given rollno.
Page 1
PL SQL ASSIGNMENT-4

declare
cursor c_exam is select * from
exam where rollno = &rollno;
begin
for m_exam in c_exam
loop
dbms_output.put_line(
m_exam.rollno || ' ' || m_exam.paperno || ' ' ||
m_exam.marks);
end loop;
exception
when cursor_already_open then
dbms_output.put_line('
cursor is alredy opened ');
end;
/

3. Write PL/SQL Block to display the information of all


account holder.

declare
cursor c_acc is select * from
account where no = &ano;
Page 2
PL SQL ASSIGNMENT-4
begin
for m_acc in c_acc
loop
dbms_output.put_line(
m_acc.no || ' ' || m_acc.name || ' ' || m_acc.dob || ' ' ||
m_acc.op_date || ' ' || m_acc.type || ' ' || m_acc.status
|| ' ' || m_acc.flag || ' ' || m_acc.gender ||
m_acc.address || ' ' || m_acc.city || ' ' || m_acc.bal);
end loop;
exception
when cursor_already_open then
dbms_output.put_line('
cursor is alredy opened ');
end;
/

4. Write PL/SQL Block to display the information of


student whose age is greater than 18.

declare
cursor c_stud is select * from
student where to_char(sysdate,'yyyy') -
to_char(dob,'yyyy') >= 18;
begin
Page 3
PL SQL ASSIGNMENT-4
for m_stud in c_stud
loop

dbms_output.put_line(m_stud.rollno || ' ' ||


m_stud.name || ' ' || m_stud.dob || ' ' || m_stud.city ||
' ' || m_stud.marks);
end loop;
exception
when cursor_already_open then
dbms_output.put_line('
cursor is already open ' );
end;
/

5. Write PL/SQL Block to display the information of


student who has paid or not paid fees.

declare
cursor cp_std is select * from
student where rollno in (select rollno from fees);
cursor cnp_std is select * from
student where rollno not in (select rollno from fees);
begin
dbms_output.put_line(' list of
Page 4
PL SQL ASSIGNMENT-4
student who payed fees ');
for m_std in cp_std
loop

dbms_output.put_line(m_std.rollno || ' ' || m_std.name


|| ' ' || m_std.dob || ' ' || m_std.city || ' ' ||
m_std.marks);
end loop;

dbms_output.put_line('==============================
============');
dbms_output.put_line(' list of
student who not payed fees ');
for m_std in cnp_std
loop

dbms_output.put_line(m_std.rollno || ' ' || m_std.name


|| ' ' || m_std.dob || ' ' || m_std.city || ' ' ||
m_std.marks);
end loop;
exception
when cursor_already_open then
dbms_output.put_line('
your cursor is already opened ');
Page 5
PL SQL ASSIGNMENT-4
end;
/

6. Write PL/SQL Block to display mark-sheet of all


students.(As per mark-sheet format)

declare
cursor c_s1(rn exam.rollno%type) is
select * from exam where paperno = 101 and rollno = rn;
-- paper no 101
cursor c_s2(rn exam.rollno%type) is
select * from exam where paperno = 102 and rollno =
rn; -- paper no 102
cursor c_s3(rn exam.rollno%type) is
select * from exam where paperno = 103 and rollno =
rn; -- paper no 103
cursor c_std is select * from
student;
tot number(10);
per number(10,2);
begin
for m_std in c_std
loop

Page 6
PL SQL ASSIGNMENT-4
dbms_output.put_line('
rollno : ' || m_std.rollno);
dbms_output.put_line('
name : ' || m_std.name);
-- paper no 101
for m_m1 in
c_s1(m_std.rollno)
loop

dbms_output.put_line(' PL sql : ' || m_m1.marks);


end loop;
-- paper no 102
for m_m2 in
c_s2(m_std.rollno)
loop

dbms_output.put_line(' JAVA : ' || m_m2.marks);


end loop;
-- paper no 103
for m_m3 in
c_s3(m_std.rollno)
loop

dbms_output.put_line(' Network security : ' ||


Page 7
PL SQL ASSIGNMENT-4
m_m3.marks);
end loop;
select sum(marks) into
tot from exam where rollno = m_std.rollno group by
rollno;
dbms_output.put_line( '
total : ' || tot);
per := tot/3;
dbms_output.put_line( '
percentage : ' || per);

dbms_output.put_line('______________________________
____');
end loop;

exception
when cursor_already_open then
dbms_output.put_line('
cursor is already opened ' );
end;
/

7. Write PL/SQL Block to display summary result of all


students in given format (rollno, name,total,
Page 8
PL SQL ASSIGNMENT-4
percentage, result)

declare
cursor c_std is select * from
student;
tot number(10);
per number(10,2);
begin
for m_std in c_std
loop

dbms_output.put_line('
rollno : ' || m_std.rollno);
dbms_output.put_line('
name : ' || m_std.name);

select sum(marks) into


tot from exam where rollno = m_std.rollno group by
rollno;
dbms_output.put_line( '
total : ' || tot);
per := tot/3;
dbms_output.put_line( '
percentage : ' || per);
Page 9
PL SQL ASSIGNMENT-4
end loop;

dbms_output.put_line('______________________________
____');
exception
when cursor_already_open then
dbms_output.put_line('
cursor is already opened ' );
end;
/

8. Write PL/SQL Block to display the information of


transaction perform in each branch in given
format
Branch no : Branch Name :

====================================================
============================
Name of Account holder, Transaction Date,
Transaction Amount, Transaction Type
---
---- -----
-----

Page 10
PL SQL ASSIGNMENT-4
====================================================
============================

declare
cursor c_bran is select * from bran;
cursor c_t(bno
tran.bran_no%type) is select * from tran where bran_no
= bno;
cursor c_name(ano
account.no%type) is select name from account where
no = ano;
begin
for m_bran in c_bran
loop

dbms_output.put_line('Branch no : ' || m_bran.b_no || '


Branch Name : ' || m_bran.bran_name);

dbms_output.put_line('----------------------------------
-------------');

dbms_output.put_line('name tran_date
tran_amount tran_type');
Page 11
PL SQL ASSIGNMENT-4
for m_t in
c_t(m_bran.b_no)
loop
for m_name
in c_name(m_t.no)
loop

if(m_t.t_amount > 0)then

dbms_output.put_line(m_name.name || ' ' ||


m_t.t_date || ' ' || m_t.t_amount || ' deposit');
else

dbms_output.put_line(m_name.name || ' ' ||


m_t.t_date || ' ' || m_t.t_amount || ' withdrow');

end if;
end loop;
end loop;

dbms_output.put_line('==============================
=================');
dbms_output.put_line('-
-');
Page 12
PL SQL ASSIGNMENT-4

end loop;
end;
/

9. Write Pl/SQL Block to display the information of


male account holder and female account
holder information in given format.

Gender : Male

====================================================
=====================
Account number Name account
opening date account balance
---- ---
---
---

====================================================
=====================
total male account holder

-----------------------------------------------------------
Page 13
PL SQL ASSIGNMENT-4
--------------
Gender : female

====================================================
=====================
Account number Name account
opening date account balance
---- ---
---
---

====================================================
=====================
total female account holder

declare
cursor cm_acc is select * from
account where gender = 'm';
cursor cf_acc is select * from
account where gender = 'f';
cnt_m number;
cnt_f number;
begin
dbms_output.put_line('Gender :
Page 14
PL SQL ASSIGNMENT-4
Male');

dbms_output.put_line('==============================
======================');
dbms_output.put_line('acc_no
name op_date balance');
dbms_output.put_line('-------
-------- ----------- --------- ');
for m_m in cm_acc
loop

dbms_output.put_line(m_m.no || ' ' || m_m.name ||


' ' || m_m.op_date || ' ' || m_m.bal);
cnt_m :=
cm_acc%rowcount;
end loop;

dbms_output.put_line('----------------------------------
------------------');
dbms_output.put_line('total male
account holder : ' || cnt_m);
dbms_output.put_line('=
=');
dbms_output.put_line('Gender :
Page 15
PL SQL ASSIGNMENT-4
female');

dbms_output.put_line('==============================
======================');
dbms_output.put_line('acc_no
name op_date balance');
dbms_output.put_line('-------
-------- ----------- --------- ');
for m_m in cf_acc
loop

dbms_output.put_line(m_m.no || ' ' || m_m.name ||


' ' || m_m.op_date || ' ' || m_m.bal);
cnt_f :=
cf_acc%rowcount;
end loop;

dbms_output.put_line('----------------------------------
------------------');
dbms_output.put_line('total
female account holder : ' || cnt_f);
end;
/

Page 16
PL SQL ASSIGNMENT-4
10. Write PL/SQL Block to perform transaction in bank
and update balance accordingly. While
performing withdraw transaction , check
that sufficient balance is available or not , if no
sufficient balance than display appropriate
message if balance is available then perform
transaction. Maintain minimum balance
500.

declare
acno account.no%type := &accno;
amt account.bal%type :=
&amount;
mbal account.bal%type;
a_bal account.bal%type;
begin
select bal into mbal from account
where no = acno;
if (amt < 0 ) then
if((mbal-amt) > 500)
then
update
account set bal = mbal + amt where no = acno;

Page 17
PL SQL ASSIGNMENT-4
dbms_output.put_line(' withdraw success ');
else

dbms_output.put_line(' sufficient balance not available


');
end if;

else
a_bal := mbal + amt;
update account set bal
= a_bal where no = acno;
dbms_output.put_line('
deposite transaction success');
end if;
end;
/

11. Implement the following in Oracle:


Establish the Primary key and Foreign key
relationship between Master table and detail table
Define proper Data type and size .
Create the Master table
item_no stock Unit_expired
100 0 0
Page 18
PL SQL ASSIGNMENT-4
200 0 0
300 0 0

-create the detail table

item_no units expire_date


100 45 01-may-2005
200 30 08-jan-2005
100 25 08-jan-2005
300 10 08-feb-2005
100 65 25-jun-2005

- write a pl-sql block to update the master


table
Fetch the system date and compare with the
expire_date
if expire_date > system date then update the
stock with units otherwise do
not update the stock with units but update
uint_expired with units

display following output on the screen


= = = = = = = = = = = == == == === == == == = = = =
====
Page 19
PL SQL ASSIGNMENT-4
item no units unit expired
total
= = = == = = = == = == = = = = = = == = = = = = = =
====
100 110
25
135
= = == == == = == = == == = == = = == = == = == = =
== = ==
200 000
30
030
= = == == == = == = == == = == = = == = == = == = =
== = ==
300 010
00
010
= = == == == = == = == == = == = = == = == = == = =
== = ==
grand total 120
55
175
= = == == == = == = == == = == = = == = == = == = =
== = ==
Page 20
PL SQL ASSIGNMENT-4

-- performing

-- creating table

create table master (item_no numner(3),


stock number(3),
unit_expired number(3));

create table detail(item_no number(3),


units number(3),
expire_date date);

-- insert data on tables


insert into master
values(&item_no,&stock,&unit_expire);
insert into detail
values(&item_no,&units,'&expire_date');

-- PL/SQL block ....

-- PL/SQL block to update master with the expire unit


Page 21
PL SQL ASSIGNMENT-4
of not .....
declare
cursor c_detail is select * from
detail;
exp_dt date;
begin

for c in c_detail
loop
exp_dt := c.expire_date;
if (to_char(exp_dt,'dd')
>= to_char(sysdate,'dd') and to_char(exp_dt,'mon') >=
to_char(sysdate,'mon') and to_char(exp_dt,'yyyy') >=
to_char(sysdate,'yyyy') ) then
update
master set stock = (stock + c.units) where item_no =
c.item_no;
else
update
master set Unit_expired = (Unit_expired + c.units) where
item_no = c.item_no;
end if;
end loop;
end;
Page 22
PL SQL ASSIGNMENT-4
/

-- PL/SQL block to display date of master in given


formate ......
declare
cursor c_master is select * from
master;
exp_dt date;
cnt_unit number(10) := 0;
cnt_unit_expired number(10) := 0;
begin
dbms_output.put_line('Item_no
units unit_expired total ');

dbms_output.put_line('==============================
================');
for i in c_master
loop

dbms_output.put_line(i.item_no || ' ' || i.stock || '


' || i.unit_expired || ' ' || (i.stock +
i.unit_expired) );

dbms_output.put_line('==============================
Page 23
PL SQL ASSIGNMENT-4
================');
cnt_unit := (cnt_unit +
i.stock);
cnt_unit_expired :=
(cnt_unit_expired + i.unit_expired);
end loop;
dbms_output.put_line('grand total
' || cnt_unit || ' ' || cnt_unit_expired || ' ' ||
(cnt_unit + cnt_unit_expired));

exception
when no_data_found then
dbms_output.put_line('
invalid item no ');
end;
/

12. Write PL/SQL block to display the information of


library transaction perform by all student in
given format and calculate penalty amount
require to paid by student. Calculate penalty if
student have keep book more than 7 day
the charge penalty Rs 1/- per day
Page 24
PL SQL ASSIGNMENT-4

Rollno __________ Name:


====================================================
====================
Book Title Issue Date Return Date. Penalty
Amount
---- ---- ---- ----

====================================================
====================
Total No Of Books Read = ___________ Total
Penalty

13. Write Pl/Sql block to display information of all


account holder and at the end display following
summary information
1. Total Account Holder
2. Male Account holder and female account
holder
3. Total saving account and total current
account
4. Total open account and total close
Page 25
PL SQL ASSIGNMENT-4
account.

declare
cursor c_acc is select * from
account;
cnt_m number:= 0;
cnt number := 0;
cnt_s number := 0;
cnt_c number := 0;
cnt_o number := 0;
cnt_clo number := 0;
begin
for m_acc in c_acc
loop
dbms_output.put_line(
m_acc.no || ' ' || m_acc.name || ' ' || m_acc.dob || ' ' ||
m_acc.op_date || ' ' || m_acc.gender || ' ' || m_acc.city
|| ' ' || m_acc.bal || ' ' || m_acc.type);
if(m_acc.gender = 'm')
then
cnt_m :=
cnt_m + 1;
end if;
if(m_acc.type = 's') then
Page 26
PL SQL ASSIGNMENT-4
cnt_s := cnt_s +
1;
else
cnt_c := cnt_c
+ 1;
end if;
if (m_acc.status = 'o')
then
cnt_o := cnt_o
+ 1;
else
cnt_clo :=
cnt_clo + 1;
end if;
cnt := cnt + 1;
end loop;
dbms_output.put_line('
====================================================
============== ');
dbms_output.put_line('
total account holder = ' || cnt);
dbms_output.put_line('
total male account holder = ' || cnt_m);
dbms_output.put_line('
Page 27
PL SQL ASSIGNMENT-4
total saving account holder = ' || cnt_s);
dbms_output.put_line('
total current account holder = ' || cnt_c);
dbms_output.put_line('
total open account holder = ' || cnt_o);
dbms_output.put_line('
total close account holder = ' || cnt_clo);

end;
/

14. Write pl/sql block to display the information of book


published by all publisher in given format

Publisher name
====================================================
===========
Details of book published by publisher

declare
cursor c_pub is select
pub_code,name from publisher;
cursor c_book(pb_cd
publisher.pub_code%type) is select * from book where
Page 28
PL SQL ASSIGNMENT-4
pub_code = pb_cd;
begin
for m_pub in c_pub
loop
dbms_output.put_line(' name : '
|| m_pub.name);

dbms_output.put_line('==============================
====================================');

for m_book in
c_book(m_pub.pub_code)
loop

dbms_output.put_line(m_book.accno || ' ' ||


m_book.title || ' ' || m_book.author || ' '||
m_book.price || ' ' || m_book.pages || ' ' ||
m_book.status || ' ' || m_book.language);

end loop;
end loop;
exception
when cursor_already_open then
dbms_output.put_line('
Page 29
PL SQL ASSIGNMENT-4
cursor is already opened ');
end;
/

15. Write PL/SQL block to display information of book


published by given publisher.

declare
cursor c_book is select * from book
where pub_code = &pub_code;
begin
for m_book in c_book
loop

dbms_output.put_line(m_book.accno || ' ' ||


m_book.title || ' ' || m_book.author || ' ' ||
m_book.pub_code || ' ' || m_book.ven_code || ' ' ||
m_book.price || ' ' || m_book.pages || ' ' ||
m_book.status || ' ' || m_book.language);
end loop;
exception
when cursor_already_open then
dbms_output.put_line('
cursor is already opened ');
Page 30
PL SQL ASSIGNMENT-4
end;
/

Page 31

You might also like