Lab Manual For Database Management Systems: Sreenidhi Institute of Science & Technology
Lab Manual For Database Management Systems: Sreenidhi Institute of Science & Technology
2014-2015
DEPARTMENT OF
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.
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.
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.
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.
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.
COURSE FILE
For
For
B. Tech. II year – II SEM
DEPARTMENT
OF
x x x x
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)
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.
10. Program development using creation of package specification, package bodies, private
11. Develop programs using features parameters in a CURSOR, FOR UPDATE CURSOR,
12. Develop Programs using BEFORE and AFTER Triggers, Row and Statement Triggers and
INSTEAD OF Triggers
TEXT BOOKS:
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.
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
7. Create Sailors table with the following fields and describe the table (sid: integer, sname:
string, rating: integer, age: real)
11. Retrieve the names and ages of all sailors from the Sailors table
13. Create Sailors1 table with the following fields and describe the table (sid: integer, sname:
string, rating: integer, age: real)
19. Create Boats table with the following fields and describe the table (bid: integer, bname:
string, color: string)
22. Create Reserves table with the following fields and describe the table (sid: integer, bid: integer,
day: date)
List -1 Solutions
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
7. Create Sailors table with the following fields and describe the table (sid: integer, sname:
string, rating: integer, age: real)
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);
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:
13. Create Sailors1 table with the following fields and describe the table
(sid: integer, sname: string, rating: integer, age: real)
19. Create Boats table with the following fields and describe the table
(bid: integer, bname: string, color: string)
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:
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.
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
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:
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:
4. Find all sailors whose second letter in the names contain ‘u’
Query: select * from Sailors
where sname like '_u%'
Output:
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:
6. Find all sailors from sailors and sailors1 table by using (UNION operator)
Query: select * from Sailors
union select * from Sailors1
Output:
10 rows selected.
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
Query: select * from Sailors
minus select * from Sailors1
Output:
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:
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:
71 Zorba 10 16
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
COUNT(DISTINCT SNAME)
9
List – 3
LIST – 3 Solutions
SUM(RATING)
66
MAX(AGE)
55.5
SNAME
Lubber
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
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
11. Create Sailors table with the following fields and describe the table (sid: integer, sname:
string, rating: integer, age: real)
Query: create table reserves(sid number(2), bid number(3), day date,primary key(sid,bid))
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:
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
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
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
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
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 77895 3
2 44678 3
3 22456 2
4 24562 1
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:
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:
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:
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
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
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
20
select * from cal
Output:
Query:
select id,nullif(id,10),nvl(id,88),nvl2(id,66,88) from cal
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
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
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
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
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
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
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