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

Lab Manual For Database Management Systems: Sreenidhi Institute of Science & Technology

Doc2

Uploaded by

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

Lab Manual For Database Management Systems: Sreenidhi Institute of Science & Technology

Doc2

Uploaded by

Spam Accounts
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 57

Department of CSE Database Management system Lab Manual

SREENIDHI INSTITUTE OF SCIENCE &


TECHNOLOGY
(An Autonomous Institution approved by UGC & JNTUH)
Yamnampet, Ghatkesar Mandal, Hyderabad – 501 301.

LAB MANUAL FOR

DATABASE MANAGEMENT SYSTEMS


For

II/IV – CSE - II SEMESTER

2014-2015

DEPARTMENT OF

COMPUTER SCIENCE ENGINEERING

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


1
Department of CSE Database Management system Lab Manual

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Vision
To emerge as a premier Centre in Computer Science & Engineering with focus on
human values and professional ethics.

Mission
I. To prepare Computer Science and Engineering graduates to be a lifelong learner with
competence in basic science & engineering and professional core, interdisciplinary
subjects, with continuous update of the syllabus, so that they can succeed in industry
or to pursue higher studies or to become an entrepreneur.

II. To enable the graduates to design and create novel products required for the society at
large by shaping our department as an excellent centre of engineering education.

III. To continuously engage in research and development activities and to promote


scientific temper in the graduates.

Programme Educational Objectives

1. Graduates of the program will have requisite engineering knowledge with abilities for
analysis of the problem and to design, development of solutions and to arrive at an
optimal solution using modern tools which help them to be employable.
2. Ability to work in a team/ lead a team which needs effective communication skills
and knowledge of project management, finance and entrepreneurial abilities.
3. Graduates should have abilities to conduct investigation of complex problems and
attitude for lifelong learning skills which will enable them to pursue advanced
studies, Research and Development.
4. The graduates must be aware of the engineering professional ethics, the impact of
engineering profession on the society and the need for environmental protection and
sustainable development.

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


2
Department of CSE Database Management system Lab Manual

Programme Outcomes
The Programme Outcomes (POs) of the B.Tech (CSE) programme are listed below:

a. An ability to apply knowledge of basic sciences, mathematics and engineering in the area
of Computer Science.

b. An ability to design, implement and evaluate a software or software / hardware system to


meet the desired needs within realistic constraints such as space and time.

c. An ability to use the techniques, skills, and modern engineering tools such as software
testing tools, data warehousing and mining tools, necessary for practice as a CSE
professional.

d. An ability to analyze and solve open-ended problems using mathematical foundations,


algorithmic principles, and computer science theory in the modeling and design of
computer-based systems in a way that demonstrates comprehension of the tradeoffs
involved in design choices and to arrive at an optimal solution.

e. To understand principles of engineering, entrepreneurship with emphasis on women, and


financial management through relevant management courses to demonstrate knowledge
in the conceptualization and realizing group projects, mini & main projects.

f. An ability to function effectively as individual and as a member or leader in diverse team


in achieving multidisciplinary tasks.

g. Learn to communicate effectively on complex engineering activities through report


writing, experimental work, assignments, seminars, group projects, mini & main
projects.

h. To recognize the need for and have the preparation and ability to be a life-long learner
through the courses such as seminars & projects.

i. An ability to identify, formulate and analyze engineering problems.

j. An ability to conduct investigation of complex problems in multidisciplinary areas.

k. An understanding of professional ethics and responsibilities.


l. An engineer should be aware of social, safety, cultural and information security issues
and also responsibilities relevant to professional practice and skills.
m. An ability to understand the impact of environmental protection and sustainable
development.
SREENIDHI INSTITUTE OF SCIENCE & TECHNOLOGY
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
3
Department of CSE Database Management system Lab Manual

(AN AUTONOMOUS INSTITUTION UNDER JNTUH)


(Approved by AICTE & Aided by World Bank under TEQIP )

Yamnampet, Ghatkesar Mandal, Hyderabad - 501 301.

COURSE FILE

For

DATABASE MANAGEMENT SYSTEMS LAB MANUAL

For
B. Tech. II year – II SEM

COMPUTER SCIENCE & ENGINEERING

DEPARTMENT

OF

COMPUTER SCIENCE & ENGINEERING


a b c d e f g h i j k l m

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


4
Department of CSE Database Management system Lab Manual

x x x x

Syllabus for B. Tech. II Year II semester


Computer Science and Engineering
DATABASE MANAGEMENT SYSTEMS LAB
Code: 4FC74 L T P/D C
- - 4 2
Course Outcomes:
1. Write simple queries as well as writing nested quries to retrieve records from the tables
using appropriate keywords
2. Retrieve records by using aggregate operators, using group by and having clauses
3. Use string, date, number and arithmetic functions
4. Work with PL/SQL program with all sections, inserting records into a table from
PL/SQL program
5. Work with COMMIT, ROLLBACK and SAVEPOINT in PL/SQL block
6. Work with procedures, functions, cursors and triggers.

1. Creation, altering and droping of tables and inserting rows into a table (use constraints
while creating tables) examples using SELECT command.

2. Queries (along with sub Queries) using ANY, ALL, IN, EXISTS, NOTEXISTS, UNION,
INTERSET, Constraints.

Example: - Select the roll number and name of the student who secured fourth rank in the
class.

3. Queries using Aggregate functions (COUNT, SUM, AVG, MAX and MIN), GROUP BY,
HAVING and Creation and dropping of Views.

4. Queries using Conversion functions (to_char, to_number and to_date), string functions

(Concatenation, lpad, rpad, ltrim, rtrim, lower, upper, initcap, length, substr and instr), date
functions (Sysdate, next_day, add_months, last_day, months_between, least, greatest,
trunc, round, to_char, to_date)

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


5
Department of CSE Database Management system Lab Manual

5. i)Creation of simple PL/SQL program which includes declaration section, executable


section and exception –Handling section (Ex. Student marks can be selected from the table
and printed for those who secured first class and an exception can be raised if no records
were found)

ii)Insert data into student table and use COMMIT, ROLLBACK and SAVEPOINT in

PL/SQL block.

6. Develop a program that includes the features NESTED IF, CASE and CASE expression.
The program can be extended using the NULLIF and COALESCE functions.

7. Program development using WHILE LOOPS, numeric FOR LOOPS, nested loops using
ERROR Handling, BUILT –IN Exceptions, USE defined Exceptions, RAISE-
APPLICATION ERROR.

8. Programs development using creation of procedures, passing parameters IN and OUT of


PROCEDURES.

9. Program development using creation of stored functions, invoke functions in SQL


Statement and write complex functions.

10. Program development using creation of package specification, package bodies, private

objects, package variables and cursors and calling stored packages.

11. Develop programs using features parameters in a CURSOR, FOR UPDATE CURSOR,

WHERE CURRENT of clause and CURSOR variables.

12. Develop Programs using BEFORE and AFTER Triggers, Row and Statement Triggers and
INSTEAD OF Triggers

TEXT BOOKS:

1) ORACLE PL/SQL by example. Benjamin Rosenzweig, Elena Silvestrova, Pearson Education


3 Edition

2) ORACLE DATA BASE LOG PL/SQL Programming SCOTT URMAN, Tata Mc- Graw Hill.

3) SQL & PL/SQL for Oracle 10g, Black Book, Dr. P. S. Deshpande.

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


6
Department of CSE Database Management system Lab Manual

List1

1. Create Sailors table with the following fields and describe the table (sid: integer, sname:
string, rating: integer)

2. Add field (age: integer) to the Sailors table and describe the table

3. Modify field age to real in the Sailors table and describe the table

4. Delete age field from the Sailors table and describe the table

5. Add field (age: real) to the Sailors table and describe the table

6. Delete Sailors table

7. Create Sailors table with the following fields and describe the table (sid: integer, sname:
string, rating: integer, age: real)

8. Insert the following records in to the Sailors table

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


7
Department of CSE Database Management system Lab Manual

9. Retrieve all Records from the Sailors table

10. Retrieve all Records (unique) from the Sailors table

11. Retrieve the names and ages of all sailors from the Sailors table

12. Find all sailors with a rating above 7

13. Create Sailors1 table with the following fields and describe the table (sid: integer, sname:
string, rating: integer, age: real)

14. Insert tuples of Sailors table in to the Sailors1 table

15. Retrieve all Records from the Sailors1 table

16. Delete sailor from the Sailors1 table whose sid is 58

17. Retrieve all Records from the Sailors1 table

18. Delete all tuples from the Sailors1 table

19. Create Boats table with the following fields and describe the table (bid: integer, bname:
string, color: string)

20. Insert the following records in to the Boats table

21. Retrieve all Records from the Boats table

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


8
Department of CSE Database Management system Lab Manual

22. Create Reserves table with the following fields and describe the table (sid: integer, bid: integer,
day: date)

23. Insert the following records in to the Reserves table

24. Retrieve all Records from the Reserves table

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


9
Department of CSE Database Management system Lab Manual

List -1 Solutions

1. Create Sailors table with the following fields and describe the table (sid: integer, sname:
string, rating: integer)

Query: Create table Sailors(sid number(2),sname varchar2(20),rating number(2))


Output: Table Created
Query: desc Sailors
Output:

Name Null? Type


SID NUMBER(2)
SNAME VARCHAR2(20)
RATING NUMBER(2)

2. Add field (age: integer) to the Sailors table and describe the table

Query: ALTER TABLE Sailors


ADD (age NUMBER(2))
Output: Table Altered
Query: desc Sailors
Output:

Name Null? Type


SID NUMBER(2)
SNAME VARCHAR2(10)
RATING NUMBER(2)
AGE NUMBER(2)

3. Modify field age to real in the Sailors table and describe the table

Query: ALTER TABLE Sailors


MODIFY (age NUMBER(3,1))
Output: Table Altered
Query: desc Sailors
Output:
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
10
Department of CSE Database Management system Lab Manual

Name Null? Type


SID NUMBER(2)
SNAME VARCHAR2(10)
RATING NUMBER(2)
AGE NUMBER(3,1)

4. Delete age field from the Sailors table and describe the table

Query: ALTER TABLE Sailors


DROP COLUMN age
Output: Table Altered
Query: desc Sailors
Output:

Name Null? Type


SID NUMBER(2)
SNAME VARCHAR2(10)
RATING NUMBER(2)

5. Add field (age: real) to the Sailors table and describe the table

Query: ALTER TABLE Sailors


ADD (age NUMBER(3,1))
Output: Table Altered
Query: desc Sailors
Output:

Name Null? Type


SID NUMBER(2)
SNAME VARCHAR2(10)
RATING NUMBER(2)
AGE NUMBER(3,1)

6. Delete Sailors table

Query: DROP TABLE Sailors


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
11
Department of CSE Database Management system Lab Manual

Output: Table dropped.

7. Create Sailors table with the following fields and describe the table (sid: integer, sname:
string, rating: integer, age: real)

Query: CREATE TABLE Sailors (sid NUMBER(2), sname VARCHAR2(20),


rating NUMBER(2),age NUMBER(3,1))
Output: Table Created
Query: desc Sailors
Output:

Name Null? Type


SID NUMBER(2)
SNAME VARCHAR2(20)
RATING NUMBER(2)
AGE NUMBER(3,1)

8. Insert the following records in to the Sailors table

Query:
insert into Sailors values(22,'Dustin',7,45.0);
insert into Sailors values(29,'Brutus',1,33.0);
insert into Sailors values(31,'Lubber',8,55.5);
insert into Sailors values(32,'Andy',8,25.5);
insert into Sailors values(58,'Rusty',10,35.0);
insert into Sailors values(64,'Horatio',7,35.0);

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


12
Department of CSE Database Management system Lab Manual

insert into Sailors values(71,'Zorba',10,16.0);


insert into Sailors values(74,'Horatio',9,35.0);
insert into Sailors values(85,'Art',3,25.5);
insert into Sailors values(95,'Bob',3,63.5);
Output:
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.

9. Retrieve all Records from the Sailors table

Query: select * from Sailors


Output:

SID SNAME RATING AGE


22 Dustin 7 45
29 Brutus 1 33
31 Lubber 8 55.5
32 Andy 8 25.5
58 Rusty 10 35
64 Horatio 7 35
71 Zorba 10 16
74 Horatio 9 35
85 Art 3 25.5
95 Bob 3 63.5
10 records selected.
10. Retrieve all Records (unique) from the Sailors table
Query: select distinct sname,age from Sailors
Output:

SNAME AGE
Andy 25.5
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
13
Department of CSE Database Management system Lab Manual

Art 25.5
Bob 63.5
Brutus 33
Dustin 45
Horatio 35
Lubber 55.5
Rusty 35
Zorba 16
9 records selected.
11. Retrieve the names and ages of all sailors from the Sailors table
Query: select sname,age from sailors
Output:

SNAME AGE
Dustin 45
Brutus 33
Lubber 55.5
Andy 25.5
Rusty 35
Horatio 35
Zorba 16
Horatio 35
Art 25.5
Bob 63.5
10 records selected.
12. Find all sailors with a rating above 7
Query: select * from sailors where rating>7
Output:

SID SNAME RATING AGE


31 Lubber 8 55.5
32 Andy 8 25.5
58 Rusty 10 35
71 Zorba 10 16
74 Horatio 9 35
5 records selected

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


14
Department of CSE Database Management system Lab Manual

13. Create Sailors1 table with the following fields and describe the table
(sid: integer, sname: string, rating: integer, age: real)

Query: CREATE TABLE Sailors1 (sid NUMBER(2), sname VARCHAR2(20),


rating NUMBER(2),age NUMBER(3,1))
Output: Table created.
Query: desc Sailors1
Output:

Name Null? Type


SID NUMBER(2)
SNAME VARCHAR2(20)
RATING NUMBER(2)
AGE NUMBER(3,1)

14. Insert tuples of Sailors table in to the Sailors1 table

Query: INSERT INTO Sailors1 SELECT * FROM Sailors


Output: 10 rows created.

15. Retrieve all Records from the Sailors1 table

Query: select * from Sailors1


Output:

SID SNAME RATING AGE


22 Dustin 7 45
29 Brutus 1 33
31 Lubber 8 55.5
32 Andy 8 25.5
58 Rusty 10 35
64 Horatio 7 35
71 Zorba 10 16
74 Horatio 9 35
85 Art 3 25.5
95 Bob 3 63.5
10 records selected
16. Delete sailor from the Sailors1 table whose sid is 58
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
15
Department of CSE Database Management system Lab Manual

Query: DELETE FROM Sailors1


WHERE sid=58
Output: 1 row deleted.

17. Retrieve all Records from the Sailors1 table


Query: SELECT * FROM Sailors1
Output:

SID SNAME RATING AGE


22 Dustin 7 45
29 Brutus 1 33
31 Lubber 8 55.5
32 Andy 8 25.5
64 Horatio 7 35
71 Zorba 10 16
74 Horatio 9 35
85 Art 3 25.5
95 Bob 3 63.5
9 records selected
18. Delete all tuples from the Sailors1 table
Query: DELETE FROM Sailors1
Output: 9 rows deleted.

19. Create Boats table with the following fields and describe the table
(bid: integer, bname: string, color: string)

Query: create table boats(bid number(3),bname varchar2(9),color varchar2(5))


Output: Table Created
Query: desc boats
Output:

Name Null? Type


BID NUMBER(3)
BNAME VARCHAR2(9)
COLOR VARCHAR2(5)

20. Insert the following records in to the Boats table


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
16
Department of CSE Database Management system Lab Manual

Query:
insert into boats values(101,'Interlake','blue');
insert into boats values(102,'Interlake','red');
insert into boats values(103,'Clipper','green');
insert into boats values(104,'Marine','red');

Output:
1 row created.
1 row created.
1 row created.
1 row created.
21. Retrieve all Records from the Boats table

Query: select * from boats


Output:

BID BNAME COLOR


101 Interlake blue
102 Interlake red
103 Clipper green
104 Marine red
4 records selected
22. Create Reserves table with the following fields and describe the table
(sid: integer, bid: integer, day: date)

Query: create table reserves(sid number(2), bid number(3), day date)


Output: Table Created.
Query: desc reserves
Output:

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


17
Department of CSE Database Management system Lab Manual

Name Null? Type


SID NUMBER(2)
BID NUMBER(3)
DAY DATE

23. Insert the following records in to the Reserves table

Query:
insert into reserves values(22,101,'10-oct-98');
insert into reserves values(22,102,'10-oct-98');
insert into reserves values(22,103,'10-aug-98');
insert into reserves values(22,104,'10-jul-98');
insert into reserves values(31,102,'11-oct-98');
insert into reserves values(31,103,'11-jun-98');
insert into reserves values(31,104,'11-dec-98');
insert into reserves values(64,101,'9-may-98');
insert into reserves values(64,102,'9-aug-98');
insert into reserves values(74,103,'9-aug-98');

Output:
1 row created.
1 row created.
1 row created.
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
18
Department of CSE Database Management system Lab Manual

1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.

24. Retrieve all Records from the Reserves table

Query: select * from reserves


Output:

SID BID DAY


22 101 10-OCT-98
22 102 10-OCT-98
22 103 10-AUG-98
22 104 10-JUL-98
31 102 11-OCT-98
31 103 11-JUN-98
31 104 11-DEC-98
64 101 09-MAY-98
64 102 09-AUG-98
74 103 09-AUG-98
10 records selected

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


19
Department of CSE Database Management system Lab Manual

List2

1. Find the names of sailors who have reserved boat number 103 (using AND operator)
2. Find all sailors whose age is in between 45.0 and 63.5 (using BETWEEN operator)
3. Find all sailors whose age is in the list of values(15.0,33.2,45.7,63.5) (using IN operator)
4. Find all sailors whose second letter in the names contain ‘u’
5. Find all sailors whose first letter and third letter in the names are ‘A’ and ‘d’
6. Find all sailors from sailors and sailors1 table by using UNION operator
7. Find all sailors from sailors and sailors1 table by using INTERSECT operator
8. Find all sailors from sailors and sailors1 table by using MINUS operator
9. Find the names of sailors who have reserved boat 103 (using nested query)
10. Find the names of sailors who have reserved a blue boat (using nested query)
11. Find the names of sailors who have not reserved a red boat (using nested query)
12. Find the names of sailors who have reserved boat 103 (using Correlated Nested Query)
13. Find sailors whose rating is better than some sailor called Horatio (using ANY operator)
14. Find sailors whose rating is better than some sailor called Horatio (using ALL operator)
15. Find the sailors with the highest rating
16. Find the average age of all sailors
17. Find the minimum age of the sailor
18. Find the maximum age of the sailor
19. Count the number of sailors
20. Count the number of different sailor names

List -2 Solutions

1. Find the names of sailors who have reserved boat number 103 (using AND operator)
Query: select sname from Sailors S,reserves R
where S.sid=R.sid and R.bid=103;
Output:

SNAME
Dustin
Lubber
Horatio

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


20
Department of CSE Database Management system Lab Manual

2. Find all sailors whose age is in between 45.0 and 63.5 (using BETWEEN operator)
Query: select * from Sailors
where age between 45.0 and 63.5
Output:

SID SNAME RATING AGE


31 Lubber 8 55.5
95 Bob 3 63.5
22 Dustin 7 45

3. Find all sailors whose age is in the list of values (15.0,33.2,45.7,63.5) (using IN operator)
Query: select * from Sailors
where age in(15.0,33.2,45.7,63.5)
Output:

SID SNAME RATING AGE


95 Bob 3 63.5

4. Find all sailors whose second letter in the names contain ‘u’
Query: select * from Sailors
where sname like '_u%'
Output:

SID SNAME RATING AGE


31 Lubber 8 55.5
58 Rusty 10 35
22 Dustin 7 45

5. Find all sailors whose first letter and third letter in the names are ‘A’ and ‘d’
Query: select * from Sailors
where sname like 'A_d%'
Output:

SID SNAME RATING AGE


32 Andy 8 25.5

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


21
Department of CSE Database Management system Lab Manual

6. Find all sailors from sailors and sailors1 table by using (UNION operator)
Query: select * from Sailors
union select * from Sailors1
Output:

SID SNAME RATING AGE


22 Dustin 7 45
29 Brutus 1 33
31 Lubber 8 55.5
32 Andy 8 25.5
58 Rusty 10 35
64 Horatio 7 35
71 Zorba 10 16
74 Horatio 9 35
85 Art 3 25.5
95 Bob 3 63.5

10 rows selected.

7. Find all sailors from sailors and sailors1 table by using INTERSECT operator

Query: select * from Sailors


intersect select * from Sailors1
Output:

SID SNAME RATING AGE


22 Dustin 7 45
29 Brutus 1 33
31 Lubber 8 55.5
32 Andy 8 25.5
58 Rusty 10 35
64 Horatio 7 35
71 Zorba 10 16
74 Horatio 9 35
85 Art 3 25.5
95 Bob 3 63.5

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


22
Department of CSE Database Management system Lab Manual

8. Find all sailors from sailors and sailors1 table by using MINUS operator
Query: select * from Sailors
minus select * from Sailors1
Output:

SID SNAME RATING AGE


58 Rusty 10 35

9. Find the names of sailors who have reserved boat 103 (using nested query)
Query: select sname from Sailors
where sid in(select sid from reserves where bid =103);
Output:

SNAME
Dustin
Lubber
Horatio

10. Find the names of sailors who have reserved a blue boat (using nested query)
Query: select sname from Sailors
where sid in(select sid from reserves where bid in(select bid from boats
where color='blue'));
Output:

SNAME
Dustin
Horatio

11. Find the names of sailors who have not reserved a red boat (using nested query)
Query: select sname from Sailors
where sid in(select sid from reserves where bid in(select bid from boats
where color!='red'));
Output:

SNAME
Dustin
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
23
Department of CSE Database Management system Lab Manual

Lubber
Horatio
Horatio

12. Find the names of sailors who have reserved boat 103 (using Correlated Nested Query)
Query: select s.sname from Sailors s
where exists(select * from reserves r where
r.bid=103 and r.sid=s.sid);
Output:

SNAME
Lubber
Horatio
Dustin

13. Find sailors whose rating is better than some sailor called Horatio (using ANY
operator)
Query: select * from Sailors
where rating> any(select rating from Sailors where sname='Horatio');
Output:

SID SNAME RATING AGE


31 Lubber 8 55.5
32 Andy 8 25.5
58 Rusty 10 35
71 Zorba 10 16
74 Horatio 9 35

14. Find sailors whose rating is better than some sailor called Horatio (using ALL
operator)
Query: select * from Sailors
where rating> all(select rating from Sailors where sname='Horatio');
Output:

SID SNAME RATING AGE


58 Rusty 10 35

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


24
Department of CSE Database Management system Lab Manual

71 Zorba 10 16

15. Find the sailors with the highest rating


Query: select * from Sailors
where rating=(select max(rating) from Sailors)
Output:

SID SNAME RATING AGE


58 Rusty 10 35
71 Zorba 10 16
16. Find the average age of all sailors
Query: select avg(age) from Sailors
Output:

AVG(AGE)
36.9
17. Find the minimum age of the sailor
Query: select min(age) from Sailors
Output:

MIN(AGE)
16
18. Find the maximum age of the sailor
Query: select max(age) from Sailors
Output:

MAX(AGE)
63.5
19. Count the number of sailors
Query: select count(*) from Sailors
Output:

COUNT(*)
10
20. Count the number of different sailor names

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


25
Department of CSE Database Management system Lab Manual

Query: select count(distinct sname) from Sailors


Output:

COUNT(DISTINCT SNAME)
9

List – 3

1. Find the sum of ratings of all sailors


2. Find the second maximum age of the sailor
3. Find the name of the sailor whose age is second maximum
4. Find the name and age of the oldest sailor
5. Find the number of sailors belongs to each rating level
6. Find the age of the youngest sailor for each rating level
7. Find the age of the youngest sailor for each rating level, which is greater than 7
8. Find the age of the youngest sailor who is eligible to vote (i.e., is at least 18 years old) for
each rating level with at least two such sailors
9. Find the average age of sailors for each rating level that has at least two sailors
10. Drop sailors table
11. Create Sailors table with the following fields and describe the table ( sid: integer, sname:
string, rating: integer, age: real)
Note: Underlined attribute represents the primary key
12. Insert records in to the Sailors table
13. Drop Reserves table
14. Create Reserves table with the following fields and describe the table (sid: integer, bid:
integer, day: date)
15. Insert records in to the Reserves table
16. Drop Boats table
17. Create Boats table with the following fields and describe the table (bid: integer, bname:
string, color: string).
18. Insert records in to the Boats table
19. For each red boat, find the number of reservations for this boat.
20. Find those ratings for which the average age of sailors is the minimum over all ratings
21. Count the number of sailors who have reserved boats between '9/5/98' and '10/10/98'
22. Find the names of sailors who have reserved boats between '9/4/98' and '1/8/98'
23. Find the number of reservations made for each boat
24. Find the number of reservations (with at least three) made for each boat
25. Find the number of reservations made for each boat during '9/5/98' and '10/10/98'
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
26
Department of CSE Database Management system Lab Manual

26. Find the days on which blue color boat is reserved


27. Find the days on which 'Horatio' reserved the boats
28. Find the number of boat reservations made by each sailor
29. Find the number of boats in each color
30. Find the colors of boats reserved by ‘Lubber’

LIST – 3 Solutions

1. Find the sum of ratings of all sailors


Query: select sum(rating) from Sailors
Output:

SUM(RATING)
66

2. Find the second maximum age of the sailor


Query: select max(age) from Sailors
where age!=(select max(age) from Sailors)
Output:

MAX(AGE)
55.5

3. Find the name of the sailor whose age is second maximum


Query: select sname from Sailors
where age=(select max(age) from Sailors
where age!=(select max(age) from Sailors));
Output:

SNAME
Lubber

4. Find the name and age of the oldest sailor


Query: select sname,age from Sailors
where age=(select max(age) from Sailors)
Output:

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


27
Department of CSE Database Management system Lab Manual

SNAME AGE
Bob 63.5
5. Find the number of sailors belongs to each rating level
Query: select rating,count(rating) from Sailors
group by rating
Output:

RATING COUNT(RATING)
1 1
3 2
7 2
8 2
9 1
10 2

6. Find the age of the youngest sailor for each rating level
Query: select rating,min(age) from Sailors
group by rating
Output:

RATING MIN(AGE)
1 33
3 25.5
7 35
8 25.5
9 35
10 16
7. Find the age of the youngest sailor for each rating level, which is greater than 7
Query: select rating,min(age) from Sailors
group by rating
having rating>7

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


28
Department of CSE Database Management system Lab Manual

RATING MIN(AGE)
8 25.5
9 35
10 16

8. Find the age of the youngest sailor who is eligible to vote (i.e., is at least 18 years old) for
each rating level with at least two such sailors
Query: select rating,min(age) from Sailors
where age>=18
group by rating
having count (rating)>=2
Output:

RATING MIN(AGE)
3 25.5
7 35
8 25.5

9. Find the average age of sailors for each rating level that has at least two sailors
Query: select rating,avg(age) from Sailors
group by rating
having count (rating)>=2
Output:

RATING AVG(AGE)
3 44.5
7 40
8 40.5
10 25.5

10. Drop sailors table


Query: DROP TABLE Sailors
Output: Table dropped.

11. Create Sailors table with the following fields and describe the table (sid: integer, sname:
string, rating: integer, age: real)

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


29
Department of CSE Database Management system Lab Manual

Note: Underlined attribute represents the primary key


Query: CREATE TABLE Sailors (sid NUMBER(2) primary key, sname
VARCHAR2(20),rating NUMBER(2),age NUMBER(3,1))
Output:
Table created.
Query: desc Sailors
Output:

Name Null? Type


SID NOT NULL NUMBER(2)
SNAME VARCHAR2(20)
RATING NUMBER(2)
AGE NUMBER(3,1)

12. Insert records in to the Sailors table


Query:
insert into Sailors values(22,'Dustin',7,45.0);
insert into Sailors values(29,'Brutus',1,33.0);
insert into Sailors values(31,'Lubber',8,55.5);
insert into Sailors values(32,'Andy',8,25.5);
insert into Sailors values(58,'Rusty',10,35.0);
insert into Sailors values(64,'Horatio',7,35.0);
insert into Sailors values(71,'Zorba',10,16.0);
insert into Sailors values(74,'Horatio',9,35.0);
insert into Sailors values(85,'Art',3,25.5);
insert into Sailors values(95,'Bob',3,63.5);
Output:
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.

13. Drop Reserves table


SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
30
Department of CSE Database Management system Lab Manual

Query: DROP TABLE reserves


Output: Table dropped.

14. Create Reserves table with the following fields and


describe the table (sid: integer, bid: integer, day: date)

Query: create table reserves(sid number(2), bid number(3), day date,primary key(sid,bid))

Output: Table Created.


Query: desc reserves
Output:

Name Null? Type


SID NOT NULL NUMBER(2)
BID NOT NULL NUMBER(3)
DAY DATE

15. Insert records in to the Reserves table


Query:
insert into reserves values(22,101,'10-oct-98');
insert into reserves values(22,102,'10-oct-98');
insert into reserves values(22,103,'10-aug-98');
insert into reserves values(22,104,'10-jul-98');
insert into reserves values(31,102,'11-oct-98');
insert into reserves values(31,103,'11-jun-98');
insert into reserves values(31,104,'11-dec-98');
insert into reserves values(64,101,'9-may-98');
insert into reserves values(64,102,'9-aug-98');
insert into reserves values(74,103,'9-aug-98');
Output:
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
31
Department of CSE Database Management system Lab Manual

16. Drop Boats table


Query: drop table boats
Output: table dropped.

17. Create Boats table with the following fields and describe the table (bid: integer, bname:
string, color: string).
Query: create table boats(bid number(3) primary key,bname varchar2(9),color varchar2(5))
Output: Table Created
Query: desc boats
Output:

Name Null? Type


BID NOT NULL NUMBER(3)
BNAME VARCHAR2(9)
COLOR VARCHAR2(5)
18. Insert records in to the Boats table
Query:
insert into boats values(101,'Interlake','blue');
insert into boats values(102,'Interlake','red');
insert into boats values(103,'Clipper','green');
insert into boats values(104,'Marine','red');

Output:
1 row created.
1 row created.
1 row created.
1 row created.

19. For each red boat, find the number of reservations for this boat.
Query: select bid,count(bid) from reserves
where bid in(select bid from boats where color='red')
group by bid
Output:

BID COUNT(BID)
102 3
104 2

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


32
Department of CSE Database Management system Lab Manual

20. Find those ratings for which the average age of sailors is the minimum over all ratings

Query: select temp1.rating,temp1.ave age from (select rating,avg(age) ave from sailors group
by rating) temp1
where temp1.ave=(select min(temp2.ave) from (select rating,avg(age) ave from sailors
group by rating) temp2)
Output:

RATING AGE
10 25.5
21. Count the number of sailors who have reserved boats between '9/5/98' and '10/10/98'
Query: select count(sid) from reserves
where day between '9-may-98' and '10-oct-98'
Output:

COUNT(SID)
8

22. Find the names of sailors who have reserved boats between '9/4/98' and '1/8/98'
Query: select sname from sailors
where sid in(select sid from reserves where day between '9-apr-98' and '1-aug- 98')
Output:

SNAME
Dustin
Lubber
Horatio

23. Find the number of reservations made for each boat


Query: select bid, count(bid) from reserves
group by bid
Output:

BID COUNT(BID)
101 2
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
33
Department of CSE Database Management system Lab Manual

102 3
103 3
104 2
24. Find the number of reservations (with at least three) made for each boat
Query: select bid,count(bid) from reserves
group by bid
having count(bid)>=3
Output:

BID COUNT(BID)
102 3
103 3
25. Find the number of reservations made for each boat during '9/5/98' and '10/10/98'
Query: select bid,count(bid) from reserves
where day between '9-may-98' and '10-oct-98'
group by bid
Output:

BID COUNT(BID)
101 2
102 2
103 3
104 1
26. Find the days on which blue color boat is reserved
Query: select day from reserves where bid in( select bid from boats where color='blue')
Output:

DAY
10-OCT-98
09-MAY-98
27. Find the days on which 'Horatio' reserved the boats
Query: select day from reserves where sid in(select sid from sailors where sname='Horatio')
Output:

DAY
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
34
Department of CSE Database Management system Lab Manual

09-MAY-98
09-AUG-98
09-AUG-98
28. Find the number of boat reservations made by each sailor
Query: select s.sname,count(*)from sailors s, reserves r
where s.sid=r.sid
group by s.sname;

Output:

SNAME COUNT(*)
Dustin 4
Horatio 3
Lubber 3

29. Find the number of boats in each color


Query: select count(bid),color from boats
group by color
Output:

COUNT(BID) COLOR
1 blue
1 green
2 red
30. Find the colors of boats reserved by ‘Lubber’
Query: select distinct color from boats where bid in
(select bid from reserves where sid in
(select sid from sailors where sname='Lubber'))
Output:

COLOR
green
red

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


35
Department of CSE Database Management system Lab Manual

List4
1.Create Persons table with the following fields (declare FirstName as DEFAULT 'Ola') and
describe the table

LastNam
Pid FirstName Address City
e

1 Ola Hansen Timoteivn 10 Sandnes

2 Tove Svendson Borgvn 23 Sandnes

3 Kari Pettersen Storgt 20 Stavanger

2. Specify UNIQUE constraint on field LastName in the Persons table


3. Specify PRIMARY KEY constraint on field Pid in the Persons table
4. Create Orders table with the following fields (declare Oid as Primary Key and Pid as
Foreign Key) and describe the table
Oid OrderNo Pid

1 77895 3

2 44678 3

3 22456 2

4 24562 1

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


36
Department of CSE Database Management system Lab Manual

31. Insert the Persons records in to the Persons table


32. Insert the Orders records in to the Orders table
33. Insert (4,'Andy','Hansen','Storgt 20','Stavanger') record in to the Persons table and observe
the result
34. Insert (3,'Andy','Tahsen','Storgt 20','Stavanger') record in to the Persons table and observe the
result
35. Insert (5,51212,4) record in to the Orders table and observe the result
36. Delete record from the Persons table whose Pid is 3 and observe the result
37. Give example which includes all Arithmetic functions
38. Give example which includes all String functions
39. Give example which includes all Date and Time functions
40. Retrieve tuples from the reserves table (display ‘day’ as dd-mon-yyyy)
41. Retrieve tuples from the boats table, in which trim ‘In’ from left side on ‘bname’
42. Retrieve tuples from the sailors table, in which trim ‘io’ from right side on ‘sname’
43. Convert ‘2010 10 feb’ in to date
44. Give example which includes all General functions

List-4 Solutions

1. Create Persons table with the following fields (declare FirstName as DEFAULT 'Ola')
and describe the table
Pid FirstName LastName Address City
1 Ola Hansen Timoteivn 10 Sandnes
2 Tove Svendson Borgvn 23 Sandnes
3 Kari Pettersen Storgt 20 Stavanger
Query:
create table Persons(Pid number(5),FirstName varchar2(10) default 'Ola',LastName
varchar2(10), Address varchar2(15),City varchar2(10));
Output:
Table created.
2. Specify UNIQUE constraint on field LastName in the Persons table
Query:
alter table Persons add unique(LastName);
Output:
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
37
Department of CSE Database Management system Lab Manual

Table altered.
3. Specify PRIMARY KEY constraint on field Pid in the Persons table
Query:
alter table Persons add primary key(Pid);
Output:
Table altered.
4. Create Orders table with the following fields (declare Oid as Primary Key and Pid
as Foreign Key) and describe the table
Oid OrderNo Pid
1 77895 3
2 44678 3
3 22456 2
4 24562 1

Query:
create table Orders (Oid number(2) PRIMARY KEY,OrderNo number(5),Pid
number(2),FOREIGN KEY(Pid ) REFERENCES Persons(Pid));
Output:
Table created.
5. Insert Persons records in to Persons table
Query:
insert into persons(pid,lastname,address,city) values(1,'Hansen','Timotevin 10','Sandnes');
Output:
1 row created.
Query:
Select * from persons;
Output:

PID FIRSTNAME LASTNAME ADDRESS CITY

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


38
Department of CSE Database Management system Lab Manual

1 Ola Hansen Timotevin 10 Sandnes

Query:
insert into persons values(2,'Tove','Svendson','Borgvn 23','Sandnes');
insert into persons values(3,'Kari','Pettersen','Storgt 20','Stavanger');
Query:
Select * from persons;
Output:

PID FIRSTNAME LASTNAME ADDRESS CITY

1 Ola Hansen Timotevin 10 Sandnes

2 Tove Svendson Borgvn 23 Sandnes

3 Kari Pettersen Storgt 20 Stavanger

6. Insert Orders records in to Orders table


Query:
insert into orders values(1,77895,3);
insert into orders values(2,44678,3);
insert into orders values(3,22456,2);
insert into orders values(4,24562,1);
Output:

OID ORDERNO PID


1 77895 3
2 44678 3
3 22456 2
4 24562 1
7. Insert (4,'Andy','Hansen','Storgt 20','Stavanger') record in to Persons table and
observe the result
Query:
insert into persons values(4,'Andy','Hansen','Storgt 20','Stavanger');
Output:
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
39
Department of CSE Database Management system Lab Manual

ERROR: ORA-00001: unique constraint (SCOTT.SYS_C005118) violated


8. Insert (3,'Andy','Tahsen','Storgt 20','Stavanger') record in to Persons table and observe
the result
Query:
insert into persons values(3,'Andy','Tahsen','Storgt 20','Stavanger');
Output:
ERROR: ORA-00001: unique constraint (SCOTT.SYS_C005119) violated
9. Insert (5,51212,4) record in to Orders table and observe the result
Query:
insert into orders values(5,51212,4);
Output:
ERROR: ORA-02291: integrity constraint (SCOTT.SYS_C005121) violated - parent key not
found
10. Delete record from the Persons table whose Pid is 3 and observe the result
Query:
delete from Persons where Pid=3;
Output:
ERROR: ORA-02292: integrity constraint (SCOTT.SYS_C005121) violated - child record found
11. Give example which includes all Arithmetic functions
Query:
select abs(4.55),ceil(4.55),floor(4.55), mod(5,4),power(2,3),sign(-10), sign(10),
sqrt(9),round(4.333,2),trunc(4.333,2) from dual
Output:

SIG
ABS(4. CEIL(4 FLOOR( MOD( POWER SIGN( SQRT ROUND(4. TRUNC(4.
N(-
55) .55) 4.55) 5,4) (2,3) 10) (9) 333,2) 333,2)
10)
4.55 5 4 1 8 -1 1 3 4.33 4.33
12. Give example which includes all String functions
Query:
select ascii('A'),chr(65),concat('abc','def'),instr('abaabcab','ab',1,2),substr('abcd',2) from dual
Output:

ASCII('A') C CONCAT INSTR('ABAABCAB','AB',1,2) SUB


65 A abcdef 4 bcd
select length('abcd'),lengthb('abcde'),lower('ABCD'),lpad('abcd',7,'e'),
ltrim('abcd','a') from dual
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
40
Department of CSE Database Management system Lab Manual

Output:

LENGTH('ABCD') LENGTHB('ABCDE') LOWE LPAD('A LTR


4 5 abcd eeeabcd bcd
select replace('abcd','a','ef'),rtrim('abcd','d'),rpad('abcd',7,'e'),to_char(123) from dual
Output:

REPLA RTR RPAD('A TO_


efbcd abc abcdeee 123
select substr('abcdef',2,3),translate('abcdef','bc','ef'),upper('abcd'),initcap('abcd') from dual
Output:

SUB TRANSL UPPE INIT


bcd aefdef ABCD Abcd
13. Give example which includes all Date and Time functions
Query:
select add_months(current_date,5),current_date,current_timestamp(2),to_date('10-10-2011','dd-
mm-yy'),months_between('10-oct-2011','10-oct-2010'),
EXTRACT(year from current_date),to_char(current_date,'mon dd yyyy')
from dual
Output:

MONTHS_BE
TWEEN('10-
ADD_M CURR CURRENT_TI TO_D EXTRACT(YEARFRO TO_CH
OCT-
ONTH ENT_D MESTAMP(2) ATE(' MCURRENT_DATE) AR(CUR
2011','10-
OCT-2010')
10- 10-NOV-12 10-
10-APR- nov 10
NOV- 02.25.41.00 PM OCT- 12 2012
13 2012
12 +05:30 11

14. Retrieve tuples from the reserves table (display ‘day’ as dd-mon-yyyy)
Query:
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
41
Department of CSE Database Management system Lab Manual

select sid,bid,to_char(day,'dd-mon-yyyy') day from reserves


Output:

SID BID DAY


22 101 10-OCT-1998
22 102 10-OCT-1998
22 103 10-AUG-1998
22 104 10-JUL-1998
31 102 11-OCT-1998
31 103 11-JUN-1998
31 104 11-DEC-1998
64 101 09-MAY-1998
64 102 09-AUG-1998
74 103 09-AUG-1998
15. Retrieve tuples from the boats table, in which trim 'In' from left side on ‘bname’
Query:
select ltrim(bname,'In') from boats
Output:

LTRIM(BNAME,'IN)
terlake
terlake
Clipper
Marine
16. Retrieve tuples from the sailors table, in which trim 'io' from right side on ‘sname’
Query:
select rtrim(sname,'io') from sailors
Output:

RTRIM(SNAM
Dustin
Brutus

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


42
Department of CSE Database Management system Lab Manual

Lubber
Andy
Rusty
Horat
Zorba
Horat
Art
Bob
17. Convert ‘2010 10 feb’ in to date
Query:
select to_date('2010 10 feb','yyyy dd mon') todate from dual
Output:
TODATE
10-FEB-10

18. Give example which includes all General functions


Query:
select sid,sname,cast(sid as varchar2(10)) from sailors
Output:

SID SNAME CAST(SIDAS


22 Dustin 22
29 Brutus 29
31 Lubber 31
32 Andy 32
58 Rusty 58
64 Horatio 64
71 Zorba 71
74 Horatio 74
85 Art 85
95 Bob 95
Query:
create table cal(id number);
Output:
Table created.
Query:
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
43
Department of CSE Database Management system Lab Manual

insert into cal values(10);


insert into cal(id) values(null);
insert into cal values(20);
Query:
ID
10

20
select * from cal
Output:

Query:
select id,nullif(id,10),nvl(id,88),nvl2(id,66,88) from cal
Output:

ID NULLIF(ID,10) NVL(ID,88) NVL2(ID,66,88)


10 10 66
88 88
20 20 20 66
Query:
select sid,decode(sid,22,'A',31,'B','ZZ') results from sailors
Output:

SID RE
22 A
29 ZZ
31 B
32 ZZ
58 ZZ
64 ZZ
71 ZZ
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
44
Department of CSE Database Management system Lab Manual

74 ZZ
85 ZZ
95 ZZ

List5

1. Write a PL/SQL program to perform the following:

Insert <99,'John',9,25.6> into the Sailors table, Update sname 'John' to 'Joe' in the Sailors table,
Delete tuple from the Sailors table whose sname is 'Joe'

2. Retrieve tuple from the Sailors table whose sname is 'Lubber' using PL/SQL program
3. Write a PL/SQL program to find largest and smallest of three numbers
4. Write a PL/SQL program to check whether a number is prime or not?
5. Write a PL/SQL program to check whether a number is palindrome or not?
6. Write a PL/SQL program to show handling of Pre-defined exception
7. Write a PL/SQL program to show handling of User-defined exception
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
45
Department of CSE Database Management system Lab Manual

8. Write a PL/SQL program to print the following values

0 if a=0

1 if a>0

-1 if a<0

9. Write a PL/SQL program to find even numbers and odd numbers in a given range m to n
10. Write a PL/SQL program to find armstrong numbers in a given range m to n
11. Write a PL/SQL program to retrieve records from the sailors table using cursor
12. Write a PL/SQL procedure to find factorial of a given number
13. Write a PL/SQL procedure to print first n fibonacci numbers
14. Write a PL/SQL function to find sum of digits of a number
15. Write a PL/SQL recursive function to find factorial of a number
16. Explain Packages with PL/SQL program

List-5 Solutions

1. Write a PL/SQL program to perform the following: Insert <99,'John',9,25.6> into the
Sailors table, Update sname 'John' to 'Joe' in the Sailors table, Delete tuple from the
Sailorstable whose sname is 'Joe'
Program:
DECLARE
s_id number(2):=99;
s_name varchar2(20):='john';
s_rating number(2):=9;
s_age number(3,1):=25.6;
BEGIN
INSERT INTO Sailors VALUES(s_id,s_name,s_rating,s_age);
end;
Output:
SID SNAME RATING AGE
22 Dustin 7 45
29 Brutus 1 33
31 Lubber 8 55.5
32 Andy 8 25.5
58 Rusty 10 35
64 Horatio 7 35
71 Zorba 10 16
74 Horatio 9 35

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


46
Department of CSE Database Management system Lab Manual

85 Art 3 25.5
95 Bob 3 63.5
99 john 9 25.6
11 rows selected.
Program:
begin
update sailors set sname='joe' where sname='john';
end;
Output:
SID SNAME RATING AGE
22 Dustin 7 45
29 Brutus 1 33
31 Lubber 8 55.5
32 Andy 8 25.5
58 Rusty 10 35
64 Horatio 7 35
71 Zorba 10 16
74 Horatio 9 35
85 Art 3 25.5
95 Bob 3 63.5
99 joe 9 25.6
11 rows selected.
Program:
begin
delete from sailors where sname='joe';
end;

Output:
SID SNAME RATING AGE
22 Dustin 7 45
29 Brutus 1 33
31 Lubber 8 55.5
32 Andy 8 25.5
58 Rusty 10 35
64 Horatio 7 35
71 Zorba 10 16
74 Horatio 9 35
85 Art 3 25.5
95 Bob 3 63.5
10 rows selected.
2. Retrieve tuple from the Sailors table whose sname is 'Lubber' using PL/SQL program
Program:
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
47
Department of CSE Database Management system Lab Manual

declare
s_id sailors.sid%type;
s_sname sailors.sname%type;
s_rating sailors.rating%type;
s_age sailors.age%type;
begin
select sid,sname,rating,age into s_id,s_sname,s_rating,s_age from sailors where
sname='lubber';
dbms_output.put_line('sid is '||s_id||' sname is'||s_sname||' rating'||s_rating||' age i s'||s_age);
end;
Output:
sid is 31 sname is lubber rating is 8 age is 55.5
3. Write a PL/SQL program to find largest and smallest of three numbers
Program:
declare
a number(2):=10;
b number(2):=5;
c number(2):=15;
large number(2):=0;
small number(2):=0;
begin
if b>a and b>c then
large:=b;
elsif c>b and c>a then
large:=c;
else
large:=a;
end if;
dbms_output.put_line('largest number is'||large);
if b<a and b<c then
small:=b;
elsif c<b and c<a then
small:=c;
else
small:=a;
end if;
dbms_output.put_line('smallest number is'||small);
end;
Output:
largest number is15
smallest number is5
4. Write a PL/SQL program to check whether a number is prime or not?
Program:
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
48
Department of CSE Database Management system Lab Manual

declare
a number:=&a;
c number:=0;
i number;
begin
for i in 1..a loop
if mod(a,i)=0 then
c:=c+1;
end if;
end loop;
if c=2 then
dbms_output.put_line('number is prime');
else
dbms_output.put_line('number is not prime');
end if;
end;
Output:
Enter value for a: 7
old 2: a number:=&a;
new 2: a number:=7;
number is prime
5. Write a PL/SQL program to check whether a number is palindrome or not?
Program:
declare
a number:=&a;
c number:=a;
r number:=0;
s number:=0;
begin
while a>0 loop
r:=mod(a,10);
s:=s*10+r;
a:=floor(a/10);
end loop;
if s=c then
dbms_output.put_line('number is palindrome');
else
dbms_output.put_line('number is not palindrome');
end if;
end;
Output:
Enter value for a: 123
old 2: a number:=&a;
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
49
Department of CSE Database Management system Lab Manual

new 2: a number:=123;
number is not palindrome
6. Write a PL/SQL program to show handling of Pre-defined exception Program:
declare
a number:=4;
c number;
begin
c:=a/0;
exception
when ZERO_DIVIDE then
dbms_output.put_line('divide by zero exception');
end;
Output:
divide by zero exception
7. Write a PL/SQL program to show handling of User-defined exception
Program:
declare
a number(2);
b number(2);
c number(2);
ex exception;
begin
a:=&a;
b:=&b;
if(b=0) then
raise ex;
else
c:=a/b;
dbms_output.put_line(c);
end if;
exception
when ex then
dbms_output.put_line('divide by zero');
end;
Output:
old 8: b:=&b;
new 8: b:=0;
divide by zero
8. Write a PL/SQL program to print the following values
0 if a=0
1 if a>0
-1 if a<0
Program:
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
50
Department of CSE Database Management system Lab Manual

declare
a number:=&a;
begin
if a=0 then
dbms_output.put_line('0');
elsif a>0 then
dbms_output.put_line('1');
else
dbms_output.put_line('-1');
end if;
end;
Output:
Enter value for a: 5
old 2: a number:=&a;
new 2: a number:=5;
1
9. Write a PL/SQL program to find even numbers and odd numbers in a given range m to
n
Program:
declare
m number:=&m;
n number:=&n;
i number;
begin
for i in m..n loop
if mod(i,2)=1 then
dbms_output.put_line(i||' is odd number');
else
dbms_output.put_line(i||' is even number');
end if;
end loop;
end;
Output: Enter value for a: 25
old 2: m number:=&m;
new 2: m number:=25;
Enter value for a: 30
old 3: n number:=&n;
new 3: n number:=30;
25 is odd number
26 is even number
27 is odd number
28 is even number
29 is odd number
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
51
Department of CSE Database Management system Lab Manual

30 is even number
10. Write a PL/SQL program to find armstrong numbers in a given range m to n
Program:
declare
m number:=&m;
n number:=&n;
i number;
c number;
r number;
s number:=0;
begin
for i in m..n loop
c:=i;
while c>0 loop
r:=mod(c,10);
s:=s+(r*r*r);
c:=floor(c/10);
end loop;
if s=i then
dbms_output.put_line(i||' is armstrong');
end if;
s:=0;
end loop;
end;
Output:
Enter value for m: 150
old 2: m number:=&m;
new 2: m number:=150;
Enter value for n: 1000
old 3: n number:=&n;
new 3: n number:=1000;
153 is armstrong
370 is armstrong
371is armstrong
407 is armstrong

11. Write a PL/SQL program to retrieve records from the sailors table using cursor
Program:
declare
s_id number(2);
s_name varchar2(20);
s_rating number(2);
s_age number(3,1);
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
52
Department of CSE Database Management system Lab Manual

cursor c_sailors is select sid,sname,rating,age from sailors;


begin
open c_sailors;
loop
fetch c_sailors into s_id,s_name,s_rating,s_age;
dbms_output.put_line(s_id||' '||s_name||' '||s_rating||' '||s_age);
exit when c_sailors%notfound;
end loop;
close c_sailors;
end;
Output:
22 dustin 7 45
29 brutus 1 33
31 lubber 8 55.5
32 andy 8 25.5
58 rusty 10 35
64 horatio 7 35
71 zorba 10 16
74 horatio 9 35
85 art 3 25.5
95 bob 3 63.5

12. Write a PL/SQL procedure to find factorial of a given number


Procedure:
create or replace procedure fact(n in number, r out number) is
i number:=n; --Never modify in parameter value
f number:=1;
begin
while(i>1) loop
f:=f*i;
i:=i-1;
end loop;
r:=f; --Assign final value to out parameter
end fact;
Output:
Procedure created.
Program:
declare
n number;
r number;
begin
n:=&n;
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
53
Department of CSE Database Management system Lab Manual

fact(n,r);
dbms_output.put_line('Factorial of '||n||' is '||r);
end;
Output:
Enter value for n: 6
old 5: n:=&n;
new 5: n:=6;
Factorial of 6 is 720

13. Write a PL/SQL procedure to print first n Fibonacci numbers


Procedure:
create or replace procedure fibonacci(n in number) is
f1 number:=0;
f2 number:=1;
f3 number;
i number:=1;
begin
dbms_output.put_line('Fibonacci Series is ');
dbms_output.put_line(f1);
dbms_output.put_line(f2);
while(i<n-1) loop
f3:=f1+f2;
f1:=f2;
f2:=f3;
i:=i+1;
dbms_output.put_line(f3);
end loop;
end fibonacci;
Output:
Procedure created.
Program:
declare
n number;
begin
n:=&n;
fibonacci(n);
end;
Output:
Enter value for n: 5
old 4: n:=&n;
new 4: n:=5;
Fibonacci Series is
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
54
Department of CSE Database Management system Lab Manual

0
1
1
2
3
14. Write a PL/SQL function to find sum of digits of a number
Function:
create or replace function sumofn(n in number) return number is
s number:=0;
i number:=n;
begin
while(i!=0) loop
s:=s+mod(i,10);
i:=floor(i/10); --Extract decimal part
end loop;
return s;
end sumofn;
Output:
Function created.
Program:
declare
n number;
r number;
begin
n:=&n;
r:=sumofn(n);
dbms_output.put_line('Sum of '||n||' digits is '||r);
end;
Output:
Enter value for n: 456
old 5: n:=&n;
new 5: n:=456;
Sum of 456 digits is 15
15. Write a PL/SQL recursive function to find factorial of a number
Procedure:
create or replace function rfact(n in number) return number is
f number:=1;
begin
if n=1 then
return 1;
else
f:=n*rfact(n-1);
return f;
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
55
Department of CSE Database Management system Lab Manual

end if;
end;
Output:
Function created
Query;
declare
n number;
r number;
begin
n:=&n;
r:=rfact(n);
dbms_output.put_line('Factorial of '||n||' is '||r);
end;
Output:
Enter value for n: 7
old 5: n:=&n;
new 5: n:=7;
Factorial of 7 is 5040

16. Explain Packages with PL/SQL program


Package Specification:
CREATE OR REPLACE PACKAGE DEMO AS
-- Function Specs goes here
FUNCTION fact(n in number) return number;
-- Procedure one specs goes here
PROCEDURE circle(radius in number, area out number, cir out number);
END DEMO;
Output:
Package created.
Package Body:
CREATE OR REPLACE PACKAGE BODY DEMO AS
FUNCTION fact (n in number) return number is
pno number(3):= 0;
c number:= 1;
BEGIN
pno:= n;
WHILE pno >= 1
Loop
c:= c * pno;
pno:= pno - 1;
END LOOP;
return c;
SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY
56
Department of CSE Database Management system Lab Manual

END fact;
PROCEDURE circle (radius in number, area out number, cir out number) AS
BEGIN
area:= (22/7) * radius * radius;
cir:= 2* (22/7) * radius;
END CIRCLE;
END DEMO;
Output:
Package body created.
Program:
DECLARE
x1 number:= 2; -- radius
f1 number:= 5; -- fact
x number:= 0;
sa number:= 0;
sb number:= 0;
BEGIN
x:= demo.FACT(f1);
dbms_output.put_line('Factorial of '||f1||' is '||x);
demo.CIRCLE(x1,sa,sb);
dbms_output.put_line('Area is '||sa||' '||'Circumference is '||sb);
END;
Output:
Factorial of 5 is 120
Area is 12.57142857142857142857142857142857142856
Circumference is 12.57142857142857142857142857142857142856

SREENIDHI INSTITUTE OF SCIENCE AND TECHNOLOGY


57

You might also like