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

Assignment 5 & 6 101715087

DBMS PL/SQL

Uploaded by

Mridul Mahindra
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)
27 views

Assignment 5 & 6 101715087

DBMS PL/SQL

Uploaded by

Mridul Mahindra
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/ 19

A Practical Activity Report For

Database Management Systems (UCS310)

Submitted By:

Name: Mridul Mahindra


Roll Number: 101715087
Group: ENC - 4

Submitted To:
Dr. Sidharth Quamara

Depatment of Computer Science and Engineering

THAPAR INSTITUTE OF ENGINEERING & TECHNOLOGY, (DEEMED TO BE


UNIVERSITY), PATIALA, PUNJAB
Lab Assignment 5

Q1.
SET SERVEROUTPUT ON;
DECLARE
rowCount number(3);
BEGIN
delete from emp where rNo = 101715083;
rowCount := SQL % rowCount;
IF rowCount = 1 THEN
dbms_output.put_line('record deleted successfully');
ELSE
dbms_output.put_line('record was not deleted');
END IF;
END;
/
Output:
Q2.
SET SERVEROUTPUT ON;
DECLARE
--implicit
rowCount number(3);
BEGIN
--implicit
delete from emp where rNo = 101715083;
rowCount := SQL % rowCount;
dbms_output.put_line(rowCount);
END;
/
Output:
Q3.
SET SERVEROUTPUT ON;
DECLARE
v_rNo emp.rNo % type;
v_fullName emp.fullName % type;
v_job emp.job % type;
CURSOR emp_cursor IS select rNo, fullName, job from emp where deptID = 10;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO v_rNo, v_fullName, v_job;
EXIT WHEN emp_cursor % notFound;
dbms_output.put_line(v_rNo || ' : ' || v_fullName || ' : ' || v_job);
END LOOP;
CLOSE emp_cursor;
END;
/
Output:
Q4.
SET SERVEROUTPUT ON;
DECLARE
v_rNo emp.rNo % type;
v_fullName emp.fullName % type;
CURSOR emp_cursor IS select * from (select rNo, fullName from emp order by salary
desc) where rownum <= 5;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO v_rNo, v_fullName;
EXIT WHEN emp_cursor % notFound;
dbms_output.put_line(v_rNo || ' : ' || v_fullName);
END LOOP;
CLOSE emp_cursor;
END;
/Output:
Q5.
SET SERVEROUTPUT ON;
DECLARE
CURSOR emp_cursor IS select rNo, fullName from EMP;
BEGIN
FOR record IN emp_cursor LOOP
dbms_output.put_line(record.rNo || ' : ' || record.fullName);
END LOOP;
END;
/Output:
Q6.
SET SERVEROUTPUT ON;
BEGIN
FOR record IN (select * from (select rNo, fullName from emp order by salary desc) where
rownum <= 5) LOOP
dbms_output.put_line(record.rNo || ' : ' || record.fullName);
END LOOP;
END;
/
Output:
Lab Assignment 6

Q1.
create table CUST (Cno number(10) Primary key, MeterNo number(10) Unique,
Prev_Reading
number(5), Current_Reading number(5), Units number(8), Bill_Amount number(8));
insert into CUST (Cno, MeterNo, Prev_Reading, Current_Reading) values (89, 100, 300,
800);
DECLARE
rec CUST % rowtype;
b number; --bill
u number; --units
extra number;
BEGIN
select * into rec from cust where cno = 89;
u := rec.current_reading - rec.prev_reading;
if u <= 100 then
b := u*0.5;
else
b := 50 + (u-100) * 0.75;
end if;
update cust set units = u, bill_amount = b where cno = 89;
END;
/
select * from CUST;
OUTPUT:
Q2.
create table CUST (Cno number(10) Primary key, MeterNo number(10) Unique,
Prev_Reading
number(5), Current_Reading number(5), Units number(8), Bill_Amount number(8));
insert into CUST (Cno, MeterNo, Prev_Reading, Current_Reading) values (89, 100, 300,
800);
insert into CUST (Cno, MeterNo, Prev_Reading, Current_Reading) values (67, 101, 790,
1000);
insert into CUST (Cno, MeterNo, Prev_Reading, Current_Reading) values (90, 200, 800,
1200);
insert into CUST (Cno, MeterNo, Prev_Reading, Current_Reading) values (62, 789, 200,
800);
insert into CUST (Cno, MeterNo, Prev_Reading, Current_Reading) values (70, 899, 3200,
8700);
insert into CUST (Cno, MeterNo, Prev_Reading, Current_Reading) values (66, 500, 3000,
9800);
DECLARE
meter_number number;
prev_reading number;
current_reading number;
u number; --units
b number; --bill
CURSOR c IS (select MeterNo, Prev_Reading, Current_Reading from cust);
BEGIN
OPEN c;
LOOP
FETCH c INTO meter_number, prev_reading, current_reading;
EXIT WHEN c % notFound;
u := current_reading - prev_reading;
if u <= 100 then
b := u*0.5;
else
b := 50 + (u-100) * 0.75;
end if;
update cust set units = u, bill_amount = b where MeterNo = meter_number;
END LOOP;
CLOSE c;
END;
/
select * from CUST;
OUTPUT:
Q3.
create table temp (emp_id number(10), old_salary number(10), new_salary number(10),
deptID number(5));
insert into temp (emp_id, old_salary, deptID) values (101715083, 10000, 10);
insert into temp (emp_id, old_salary, deptID) values (101715087, 20000, 20);
insert into temp (emp_id, old_salary, deptID) values (101715088, 30000, 30);

DECLARE
cursor cur is select * from temp;
new number;
dept number;
old number;
BEGIN
for instance in cur loop
old := instance.old_salary;
dept := instance.deptID;
if dept = 10 then
new := old + 1000;
elsif dept = 20 then
new := old + 500;
elsif dept = 30 then
new := old + 800;
end if;
update temp set new_salary = new where emp_id = instance.emp_id;
end loop;
END;
/
select * from temp;
OUTPUT:
Q4.
create table student (rno number, marks_subject1 number, marks_subject2 number,
marks_subject3 number, marks_subject4 number, total number, percentage number);
insert into student (rno, marks_subject1, marks_subject2, marks_subject3, marks_subject4)
values (101715083, 80, 90, 70, 85);
insert into student (rno, marks_subject1, marks_subject2, marks_subject3, marks_subject4)
values (101715087, 70, 80, 80, 90);

DECLARE
t student.total % type;
p student.percentage % type;
CURSOR cur IS select * from student;
BEGIN
for rec in cur loop
t := rec.marks_subject1 + rec.marks_subject2 + rec.marks_subject3 +
rec.marks_subject4;
p := t*0.25;
update student set total = t, percentage = p where rno = rec.rno;
end loop;
end;
/
select * from student;
OUTPUT:

Q5.

Yes, the above problem can be solved without using cursors by iterating
through a column in the table using a loop in PL/SQL.
We will have to make an assumption about the data that it can give us a column
which is iterable and we should know the number of rows we need to iterate.
The code is as follows-
create table student (rno number, marks_subject1 number, marks_subject2
number,
marks_subject3 number, marks_subject4 number, total number, percentage
number);
insert into student (rno, marks_subject1, marks_subject2, marks_subject3,
marks_subject4)
values (101715081, 80, 90, 70, 85);
insert into student (rno, marks_subject1, marks_subject2, marks_subject3,
marks_subject4)
values (101715082, 70, 80, 80, 90);
insert into student (rno, marks_subject1, marks_subject2, marks_subject3,
marks_subject4)
values (101715083, 60, 100, 70, 50);
insert into student (rno, marks_subject1, marks_subject2, marks_subject3,
marks_subject4)
values (101715084, 75, 80, 100, 70);
insert into student (rno, marks_subject1, marks_subject2, marks_subject3,
marks_subject4)
values (101715085, 70, 85, 90, 90);

DECLARE
t number(10);
p number(10);
rec student % rowtype;
rollnumber number(10) := 101715081;
BEGIN
LOOP
select * into rec from student where rno = rollnumber;
t := rec.marks_subject1 + rec.marks_subject2 + rec.marks_subject3 +
rec.marks_subject4;
p := t * 0.25;
update student set total = t, percentage = p where rno = rec.rno;
rollnumber := rollnumber + 1;
EXIT WHEN rollnumber > 101715085;
END LOOP;
END;
/
select * from student;

Q6.
create table emp (rNo number(10), fullName varchar(20), job varchar(10),
salary number(10), deptID number(10));
insert into emp values (101715087, 'Mridul', 'boss', 100000, 10);
insert into emp values (101715083, 'Mayank', 'coder', 10000, 20);
insert into emp values (101715088, 'Mukul', 'coder', 50000, 10);
insert into emp values (101715080, 'Mangesh', 'coder', 30000, 20);
insert into emp values (101715081, 'Manik', 'coder', 4000, 10);
insert into emp values (101715082, 'Manish', 'coder', 70000, 20);
insert into emp values (101715086, 'Raina', 'coder', 2000, 10);
DECLARE
CURSOR cur (v number) IS (select rNo, fullName, job from emp where rNo = v);
BEGIN
FOR rec IN cur (101715087) LOOP
dbms_output.put_line(rec.rNo || ' : ' || rec.fullName || ' : ' || rec.job);
END LOOP;
END;
/
OUTPUT:

Q7.
create table emp (rNo number(10), fullName varchar(20), job varchar(10),
salary number(10), deptID number(10));
insert into emp values (101715087, 'Mridul', 'boss', 100000, 10);
insert into emp values (101715083, 'Mayank', 'coder', 10000, 20);
insert into emp values (101715088, 'Mukul', 'coder', 50000, 10);
insert into emp values (101715080, 'Mangesh', 'coder', 30000, 20);
insert into emp values (101715081, 'Manik', 'coder', 4000, 10);
insert into emp values (101715082, 'Manish', 'coder', 70000, 20);
insert into emp values (101715086, 'Raina', 'coder', 2000, 10);
DECLARE
total number(10) := 0;
CURSOR cur (v number) IS (select salary from emp where rowNum <= v);
BEGIN
FOR rec in cur (4) LOOP
total := total + rec.salary;
END LOOP;
dbms_output.put_line(total);
END;
/
OUTPUT:

You might also like