You are on page 1of 14

AU2040162_Madhvendra Jhala DBMS SEC:2

CSE250 Database Management System


Assignment-3
Submission Date : April 5, 2022

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

1. Write a procedure to input any number. Check whether it is prime or not. If it


is not prime, display
its factors.

Soln: create or replace procedure isprime(n int) as


i int:= 1;
c int:=1;
begin
for i in 2..n/2 loop
if mod(n,i) = 0
then
c:=0;
exit;
end if;
end loop;
if c = 1
then
dbms_output.put_line('Prime');
else
dbms_output.put_line('Not Prime');
dbms_output.put_line('Factors: ');
for j in 1..n/2
loop
if mod(n,i) = 0
then
c := 0;
dbms_output.put_line(i);
i := i + 1;
ELSE
i := i + 1;
end if;
end loop;
end if;
end;
/

declare
n int:= &n;
begin
isprime(5);
end;
/

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

2. An insurance company follows the following rules to calculate premium.


a. If a person’s health is excellent and the person is between 25 and 35 years of
age and lives
in a city and is a male then the premium is Rs. 4 per thousand.
b. If a person satisfies all the above conditions except that the gender is female
then the
premium is Rs. 3 per thousand.
c. If a person’s health is poor and the person is between 25 and 35 years of age
and lives in
a village and is a male then the premium is Rs. 6 per thousand.
d. In all other cases the person is not insured.
Write a procedure to input person’s health, age, city and gender; and display the
premium as per
the rules given above.

Soln: create or replace procedure prem_cal(health char, age int, lives char, gender
char) as
premium int;

begin
if(health='excellent' and age>=25 and age<=35 and lives='city' and
gender='Male') then
premium=:4;
dbms_output.put_line('Persons premium is ' || premium');

elseif(health='excellent' and age>=25 and age<=35 and lives='city' and


gender='Female') then
premium=:3;
dbms_output.put_line('Persons premium is ' || premium');

elseif(health='poor' and age>=25 and age<=35 and lives='village' and


gender='Male') then
premium=:6;
dbms_output.put_line('Persons premium is ' || premium');

else
dbms_output.put_line('Person is not insured.');
end if;
end;
/

exec cal_premium('excellent', 32, 'Y', 'M');

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

3. A company makes three products which are codified as class A, class B and class
C. On class A
items for purchase above Rs. 5000, 10% discount is given. For class B for purchase
above
Rs. 8000, a discount of 5% is given. On class C on a purchase of Rs. 10000 and
above, 4%
discount is given. Write a procedure to input product’s class and purchase amount
and display
discount along with the payable amount.
Soln:

create or replace procedure billing(class char, amount int) as


payableamount int;
discount int;
begin
if(class='A' and amount>5000) then
discount:=10;
payableamount := amount *(1 - discount/100);
dbms_output.put_line('Final Bill is:'||payable amount|| 'rs only ');

elseif(class='B' and amount>8000) then


discount:=5;
payableamount := amount *(1 - discount/100);
dbms_output.put_line('Final Bill is:' ||payable amount|| 'rs only ');

elseif(class='C' and amount>10,000) then


discount:=4;
payableamount := amount *(1 - discount/100);
dbms_output.put_line('Final Bill is:'||payable amount|| 'rs only ');

else
dbms_output.put_line('No discount will be provided');

end if;
end;
/
exec disc('A', 6000);
===================================================================================
==================================================================
create or replace procedure disc(class char,purchaseAmount int) as
disc int;
payableAmount int;
begin
if(class = 'A' and purchaseAmount > 5000) then
disc:=10;
payableAmount:= purchaseAmount *(1 - disc/100);
dbms_output.put_line('Disc: ' || disc || '%');
dbms_output.put_line('Payable Amount: ' ||payableAmount ||
'Rs.');
elsif (class = 'B' and purchaseAmount > 8000) then
disc := 5;
payableAmount := purchaseAmount *(1 - disc/100);
dbms_output.put_line('Disc: ' || disc || '%');
dbms_output.put_line('Payable Amount: ' ||payableAmount ||
'Rs.');
elsif (class = 'C' and purchaseAmount >= 10000) then
disc := 4;
payableAmount := purchaseAmount *(1 - disc/100);
dbms_output.put_line('Disc: ' || disc || '%');
dbms_output.put_line('Payable Amount: ' ||payableAmount ||
'Rs.');
else
dbms_output.put_line('No disc');
end if;
end;
/

exec disc('A', 6000);

===================================================================================
==========================================
4. Write a procedure to input marks of three subjects. Assume that marks of each
subject is
obtained out of 50. Calculate percentage. From the following table, check
percentage range and
according to that display the grade.
Percentage Grade
>=0 and <35 F
>=35 and <50 D
>=50 and <65 C
>=65 and <80 B
>=80 A

Soln:

create or replace procedure markings(mark1 int, mark2 int, mark3 int) as


percentage :=(mark1 + mark2 + mark3)* 2/3 int;
grade char;

begin
if
(percentage>=0 and percentage<35) then
grade := 'F';

elseif
(percentage>=35 and percentage<50) then
grade := 'D';

elseif
(percentage>=50 and percentage<65) then
grade := 'D';

else
grade := 'A';
end if;
end;
/
===================================================================================
=======
create or replace procedure grade(physics int,chemistry int, maths int) as
percentage int;
grade char;
begin
if(physics <= 50 and chemistry <= 50 and maths <= 50) then
percentage:= (physics + chemistry + maths) * (100/150);
if(percentage >=0 and percentage < 35) then
grade := 'F';
elsif(percentage < 50) then
grade := 'D';
elsif(percentage < 65) then
grade := 'C';
elsif(percentage < 80) then
grade := 'B';
else
grade := 'A';
end if;
dbms_output.put_line('Grade of student is ' || grade);
else
dbms_output.put_line('Invalid Marks');
end if;

end;
/

exec grade(35,36,37);

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

5. Write a function to input basic_salary of the employee and return net_salary by


applying the
following formulas.
DA=43% of basic_salary
HRA=15% of (basic_salary+DA)
MA=100.00
CLA=240.00
TA=800.00
PF=12% of (basic_salary+DA)
IT=10% of basic_salary
PT : if basic_salary>=0 and <2000 then PT=20.00
if basic_salary>=2000 and <4000 then PT=40.00
if basic_salary>=4000 and <6000 then PT=60.00
if basic_salary>=6000 then PT=100.00
Allowances=DA+HRA+MA+CLA+TA
Deductions=PF+IT+PT
Net_salary=basic_salary+allowances-deductions

Soln:
create or replace function salary(basic_salary int) return int as
net_salary int;
DA int;
HRA int;
MA int;
CLA int;
TA int;
PF int;
IT int;
PT int;
Allowances int;
Deductions int;
begin
DA := 0.43 * basic_salary;
HRA := 0.15 * (basic_salary + DA);
MA := 100;
CLA := 240;
TA := 800;
PF := 0.12 * (basic_salary + DA);
IT := 0.10 * basic_salary;
if (basic_salary >= 0 and basic_salary < 2000) then PT := 20;
elsif (basic_salary < 4000) then PT := 40;
elsif (basic_salary < 6000) then PT := 60;
else PT := 100;
end if;
Allowances := DA + HRA + MA + CLA + TA;
Deductions := PF + IT +PT;
net_salary := basic_salary + Allowances - Deductions;
return net_salary;
end;
/

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

6. Write a function to input person’s age as total no. of days and return age of
that person in years,
months and days. (For ex., if user inputs 3456 then the age should be displayed as
9 years, 5
months and 5 days. Hint: First divide no. with 365 to get years. The quotient will
be no. of years
and reminder should be further divided by 30 to get months. When reminder is
divided with 30,
the quotient will be no. of months and reminder will be no. of days )

Soln:
create or replace function age(ageInDays int) return int as
disp varchar(255);
Years int;
months int;
days int;
temp int;
begin
Years := ageInDays / 365;
temp := REMAINDER(ageInDays, 365);
months := FLOOR(temp/30);
days := temp - (months*30);
disp := 'AGE Years: ' || Years || ' Months: ' || months || ' Days: '
|| days;
return disp;
end;
/

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

7. A computer manufacturing company has the following monthly compensation policy


for their
salespersons.
Basic Salary : 10000.00
Bonus for every computer sold : 500.00
Commission on the total monthly sales : 2%
Write a procedure to input price of one computer and total no. of computers sold by
the
salesperson. Calculate and display total bonus, total commission and gross salary.

Soln: create or replace procedure salesPersonPolicy(pricePerComputer int,


noOfComputers int) as
basic_salary int:= 10000;
bonusPerComputer int:= 500;
monthlyCommission int:= 2;
total_bonus int;
total_commission int;
gross_salary int;
begin
total_bonus := noOfComputers * bonusPerComputer;
total_commission := total_bonus * (monthlyCommission / 100);
gross_salary := basic_salary + total_bonus + total_commission;
dbms_output.put_line('Total Bonus: ' || total_bonus);
dbms_output.put_line('Total Commission: ' || total_commission);
dbms_output.put_line('Gross Salary: ' || gross_salary);
end;
/

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

8. Create a table PERSON with fields first_name, middle_name, last_name, age and
gender.
Person(first_name,middle_name,last_name,age,gender)
Write procedure that will retrieve records from the PERSON table and will display
person’s
name as following four formats. For Ex., if first name is Pooja, middle name is
Harshad and last
name is Shah then it should be displayed as follows.
Pooja Harshad Shah P. H. Shah Shah P.H. Shah Pooja Harshad

Soln:
create table person(
first_name varchar(50),
middle_name varchar(50),
last_name varchar(50),
age int,
gender char(1));

create or replace procedure name as


cursor c_n is select * from person;
r_n c_n%rowtype;
begin
open c_n;
loop
fetch c_n into r_n;
if c_n%notfound then
exit;
end if;
dbms_output.put_line(
r_n.first_name ||' '|| r_n.middle_name ||' '||
r_n.last_name ||' '||
substr(r_n.first_name,1,1) ||'.'||
substr(r_n.middle_name,1,1)||'.'|| r_n.last_name ||' '||
r_n.last_name ||'.'||
substr(r_n.first_name,1,1)||'.'|| substr(r_n.middle_name,1,1) ||' '||
r_n.last_name ||' '|| r_n.first_name ||' '||
r_n.middle_name
);
end loop;
close c_n;
end;
/

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

9. Write a procedure that will read records from the PERSON table. Display all the
names given in
the PERSON table as per the conditions given below. For ex., if first name=Hemal,
middle
name= Harshad, last name=Shah,
a. If gender=female and age >12 then display it as Ms. Hemal Harshad Shah
b. If gender=female and age<=12 then display it as Baby Hemal Harshad Shah
c. If gender=male and age<=12 then display it as Master Hemal Harshad Shah
d. If gender=male and age>12 then display it as Mr. Hemal Harshad Shah

create or replace procedure prefixOfName as


cursor c_person is select * from person;
r_person c_person%rowtype;
begin
open c_person;
loop
fetch c_person into r_person;
if c_person%notfound then
exit;
end if;

if (r_person.gender = 'F' and r_person.age >


12) then
dbms_output.put_line('Ms. ' ||
r_person.first_name ||' '|| r_person.middle_name ||' '|| r_person.last_name);
elsif (r_person.gender = 'F' and r_person.age
<= 12) then
dbms_output.put_line('Baby ' ||
r_person.first_name ||' '|| r_person.middle_name ||' '|| r_person.last_name);
elsif (r_person.gender = 'M' and r_person.age
<= 12) then
dbms_output.put_line('Master ' ||
r_person.first_name ||' '|| r_person.middle_name ||' '|| r_person.last_name);
else
dbms_output.put_line('Mr. ' ||
r_person.first_name ||' '|| r_person.middle_name ||' '|| r_person.last_name);
end if;
end loop;
close c_person;
end;
/

(gender = 'M' and age > 12)

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

10. Write a procedure to display the details of STUDENT and CLASS table.
Class(classcode,classdesc)
Student(stdno,classcode,stdname,birthdate,admissiondate,gender,contact)

Soln:
create table class(
classcode char(50),
classdesc varchar(50));

create table students(


stdno int,
classcode char(50),
stdname varchar(50),
birthdate date,
admissiondate date,
gender char(1),
contact int);

create or replace procedure display as


cursor c_class is select * from class;
cursor c_students is select * from students;
r_class c_class%rowtype;
r_students c_students%rowtype;
begin
open c_class;
open c_students;
loop
fetch c_class into r_class;
fetch c_students into r_students;
if c_class%notfound then
exit;
end if;

if c_students%notfound then
exit;
end if;

dbms_output.put_line(r_class.classcode ||' '||


r_class.classdesc);
dbms_output.put_line(r_students.stdno ||' '||
r_students.classcode
||' '||
r_students.stdname
||' '||
r_students.birthdate
||' '||

r_students.admissiondate ||' '||


r_students.gender ||'
'||
r_students.contact);
end loop;
close c_students;
close c_class;
end;
/
===================================================================================
======================================

11. Create a procedure to display employee name, salary and joining date from the
EMPLOYEE
table. Also, display total no. of years of experience of each employee from the
joining_date field
and retirement date of each employee from the birth_date field.

Soln: create table employee(


empno int,
deptno int,
empname varchar(50),
salary int,
birth_date date,
joining_date date);

create or replace procedure displayEmployee as


cursor c_emp is select * from employee;
r_emp c_emp%rowtype;
begin
open c_emp;
loop
fetch c_emp into r_emp;

if c_emp%notfound then
exit;
end if;

dbms_output.put_line(r_emp.empname ||' '||


r_emp.salary ||' '||
r_emp.joining_date
||' '||

round(months_between(sysdate,r_emp.joining_date)/12,0) ||' '||

add_months(r_emp.birth_date, 60*12)
);
end loop;
close c_emp;
end;
/

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

12. Create a procedure which will display department wise employee details in
ascending order of
department name and descending order of employee names within the department.
Department(deptno, deptname)
Employee(empno, deptno, empname, salary, birth_date, joining_date)

Soln:
create table depart(deptno int, deptname varchar(50));
create or replace procedure displayDepEmp as
cursor c_emp is select *
from employee e
join depart d
on e.deptno = d.deptno
group by d.deptno
order by d.deptname, e.empname;

r_emp c_emp%rowtype;
begin
open c_emp;
loop
fetch c_emp into r_emp;

if c_emp%notfound then
exit;
end if;

dbms_output.put_line(r_emp.empname ||' '||


r_emp.salary ||' '||
r_emp.joining_date
||' '||

round(months_between(sysdate,r_emp.joining_date)/12,0) ||' '||

add_months(r_emp.birth_date, 60*12)
);
end loop;
close c_emp;
end;
/

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

13. Write a pl/sql block to update salary of employees as per the rules given
below.
If total no. of years of experience is below 5 years, increase the salary by 5%
If total no. of years of experience is between 5 and 10 years, increase the salary
by 8%,
If total no. of years of experience is between 10 and 15, increase the salary by
10%,
In all other cases, increase the salary by 13%

Soln:
create or replace procedure updateSalary as
cursor c_emp is select * from employee;
r_emp c_emp%rowtype;
exp int:= round(months_between(sysdate,r_emp.joining_date)/12,0);
begin
open c_emp;
loop
fetch c_emp into r_emp;

if c_emp%notfound then
exit;
end if;

if (exp < 5) then


update employee
set salary = salary * (1 + 0.05)
where exp < 5;
elsif (exp >= 5 and exp < 10) then
update employee
set salary = salary * (1 + 0.08)
where exp >= 5 and exp < 10;
elsif (exp >= 10 and exp < 15) then
update employee
set salary = salary * (1 + 0.10)
where exp >= 10 and exp < 15;
else
update employee
set salary = salary * (1 + 0.13)
where exp >= 15;
end if;
end loop;
close c_emp;
end;
/

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

14. Create a function which will take student name as a parameter and will return
name of the subject
in which he/she got highest marks. Use this table : Student(Id, name, DBMS_marks,
DM_marks,
DS_marks, FOP_marks, OOP_marks)

Soln:
create table Std(
Id int,
name varchar(50),
DBMS_marks int,
DM_marks int,
DS_marks int,
FOP_marks int,
OOP_marks int);

create or replace function maxMarks(name varchar) return int as


cursor c_std is select * from Std;
r_std c_std%rowtype;
maxMarks int := 0;
begin

open c_std;
loop
fetch c_std into r_std;

if c_std%notfound then
exit;
end if;

if (maxMarks < r_std.DBMS_marks) then


maxMarks := r_std.DBMS_marks;
elsif (maxMarks < r_std.DM_marks) then
maxMarks := r_std.DM_marks;
elsif (maxMarks < r_std.DS_marks) then
maxMarks := r_std.DS_marks;
elsif (maxMarks < r_std.FOP_marks) then
maxMarks := r_std.FOP_marks;
else
maxMarks := r_std.OOP_marks;
end if;

end loop;
close c_std;

return maxMarks;
end;
/

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

15. Create a procedure to display names of each student and the subject in which
he/she has obtained
highest marks along with the marks. Use this table : Student(Id, name, DBMS_marks,
DM_marks, DS_marks, FOP_marks, OOP_marks)

Soln:
create or replace procedure stdinfomark as
cursor c_np is select * from Std;
r_np c_np%rowtype;

begin
open c_np;
loop
fetch c_np into r_np;

if c_np%notfound then
exit;
end if;

if (r_np.dbms_marks>=r_np.dm_marks and r_np.dbms_marks>=r_np.ds_marks


and r_np.dbms_marks>=r_np.fop_marks and r_np.dbms_marks>=r_np.oop_marks) then
stdinfomark_output.put_line(r_np.name||' DBMS: '||
r_np.dbms_marks);
elsif (r_np.dm_marks>=r_np.dbms_marks and r_np.dm_marks>=r_np.ds_marks
and r_np.dm_marks>=r_np.fop_marks and r_np.dm_marks>=r_np.oop_marks) then
stdinfomark_output.put_line(r_np.name||' DM: '||
r_np.dm_marks);
elsif (r_np.ds_marks>=r_np.dbms_marks and r_np.ds_marks>=r_np.dm_marks
and r_np.ds_marks>=r_np.fop_marks and r_np.ds_marks>=r_np.oop_marks) then
stdinfomark_output.put_line(r_np.name||' DS: '||
r_np.ds_marks);
elsif (r_np.fop_marks>=r_np.dbms_marks and
r_np.fop_marks>=r_np.dm_marks and r_np.fop_marks>=r_np.ds_marks and
r_np.fop_marks>=r_np.oop_marks) then
stdinfomark_output.put_line(r_np.name||' FOP: '||
r_np.fop_marks);
elsif (r_np.oop_marks>=r_np.dbms_marks and
r_np.oop_marks>=r_np.dm_marks and r_np.oop_marks>=r_np.ds_marks and
r_np.oop_marks>=r_np.fop_marks) then
stdinfomark_output.put_line(r_np.name||' OOP: '||
r_np.oop_marks);
else
stdinfomark_output.put_line('...');
end if;

end loop;
close c_np;
end;
/

***********************************************************************************
*******************************************************************

You might also like