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

Lab Cycle 3 Solutions

1. The document contains solutions to 7 lab cycle problems involving PL/SQL programming. The problems cover topics like printing perfect numbers, inventory management, calculating HCF and LCM, handling exceptions, validating employee dates of birth, and deleting unenrolled courses from a table. 2. For each problem, the PL/SQL code solution is provided along with sample table schemas and outputs showing the results of running the code. Built-in functions, exceptions, loops, conditions and DML statements are used in the solutions. 3. The problems progress from basic to more advanced concepts like exception handling, validation, and deleting related records from multiple tables.

Uploaded by

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

Lab Cycle 3 Solutions

1. The document contains solutions to 7 lab cycle problems involving PL/SQL programming. The problems cover topics like printing perfect numbers, inventory management, calculating HCF and LCM, handling exceptions, validating employee dates of birth, and deleting unenrolled courses from a table. 2. For each problem, the PL/SQL code solution is provided along with sample table schemas and outputs showing the results of running the code. Built-in functions, exceptions, loops, conditions and DML statements are used in the solutions. 3. The problems progress from basic to more advanced concepts like exception handling, validation, and deleting related records from multiple tables.

Uploaded by

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

Lab Cycle 3 Solutions

1. Write a PL/SQL program to print the first n perfect numbers.

declare
n number:=&n;
i number;
j number;
s number;

begin
for i in 1 .. n loop
s:=0;
for j in 1..(i-1) loop
if(mod(i,j)=0) then
s:=s+j;
end if;
end loop;
if(s=i) then
dbms_output.put_line(i);
end if;
end loop;
end;

Output:
SQL> @Z:\PLSQL\perfect.sql
Input truncated to 1 characters
Enter value for n: 500
old 2: n number:=&n;
new 2: n number:=500;
6
28
496

PL/SQL procedure successfully completed.


2. Consider the following table. If quantity of an item is less than its ROL(Reorder
Level), then the item should be reordered. Write a PL/Sql block that notifies
the number of items to be reordered in the Inventory table.

INVENTORY
Item_ID Item_nam Quantity ROL ROQ
e

declare
id inventory.item_id%type := '&id';
q inventory.quantity%type;
rlinventory.rol%type;
rqinventory.roq%type;
n number;
begin
select quantity,rol,roq into q,rl,rq from inventory where item_id = id;
if q <rl then
n:=trunc((rl-q)/rq);
dbms_output.put_line('quantity is less than reorder level, place'||
n||' items');
else
dbms_output.put_line('quantity is sufficient');
end if;
end;
/

Output:

ITEM_ID ITEM_NAME QUANTITY ROL ROQ


---------- ------------------- -------------- ---------- ----------
I101 Pen 50 100 30
I102 Pencil 150 100 40
I103 Eraser 40 60 20
I104 Box 140 120 50

SQL> @ Z:\PLSQL\inv1.sql;
Enter value for id: I101
old 2: id inventory.item_id%type := '&id';
new 2: id inventory.item_id%type := 'I101';

quantity is less than reorder level, place 1 items

PL/SQL procedure successfully completed.

SQL> @ Z:\PLSQL\inv1.sql;
Enter value for id: I102
old 2: id inventory.item_id%type := '&id';
new 2: id inventory.item_id%type := 'I102';

quantity is sufficient

PL/SQL procedure successfully completed.

3. Write a PL/SQL program to read two numbers, calculate their HCF and LCM
and store the details in the numbers table.

NUMBERS
Number Number HCF LCM
1 2

declare
a number := &a;
b number := &b;
g number;
l number;
a1 number;
b1 number;
begin
a1:=a;
b1:=b;
while (a <> b) loop
if (a < b) then
b := b - a;
else
a := a - b;
end if;
end loop;
g := a;
l:= (a1*b1)/g;
insert into demo_tab values(a1,b1,l,g);
end;

Output:

SQL> @prg1.sql;
22 /
Enter value for a: 4
old 2: a number := &a;
new 2: a number := 4;
Enter value for b: 8
old 3: b number := &b;
new 3: b number := 8;

PL/SQL procedure successfully completed.

SQL> select * from numbers;

N1 N2 LCM HCF
---------- ---------- ---------- ----------
4 8 8 4

4. Consider the following relation schemas


Inventory
Product_I Product_nam
Quantity
D e

Purchase_Record
Product_I Statu
Date
D s
Write a PL/SQL block to read the quantity of a product from inventory and
if it is > 0 reduce the quantity by 1 and record the status of purchase of that
product as ‘PURCHASED’. Otherwise record the status of purchase of that
product as ‘OUT OF STOCK’. While recording the status of a purchase,
record the date of transaction.
declare
id inven.pid%type := &id;
q inven.qty%type;
begin
select qty into q from inven where pid = id;
if q > 0 then
update inven set qty = qty - 1 where pid = id;
insert into pur_rec values(id,'PURCHASED',sysdate);
else
insert into pur_rec values(id,'OUTOFSTOCK',sysdate);
end if;
end;

output:
SQL>select * from invent;

PRODUCT_ID PRODUCT_NAME QUANTITY


---------- --------------- ----------
1 mobile 35
2 laptop 50
3 bottle 45
4 bags -10
SQL> @inven1.sql;
13 /
Enter value for id: 1
old 2: id inven.product_id%type := &id;
new 2: id inven.product_id%type := 1;

PL/SQL procedure successfully completed.


SQL> select * from inven;

PRODUCT_ID PRODUCT_NAME QUANTITY


---------- --------------- ----------
1 mobile 34
2 laptop 50
3 bottle 45
4 bags -10
SQL> select * from purchase_record;

PRODUCT_ID STATUS PDATE


---------- -------------------- ---------
1 PURCHASED 17-MAR-20

SQL> @C:\Users\Administrator\Desktop\inven1.sql;
13 /
Enter value for id: 4
old 2: id inven.product_id%type := &id;
new 2: id inven.product_id%type := 4;

PL/SQL procedure successfully completed.

SQL> select * from purchase_record;

PRODUCT_ID STATUS PDATE


---------- -------------------- ---------
1 PURCHASED 17-MAR-20
4 OUTOFSTOCK 17-MAR-20

5. Write a PL/SQL block to handle the following built-in exceptions


 no_data_found
 too_many_rows
 zero_divide

declare
s employee.ssn%type ;
n number := 10;
m number;
begin
select ssn into s from employee;
m := n/0;
select ssn into s from employee where dno = 100;
exception
when too_many_rows then
dbms_output.put_line('too many rows returned where only 1 is expected');
when zero_divide then
dbms_output.put_line('attempting to divide by zero');
when no_data_found then
dbms_output.put_line('no such department exists');
end;
SQL> @p34;
14 /
too many rows returned where only 1 is expected

PL/SQL procedure successfully completed.

SQL> @p34;
14 /
attempting to divide by zero

PL/SQL procedure successfully completed.


SQL> @p34;
18 /
no such department exists

PL/SQL procedure successfully completed.

6. Write a PL/SQL block to validate the birth date of an employee in the


following emp table. If the date of birth of any employee is found to be
invalid, using an exception display relevant message and update it with new
birth date.
Emp
Eno Name Birthdate
Hint: If calculated age of employee is less than 22 or greater than 60 then
birthdate is invalid.
declare
no number:=&no;
navarchar(20);
d date;
age number;
age_exp exception;
begin
select name,bdate into na,d
from employ where empid=no;
age:=trunc((sysdate-d)/365);
if age<22 or age>60 then
raise age_exp;
else
dbms_output.put_line('valid birthdate');
end if;
exception
when age_exp then
dbms_output.put_line('invalid birthdate');
update employ set bdate='10-MAR-1965' where empid=no;
end;

Output:
SQL> select * from employ;

NAME EMPID SALARY BDATE


-------------------- ---------- ---------- ---------
sasi 103 5000 10-JUN-55
Latha 113 6000 10-JUN-20
Anu 101 6720 10-MAR-65
Divya 104 7890 10-JUN-60

SQL> @birth.sql;
22 /
Enter value for no: 103
old 2: no number:=&no;
new 2: no number:=103;
invalid birthdate

PL/SQL procedure successfully completed.


SQL> select * from employ;

NAME EMPID SALARY BDATE


-------------------- ---------- ---------- ---------
sasi 103 5000 10-MAR-65
Latha 113 6000 10-JUN-20
Anu 101 6720 10-MAR-65
Divya 104 7890 10-JUN-60

SQL> @birth.sql;
22 /
Enter value for no: 104
old 2: no number:=&no;
new 2: no number:=104;
valid birthdate

PL/SQL procedure successfully completed.

7. Consider the following relation schemas


Course
Coursei
name Offering_deptno
d

Enrollment
Student_I Coursei
Instructor_name
d d

Del_Audit
CourseI
Rows_deleted date
d
Write a PL/SQL block to delete records of all courses for which no
student is enrolled and then record the Course ID, no of rows deleted
and date on which deletion occurred in the Del_Audit table.

Output:
SQL> select * from course38;
COURSEID NAME OFFERING_DEPTNO
---------- -------------------- ---------------
CS221 CO 4
MATH101 DMS 2
CS223 DBMS 4
CS224 OS 3
CS225 CO 5
MATH101 DMS 3

6 rows selected.

SQL> select * from enrollment38;

STUDENT_ID COURSEID INSTRUCTOR_NAME


---------- ---------- --------------------
S201 CS221 Sairam
S202 CS223 Srinivasa
S204 CS224 Anu
S203 CS223 Srinivasa

SQL> select * from enrollment38;

STUDENT_ID COURSEID INSTRUCTOR_NAME


---------- ---------- --------------------
S201 CS221 Sairam
S202 CS223 Srinivasa
S204 CS224 Anu
S203 CS223 Srinivasa

SQL> @course1.sql;
15 /
CS225 1
MATH101 2

PL/SQL procedure successfully completed.


SQL> select * from course38;

COURSEID NAME OFFERING_DEPTNO


---------- -------------------- ---------------
CS221 CO 4
CS223 DBMS 4
CS224 OS 3

SQL> select * from del_audit;

CID ROWSDEL DDATE


---------- ---------- ---------
CS225 1 17-MAR-20
MATH101 2 17-MAR-20

8. Write a PL/SQL block that displays the employee names and their total
earnings from the Emp Table.
Hint: Total earning of an employee = 12*(gross_salary+commission)

declare
s empcy2.sal%type;
name empcy2.ename%type;
c empcy2.comm%type;
cursor emp_cr is select ename,sal,comm from empcy2;
begin
open emp_cr;
loop

fetch emp_cr into name,s,c;


exit when emp_cr%notfound;
s:=12*(s+nvl(c,0));
dbms_output.put_line(name||' '||s);
end loop;
end;

Output:
SQL>select * from empcy2;
SQL> /

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7839 KING PRESIDENT 17-NOV-81 5000 10
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7900 JAMES CLERK 7698 03-DEC-81 950 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7369 SMITH CLERK 7902 17-DEC-80 800 20

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7788 SCOTT ANALYST 7566 09-DEC-82 3200 20
7876 ADAMS CLERK 7788 12-JAN-83 1100 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10

14 rows selected.
SQL> @employ1.sql;
16 /
KING 60000
BLAKE 34200
CLARK 29400
JONES 35700
MARTIN 31800
ALLEN 22800
TURNER 18000
JAMES 11400
WARD 21000
FORD 36000
SMITH 9600
SCOTT 38400
ADAMS 13200
MILLER 15600

PL/SQL procedure successfully completed.


9. Consider the following customer table:

Cust_ID Name BirthDate Sex AnnualIncom Rating


e
C111 Alex 20-Aug-98 Male Above 100K 3
C112 Wayne 15-Nov-85 Male 50K-100K 2
C234 Tom 03-Jan-79 Male Below 50K 1

Update the Rating of customers based on the following criteria:


 If Annual_Income> 100k then rating = 1
 Annual_Income is 50k – 100k then rating = 2 and
 Annual Income < 50k then rating =3.
Show the status of updation in the console. If no records are updated,
Display the message “NO RECORDS ARE UPDATED”. Otherwise Display the
count ofrecords updated.

SQL> select * from customer1;

CUST_ID NAME BIRTHDATE SEX ANNUALINCOME RATING


---------- ---------- --------- ----- -------------------- ----------
C111 Alex 20-AUG-98 Male 3
C234 Tom 03-JAN-79 Male 1
C112 Wayne 15-NOV-85 Male 2

begin
update customer1 set rating=decode(annualincome,'Above
100K',1,'50K-100K',2,3);
end;

SQL> @C:\Users\Administrator\Desktop\rating1.sql;
4 /

PL/SQL procedure successfully completed.

SQL> select * from customer1;

CUST_ID NAME BIRTHDATE SEX ANNUALINCOME RATING


---------- ---------- --------- ----- --------------- ----------
C111 Alex 20-AUG-98 Male Above 100K 1
C234 Tom 03-JAN-79 Male Below 50K 3
C112 Wayne 15-NOV-85 Male 50K-100K 2

You might also like