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

Subject Notes Front Page -DBMS-Lab

DBMS LAB MANUAL 3RD BCA NEP

Uploaded by

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

Subject Notes Front Page -DBMS-Lab

DBMS LAB MANUAL 3RD BCA NEP

Uploaded by

rupasandhyam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

HENNUR-BAGALUR MAIN ROAD, KANNUR POST, BENGALURU-562149

Faculty of Computer Applications

21BCA3C7L- DBMS Lab Manual

III Semester BCA

Prepared By,
Sandhya M
Faculty of Computer Applications
Part A

1. The Book detail databases has a table with the following attributes. books(book_title: varchar2,
book_author: varchar2, book_price: number) Execute a Single line query and group functions.
i) Create the above table.
ii) Enter five tuples into the table.
iii) Display all the tuples in books table.
iv) Use group by functions (MAX,MIN)
v) Use group by functions SUM

i)Create table books


(
book_title varchar2(20),
book_author varchar2(20),
book_price number(10)
);

ii) Insert into books values('DBMS','Ashwini',200);


Insert into books values('C','Srikanth',300);
Insert into books values('Java','Harish',400);
Insert into books values('DBMS','Chithra',500);
Insert into books values('Java lab','Harish',100);

iii) select * from books;

book_title book_author book_price


-------------- -------------------- ----------
DBMS Ashwini 200
C Srikanth 300
Java Harish 400
DBMS Chithra 500
Java lab Harish 100

iv) select book_title,max(book_price) from books group by book_title;

book_title max(book_price)
------------------ ---------------
DBMS 500
Java 400
C 300
DBMS 200
Java lab 100
 select max(book_price) from books group by book_price ;

max(book_price)
-----------------------
500
400
300
200
100
 select book_title, min(book_price) from books group by book_title;

book_title min(book_price)
--------------- ---------------
Java lab 100
DBMS 200
C 300
Java 400
DBMS 500

 select min(book_price) book_price from books;

book_price
----------
100

v) Select sum(book_price) book_price from books;

book_price
----------
1500
----------------------------------------------------------------------------------------------------------------------------- ----------
--------------------------------------------------------------------------------------------------

2. The STUDENT detail databases has a table with the following attributes. The primary keys are
underlined. STUDENT (regno: int, name: string, dob: date, marks: int) Using DDL Commands.

i) Create the above table.


ii) Remove the existing attribute marks from the table.
iii) Change the date type of regno from integer to string.
iv) Add a new attribute phoneno to the existing table.
v) Enter five tuples into the table.
vi) Display all the tuples in student table.
vii)Delete a table.

i) Create table student


(
regno number(10) primary key ,
name varchar(20),
dob date,
marks number(10)
);

 desc student;

Name Null? Type


----------------------------------------- -------- -------------------------
---
REGNO NOT NULL NUMBER(10)
NAME VARCHAR2(20)
DOB DATE
MARKS NUMBER(10)

ii)Alter table student drop column marks;

 desc student;

Name Null? Type


----------------------------------------- -------- ----------------------------
REGNO NOT NULL NUMBER(10)
NAME VARCHAR2(20)
DOB DATE

iii)Alter table student modify regno varchar2(20);


 desc student;

Name Null? Type

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


-
REGNO NOT NULL VARCHAR2(20)
NAME VARCHAR2(20)
DOB DATE
iv)Alter table student add (marks number(10), phoneno number(10));

 desc student;

Name Null? Type


----------------------------------------- -------- ----------------------------
REGNO NOT NULL VARCHAR2(20)
NAME VARCHAR2(20)
DOB DATE
MARKS NUMBER(10)
PHONENO NUMBER(10)

 Insert into student values(‘21AIC01’,’Arun’,’19-dec-1995’,97,8105555566);

 Insert into student values(‘21AIC02’,’Vinu’,’22-oct-1994’,95,9105655566);


 Insert into student values(‘21AIC03’,’Rose’,’16-dec-1995’,78,8805555596);

 Insert into student values(‘21AIC04’,’Sharely’,’08-jul-1994’,92,8105455556);

 Insert into student values(‘21AIC05’,’Maga’,’08-dec-1995’,99,8105555966);

vi)Select * from student;

REGNO NAME DOB MARKS PHONENO


-------------------- -------------------- --------- ---------- ----------------------
21AIC01 Arun 19-DEC-95 97 8105555566
21AIC02 Vinu 22-OCT-94 95 9105655566
21AIC03 Rose 16-DEC-95 78 8805555596
21AIC04 Sharely 08-JUL-94 92 8105455556
21AIC05 Maga 08-DEC-95 99 8105555966

vii) Drop table student;

Table dropped.
----------------------------------------------------------------------------------------------------------------------------- -------------

3) The Employee database of an organization has a table with the following


attributes.Employee(empcod:int, empname:string, dob:date, department:string, salary:real) using DML
commands.

 Create the above table.

 Enter the five tuples into the table

 Display all the tuples in Employee table.

 Increase the salary of those employees working for the computer department by Rs.5000

 Delete employees from table.

i)Create table Employee


(
empcode number(10) primary key,
empname varchar2(20),
dob date,
department varchar2(15),
salary number(10,2)
);

 Insert into Employee values(001,’Arun’,’19-dec-2019’,’BCA’,30000);


 Insert into Employee values(002,’Raju’,’23-oct-2021’,’BCOM’,25000);
 Insert into Employee values(003,’Suganthi’,’02-jan-2020’,’BCA’,40000);
 Insert into Employee values(004,’Anu’,’16-dec-2020’,’BBA’,32000);
 Insert into Employee values(005,’Victor’,’19-dec-2019’,’BBA’,20000);

iii) Select * from Employee;

EMPCODE EMPNAME DOB DEPARTMENT SALARY


---------- -------------------- --------- --------------- -------------- ------------
1 Arun 19-DEC-19 BCA 30000
2 Raju 23-OCT-21 BCOM 25000
3 Suganthi 02-JAN-20 BCA 40000
4 Anu 16-DEC-20 BBA 32000
5 Victor 19-DEC-19 BBA 20000
iv) Update Employee set salary=salary+5000 where department=’BCA’;

Select * from Employee;

EMPCODE EMPNAME DOB DEPARTMENT SALARY


---------- -------------------- --------- --------------- ---------- -----------------
1 Arun 19-DEC-19 BCA 35000
2 Raju 23-OCT-21 BCOM 25000
3 Suganthi 02-JAN-20 BCA 45000
4 Anu 16-DEC-20 BBA 32000
5 Victor 19-DEC-19 BBA 20000

v) Delete from Employee where empcode = 4;

Select * from Employee;

EMPCODE EMPNAME DOB DEPARTMENT SALARY


---------- -------------------- --------- --------------- ---------- --------------------
1 Arun 19-DEC-19 BCA 35000
2 Raju 23-OCT-21 BCOM 25000
3 Suganthi 02-JAN-20 BCA 45000
5 Victor 19-DEC-19 BBA 20000

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

4. A Library database has a table with the following attributes.


Library(Bookid:int, Title:string, Author:string, Publishers:string, Yearpub:int, Price:real)
Execute DCL and TCL Commands.

i) Create the above table.

ii) Enter the five tuples into the table

iii) Display all the tuples in Library table.


iv) Display the different Publishers from the list.

v) Arrange the tuples in the alphabetical order of the Title.

vi) Execute DCL and TCL Commands.

vii) List the details of all the books whose price ranges between Rs. 100 and Rs. 300

i) Create table Library


(
Bookid number(10) primary key,
Title varchar2(20),
Author varchar2(20),
Publishers varchar2(20),
year number(5),
price number(6,2)
);

ii) Insert into Library values (111,’Maths’,’Atre’,’Himalaya’,2009,250.00);


Insert into Library values (112,’Physics’,’Sahu’,’Subhas’,2010,300.00);
Insert into Library values (113,’Computer’,’Rama’,’Subhas’,2010,220.00);
Insert into Library values (114,’Chemistry’,’Bosco’,’Himalaya’,2009,275.00);
Insert into Library values (115,’Physics’,’Ranganath’,’MES’,2009,250.00);

iii) Select * from Library;

BOOKID TITLE AUTHOR PUBLISHERS YEAR PRICE


------------------- --------------------------- ------------------------- ---------------- ------------
111 Maths Atre Himalaya 2009 250

112 Physics Sahu Subhas 2010 300

113 Computer Rama Subhas 2010 220

114 Chemistry Bosco Himalaya 2009 275

115 Physics Ranganath MES 2009 250

iv) Select distinct Publishers from Library;

PUBLISHERS
--------------------
MES
Subhas
Himalaya

v) Select * from Library order by Title asc;

BOOKID TITLE AUTHOR PUBLISHERS YEAR PRICE


----------------- ------------------- ------------------- ---------------------- -------------------- -----------
--
114 Chemistry Bosco Himalaya 2009 275

113 Computer Rama Subhas 2010 220

111 Maths Atre Himalaya 2009 250

115 Physics Ranganath MES 2009 250

112 Physics Sahu Subhas 2010 300

vi) commit;

 savepoint mysave;

Insert into Library values (116,’Sociology’,’Ranganath’,’MES’,2009,250.00);


Insert into Library values (117,’Biology’,’Ranganath’,’MES’,2009,250.00);

 Select * from Library;

 Rollback to mysave;

 select * from Library;

 grant select,update on Library to SYSTEM;

viii) Select * from Library where Price between 250 and 275;

BOOKID TITLE AUTHOR PUBLISHERS YEAR PRICE


-------------- -------------- ------------------- ------------------------- ------------ ------------
111 Maths Atre Himalaya 2009 250

114 Chemistry Bosco Himalaya 2009 275

115 Physics Ranganath MES 2009 250

-----------------------------------------------------------------------------------------------------------------------
5) The Products database has a table with the following attributes. Products(Product_id :number,
Product_name: varchar2, Item_price :number) Implement the nested queries.

i) Create the above table.


ii) Enter five tuples into the table.
iii) Display all the tuples in Products table.
iv) Use max functions to find the Maximum product price.
v) Use where clause to find the Maximum product price.
vi) Use nested queries to find the Maximum product price.
vii)Use nested queries to find the Minimum product price.

i)Create table Products


(
Product_id number,
Product_name varchar2(20),
Item_price number(6,2)
);

ii)Insert into Products values(111,’Mouse’, 400);

Insert into Products values(112,’VGA Caple’, 350);

Insert into Products values(113,’LAN Caple’, 500);

Insert into Products values(114,’Keyboard’, 475);

Insert into Products values(115,’RAM’, 800);

iii)Select * from Products;

PRODUCT_ID PRODUCT_NAME ITEM_PRICE


---------- -------------------- ---------- ------------------------
111 Mouse 400
112 VGA Caple 350
113 LAN Caple 500
114 Keyboard 475
115 RAM 800

iv)Select max(Item_price) from Products;


MAX(ITEM_PRICE)
---------------
800

v)Select Product_id, Product_name, Item_price from Products where Item_price =800;

PRODUCT_ID PRODUCT_NAME ITEM_PRICE


---------- -------------------- ---------- ------------------------------
115 RAM 800

vi)Select Product_id, Product_name, Item_price


from Products
where Item_price =(Select max(Item_price) from Products);

PRODUCT_ID PRODUCT_NAME ITEM_PRICE


---------- - ------------------- ----------
115 RAM 800

vii)Select Product_id, Product_name, Item_price


from Products
where Item_price =(Select min(Item_price) from Products);

PRODUCT_ID PRODUCT_NAME ITEM_PRICE


---------- -------------------- ----------
112 VGA Caple 350

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

6. The Student detail databases has a table with the following attributes. The primary keys are underlined.
Student (id:number, name: varchar2, dob: date).
The Marks detail databases has a table with the following attributes. The primary keys are underlined.
Marks(id:number,DBMS:number,C#:number,English:number,CCN:number).Implement join operations
in SQL.

i) Create the above tables.


ii) Enter five tuples into the tables.
iii) Display all the tuples in the tables.
iv) Join both the tables using inner join.
v) join certain columns using outer join.
i)Create table Student
(
id number(10) primary key,
name Varchar2(20),
dob date
);

Create table Marks


(
id number(10) primary key,
DBMS number(5),
C# number(5),
English number(5),
CCN number(5)
);

ii) Insert into Student values(11,’Ram’, ‘19-DEC-19’);


Insert into Student values(‘12’,’Arun’,’19-dec-1995’);
Insert into Student values(‘13’,’Vinu’,’22-oct-1994’);
Insert into Student values(‘14’,’Rose’,’16-dec-1995’);
Insert into Student values(‘15’,’Sharely’,’08-jul-1994’);

Insert into Marks values(11,50,65,70,55);


Insert into Marks values(12,80,65,90,55);
Insert into Marks values(13,70,65,40,75);
Insert into Marks values(14,50,65,70,55);
Insert into Marks values(15,60,85,70,45);

iii)select * from Student;


ID NAME DOB
---------- -------------------- ---------
11 Ram 19-DEC-19
12 Arun 19-DEC-95
13 Vinu 22-OCT-94
14 Rose 16-DEC-95
15 Sharely 08-JUL-94

 select * from Marks;

ID DBMS C# ENGLISH CCN


---------- ---------- ---------- ---------- ----------
11 50 65 70 55
12 80 65 90 55
13 70 65 40 75
14 50 65 70 55
15 60 85 70 45

iv)Select * from Student inner join Marks on Marks.id=Student.id;

ID NAME DOB ID DBMS C# ENGLISH CCN


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

11 Ram 19-DEC-19 11 50 65 70 55

12 Arun 19-DEC-95 12 80 65 90 55

13 Vinu 22-OCT-94 13 70 65 40 75

14 Rose 16-DEC-95 14 50 65 70 55

15 Sharely 08-JUL-94 15 60 85 70 45

v)Select Marks.id,Marks.DBMS,Marks.CCN,Student.name from Student right outer join Marks on


Student.id=Marks.id;
ID DBMS CCN NAME
---------- ---------- ---------- --------------------
11 50 55 Ram
12 80 55 Arun
13 70 75 Vinu
14 50 55 Rose
15 60 45 Sharely

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

7. The Emp detail databases has a table with the following attributes. The primary keys are underlined.
Emp (Empid:number, Empname: varchar2, Deptno:number,salary:number).
The Dept detail databases has a table with the following attributes. The primary keys are underlined.Dept
(Deptno:number,Deptname:varchar2,).create foreign key for Deptno.Create views for particular table.

i) Create the above tables.


ii) Enter three tuples into the tables.
iii) Display all the tuples in the tables.
iv) Create views for a particular table.
v) Display all the tuples in the created view table.

i) Create table Dept


(
Deptno number(10) not null,
Deptname varchar2(10) not null,
CONSTRAINT Dept_pk PRIMARY KEY(Deptno)
);

Create table Emp


(
Empid number(10) not null,
Empname varchar2(20) not null,
Deptno number(10),
salary number(6),
CONSTRAINT Emp_pk PRIMARY KEY (Empid),
CONSTRAINT fk_Dept FOREIGN KEY(Deptno) REFERENCES Dept(Deptno)
);

ii)Insert into Dept values(111,’BCA’);


Insert into Dept values(112,’BCOM’);
Insert into Dept values(113,’BBA’);

Insert into Emp values(10,’Rahul’,111,20000);


Insert into Emp values(11,’Anu’,112,30000);
Insert into Emp values(12,’Ranjith’,113,25000);

iii)Select * from Dept;


DEPTNO DEPTNAME
---------- ----------
111 BCA
112 BCOM
113 BBA

 Select * from Emp;

EMPID EMPNAME DEPTNO SALARY


---------- -------------------- ---------- ----------
10 Rahul 111 20000
11 Anu 112 30000
12 Ranjith 113 25000

iv)Create view empdept_view as


Select e. Empid,e. Empname,e.Deptno,d.Deptname from Emp e,Dept d where e.Deptno=d.Deptno;

v)Select * from empdept_view;

EMPID EMPNAME DEPTNO DEPTNAME


---------- -------------------- ---------- ---------- --------------------
10 Rahul 111 BCA
11 Anu 112 BCOM
12 Ranjith 113 BBA

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

8.Implement Locks for a particular table.

Connect /as sysdba;

Create user:

Create user username identified by password;


Ex. Create user sampleuser identified by sample;

Grant all privileges to username;


Ex. Grant all privileges to sampleuser;

Create table salarydb


(
empcode number(10) primary key,
empname varchar2(20),
dept varchar2(15),
salary number(10)
);
lock table salarydb in exclusive mode nowait;

Table(s) Locked.
----------------------------------------------------------------------------------------------------------------------------- ------

9. The SALARY database of an organization has a table with the following attributes.

EMPSALARY(empcod:int, empname:string, department:string, salary:real)

i)Create the above table.

ii)Enter the five tuples into the table

iii)Display all the number of employees working in each dapartment.

iv)Find the sum of the salaries of all employees.

v)Find the sum and average of the salaries of employees of a particular department.

vi)Find the least and highest salaries that an employee draws.

i)Create table salarydb


(
empcode number(10) primary key,
empname varchar2(20),
dept varchar2(15),
salary number(10)
);
ii) Insert into salarydb values(&empcode,’&empname’,’&dept’,&salary);

iii) select * from salarydb;

EMPCODE EMPNAME DEPT SALARY


---------- -------------------- --------------- ----------
10 Anu BCA 10000
11 Raju BBA 20000
12 Keerthi BCA 22000
13 Varun BCOM 40000
14 Venu BCOM 32000

 Select dept,count(*) from salarydb group by dept;


DEPT COUNT(*)
--------------- ----------
BCA 2
BCOM 2
BBA 1

iv) Select sum(salary) from salarydb;

SUM(SALARY)
-----------
124000

v)Select sum(salary) , avg(salary) from salarydb where dept=’BCA’;

SUM(SALARY) AVG(SALARY)
----------- -----------
52000 26000

vi)Select min(salary) from salarydb;

MIN(SALARY)
-----------
20000

 Select max(salary) from salarydb;

MAX(SALARY)
-----------
40000

----------------------------------------------------------------------------------------------------------------------------- ------
10. Consider the insurance database given below. The primary keys are underline and the data types are

specified.

 PERSON(driver-id-no: string, name: string, address:strong)

 CAR(regno: string, model: string, year: int)

 ACCIDENT(report-no: int, date: date, location: String)

 OWNS(driver-id-no: string, regno: string)


 PARTICIPATED(driver-id-no: string, regno:string,

report no: int, damage-amount: int)

i) Create the above tables by properly specifying the primary keys and the foreign keys

ii) Enter at least five tuples for each relation.

iii) Demonstrate how you

a) Update the damage amount for the car with a specific regno in the accident with report no

12 to 25000.

b) Add a new accident to the database.

iv) Find total number of people who owned cars that were involved in accidents in 2002

v) Find the number of accidents in which cars belonging to a specific model were involved

Queries

1. Create table persondb(driver_id varchar2(20) primary key,name varchar2(20) not null, address
varchar2(30));
2. Create table cardb(regno varchar2(20) primary key, model varchar2(20) not null,year
number(5));
3. Create table accidentdb(report_no number(10) primary key,accident_date date,location
varchar2(20));
4. Create table ownsdb (driver_id varchar2(20) references persondb,,regno varchar2(20)
references cardb);
5. Create table participateddb(driver_id varchar2(20) references persondb,regno varchar2(20)
references cardb,report_no number(10) references accidentdb,damage_amount number(10));
6. Insert into persondb values(&driver_id,’&name’,’&address’);
7. Insert into cardb values(&regno,’&model’,&year);
8. Insert into accidentdb values(&report_no,&accident_date,’&location’);
9. Insert into ownsdb values(&driver_id,&reg_no);
10. Insert into participateddb values(&driver_id,&reg_no,&report_no,&damage_amount);

11. Update participateddb set damage_amount = 25000 where reg_no = 1001 and report_no = 12;
12. Insert into participateddb values(1004,2004,10,3000);
13. Select count(*) from accidentdb where accident_date LIKE ‘%-%-02’;
14. Select count(*) from cardb c ,participateddb p where c.regno=p.regno and c.model = ‘scoda’;
Part B

11.Write PL/SQL procedure for an application using Exception handling.

set serveroutput on;

DECLARE
N number;
BEGIN
N:=10/0;
EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE ('zero divide error');
END;
/
Output:
Zero Divide Error

PL/SQL procedure successfully completed.


----------------------------------------------------------------------------------------------------------------------------- ------
12.Write PL/SQL procedure for an application using cursors.

i)Create table customers


(
c_id number,
c_name varchar(20),
c_addr varchar(20),
c_salary number(20)
);

ii)Insert into customers values(222,’Arun’,’Chennai’,20000);


Insert into customers values(223,’Vara’,’Bangalore’, 30000);
Insert into customers values(224,’Zain’,’Mysore’, 40000);
Insert into customers values(225,’Dani’,’Chennai’, 50000);
Insert into customers values(226,’Shoba’,’Kerala’, 60000);

iii)Select * from customers;


C_ID C_NAME C_ADDR C_SALARY
---------- -------------------- -------------------- ----------
222 Arun Chennai 20000
223 Vara Bangalore 30000
224 Zain Mysore 40000
225 Dani Chennai 50000
226 Shoba Kerala 60000

set serveroutput on;

iv)PL/SQL

DECLARE
total_rows number(2);
BEGIN
UPDATE customers
SET c_salary = c_salary + 5000;
IF sql%notfound THEN
dbms_output.put_line('no customers updated');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers updated ');
END IF;
END;
/

v)Select * from customers;

C_ID C_NAME C_ADDR C_SALARY


---------- -------------------- -------------------- ----------
222 Arun Chennai 25000
223 Vara Bangalore 35000
224 Zain Mysore 45000
225 Dani Chennai 55000
226 Shoba Kerala 65000

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

13. Write PL/SQL procedure for an application using Find the total strength of students in a class using
functions.

// create a table
i)Create table student
(
Stud_id number,
Stud_name varchar(20),
Stud_address varchar(20)
);

ii)Insert into student values(121,'Anu','Bangalore');


insert into student values(122,'Azar','Chennai');
insert into student values(123, 'Ajay','Mysore');

// Function
iii)create or replace function totalStrength
return number as
strength number;
begin
select count(Stud_id) into strength from student;
return strength;
end;
/

set serveroutput on;

// PL/SQL Block
iv)declare
strength number;
begin
strength:=totalstrength();
dbms_output.put_line('Total strength of students is ' || strength);
end;
/

Output:

Total strength of students is 3

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

14) Write PL/SQL procedure for an application using package.

i)Create or replace package deptpack as


Procedure addrec(deptno number,dname varchar2,loc varchar2);
Procedure delrec(deptno number);
End deptpack;
/

ii)Create table Dept


(
deptno number(10) not null,
dname varchar2(10) not null,
loc varchar28(20)
);

ii)Create or replace package body deptpack as


Procedure addrec(deptno number,dname varchar2,loc varchar2) is
Begin
Insert into Dept values(deptno,dname,loc);
End addrec;
Procedure delrec(deptno number) is
Begin
Delete from Dept where deptno=deptno;
End delrec;
End deptpack;
/

iv)Exec deptpack.addrec(10,’HRM’,’LONDON’);

Exec deptpack.addrec(20,’SALES’,’NEW YORK’);

Exec deptpack.addrec(10,’HRM’,’BOSTON’);

v)Select * from Dept;

DEPTNO DNAME LOC


---------- ---------- --------------------
10 HRM LONDON
20 SALES NEW YORK
10 HRM BOSTON

vi)Exec deptpack.delrec(20);

vii)Select * from Dept;

DEPTNO DNAME LOC


---------- ---------- --------------------
10 HRM LONDON
HRM BOSTON

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

15.Write a PL/SQL program to find the largest of three numbers.

set serveroutput on;

// PL/SQL Block

Declare
a number;
b number;
c number;
begin
a:=&a;
b:=&b;
c:=&c;
if (a>b and a>c) then
dbms_output.put_line(‘a is largest’ || a); elsif (b>a and
b>c) then
dbms_output.put_line(‘b is largest’ || b);
else
dbms_output.put_line(‘c is the largest’||c);
end if;
end;
/

Output:

Enter value for a: 4


old 6: a:=&a;
new 6: a:=4;
Enter value for b: 5
old 7: b:=&b;
new 7: b:=5;
Enter value for c: 2
old 8: c:=&c;
new 8: c:=2;
b is largest5

PL/SQL procedure successfully completed.


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

16. Write a PL/SQL program to print number is odd or even.

set serveroutput on;

// PL/SQL Block
declare

n number;

begin

n:= &number;

if mod(n,2) = 0 then

dbms_output.put_line ('the number. '||n||' is even number');

else
dbms_output.put_line ('the number '||n||' is odd number.');

end if;

end;

Output:

Enter value for number: 5

old 4: n:= &number;

new 4: n:= 5;

The number 5 is odd number.

PL/SQL procedure successfully completed.

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

17. Write a PL/SQL program to display the description against a grade.

set serveroutput on;

// PL/SQL Block
Declare

grd char(1);

begin

grd := '&grade’;

if grd = 'a' then

dbms_output.put_line('your grade is: outstanding');

elsif grd = 'b' then

dbms_output.put_line('your grade is: excellent');

elsif grd = 'c' then

dbms_output.put_line('your grade is: very good');

elsif grd = 'd' then

dbms_output. put_line('your grade is: average');

elsif grd = 'f' then

dbms_output.put_line('your grade is: poor');


else

dbms_output.put_line('no such grade in the list.');

end if;

end;

Output:

Enter value for grade: A

old 4: grd := '&Grade';

new 4: grd := 'A';

Your Grade is: Outstanding

PL/SQL procedure successfully completed.

18. Write a PL/SQL program to check whether a given character is alphabet or digit or Special character.

set serveroutput on;

// PL/SQL Block

declare
ch char(1);
begin
ch := '&character';
if ( ch >= 'a' and ch <= 'z' )
or ( ch >= 'a' and ch <= 'z' ) then
dbms_output.put_line ('the given character is an alphabet');
elsif ch>= '0' and ch<='9' then
dbms_output.put_line ('the given character is a number');
else
dbms_output.put_line ('the given character is a special character’);
end if;
end;
/

Output

Enter value for character: #


old 4: ch := '&character';
new 4: ch := '#';
the given character is a special character

PL/SQL procedure successfully completed.


----------------------------------------------------------------------------------------------------------------------------- ----------
19. Write a PL/SQL program to find Area and Perimeter of a circle

set serveroutput on;

DECLARE

area NUMBER(6, 2) ;
perimeter NUMBER(6, 2) ;

radius NUMBER(1) := 3;

pi CONSTANT NUMBER(3, 2) := 3.14;

BEGIN

area := pi * radius * radius;


perimeter := 2 * pi * radius;
dbms_output.Put_line('Area = ' || area);
dbms_output.Put_line(' Perimeter = ' || perimeter);

END;

Area = 28.26
Perimeter = 18.84

PL/SQL procedure successfully completed.


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

20. Write a PL/SQL program to add two numbers using function.

set serveroutput on;

create or replace function adder(n1 in number, n2 in number)


return number
is
n3 number(8);
begin
n3 :=n1+n2;
return n3;
end;
/

// PL/SQL Block

DECLARE
n3 number(2);
BEGIN
n3 := adder(11,22);
dbms_output.put_line('Addition is: ' || n3);
END;
/

Output:

Addition is: 33

PL/SQL procedure successfully completed.


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

All the queries were executed in Oracle 11g.

************************************************************************************
END

You might also like