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

DBMS Lab Manual

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

DBMS Lab Manual

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

LAB MANUAL

DATABASE MANAGEMENT SYSTEM LAB

DEPARTMENT OF CSE-AI & ML

SRK INSTITUTE OF TECHNOLOGY

ACADEMIC YEAR 2021-22

(II B.Tech I Sem R20 Regulation)

Prepared By

A. KALYAN KUMAR
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

L T P C
II Year – II Semester
0 0 3 1.5
DATABASE MANAGEMENT SYSTEMS LAB

Course Objectives:
This Course will enable students to
• Populate and query a database using SQL DDL/DML Commands
• Declare and enforce integrity constraints on a database
• Writing Queries using advanced concepts of SQL
• Programming PL/SQL including procedures, functions, cursors and triggers

Course Outcomes:
At the end of the course the student will be able to:
• Utilize SQL to execute queries for creating database and performing data
manipulation operations
• Examine integrity constraints to build efficient databases
• Apply Queries using Advanced Concepts of SQL
• Build PL/SQL programs including stored procedures, functions, cursors and triggers

List of Exercises:
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)
5.
i. Create a 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
Statements and write complex functions.
10. Develop programs using features parameters in a CURSOR, FOR
UPDATE CURSOR, WHERE CURRENT of clause and CURSOR variables.
11. Develop Programs using BEFORE and AFTER Triggers, Row and Statement
Triggers and INSTEAD OF Triggers
12. Create a table and perform the search operation on table using indexing and non-
indexing techniques.

Content beyond Syllabus:

1. Write a PL/SQL Code Bind and Substitution Variables. Printing in PL/SQL

2. Write a PL/SQL block using SQL and Control Structures in PL/SQL

3. Write a PL/SQL Code using Cursors, Exceptions and Composite Data Types

4. Write a PL/SQL Code using Procedures, Functions, and Packages FORMS


Program 1

AIM: a) Creation, altering and dropping of tables (DDL)

b) inserting, deleting, update, select on tables (DML)


a) Creation, altering and dropping of tables (DDL)

Q1) Create table sailors with sid as primary key.

SQL> create table sailors(sid integer,sname varchar2(20),primary key(sid));


Table created.

SQL> desc sailors;


Name Null? Type
----------------------------------------- -------- --------------
SID NOT NULL NUMBER(38)
SNAME VARCHAR2(20)

Q2) Add a new column age to the existing table sailors

SQL> alter table sailors add age number(3);

Table altered.

SQL> desc sailors;


Name Null? Type
----------------------------------------- -------- --------------
SID NOT NULL NUMBER(38)
SNAME VARCHAR2(20)
AGE NUMBER(3)

Q3) Add new columns height as number, weight as real to existing table sailors
SQL> alter table sailors add (height number(3),weight real);

Table altered.

SQL> desc sailors;


Name Null? Type
----------------------------------------- -------- --------------
SID NOT NULL NUMBER(38)
SNAME VARCHAR2(20)
AGE NUMBER(3)
HEIGHT NUMBER(3)
WEIGHT FLOAT(63)
Q4) Modify the data type of the sid attribute in the sailors table.
SQL> alter table sailors modify sid number(3);

Table altered.
SQL> desc sailors;
Name Null? Type
----------------------------------------- -------- -------------
SID NOT NULL NUMBER(3)
SNAME VARCHAR2(20)
AGE NUMBER(3)
HEIGHT NUMBER(3)
WEIGHT FLOAT(63)
Q5) Modify the data types of name and weight in sailors table.

SQL> alter table sailors


2 modify(sname varchar2(10),weight number(5,2));

Table altered.

SQL> desc sailors;


Name Null? Type
----------------------------------------- -------- ------------
SID NOT NULL NUMBER(3)
SNAME VARCHAR2(10)
AGE NUMBER(3)
HEIGHT NUMBER(3)
WEIGHT NUMBER(5,2)

Q6) Add not not constraint to sname in sailors table.

SQL> alter table sailors


2 modify sname varchar2(20) not null;

Table altered.

SQL> desc sailors;


Name Null? Type
----------------------------------------- -------- --------------
SID NOT NULL NUMBER(3)
SNAME NOT NULL VARCHAR2(20)
AGE NUMBER(3)
HEIGHT NUMBER(3)
WEIGHT NUMBER(5,2)

Q7) Drop the column age in sailors table.


SQL> alter table sailors drop column age;

Table altered.

SQL> desc sailors;


Name Null? Type
----------------------------------------- -------- ----------------
SID NOT NULL NUMBER(3)
SNAME NOT NULL VARCHAR2(20)
HEIGHT NUMBER(3)
WEIGHT NUMBER(5,2)

Q8) Create table boats (bid,bname,color)

SQL> create table boats(bid number(3),bname varchar2(15),


2 color varchar2(10));

Table created.

SQL> desc boats;


Name Null? Type
----------------------------------------- -------- --------------
BID NUMBER(3)
BNAME VARCHAR2(15)
COLOR VARCHAR2(10)

Q9) Rename column bname to boat_name in boats table.

SQL> alter table boats


2 rename column bname to boat_name;

Table altered.

SQL> desc boats;


Name Null? Type
----------------------------------------- -------- -------------
BID NUMBER(3)
BOAT_NAME VARCHAR2(15)
COLOR VARCHAR2(10)

Q10) Add primary key constraint on bid in boats table.

SQL> alter table boats add constraint pky primary key(bid);

Table altered.
Q11) Add unique key constraint on bname in boats table

SQL> alter table boats add constraint uni unique(bname);

Table altered.

Q12) Add a check constraint on color which accepts either red, green or blue color in boats
table.

SQL> alter table boats


2 add constraint chk check(color in('red','green','blue'));

Table altered.

Q13) Display all constraints on table boats

SQL> select constraint_name,constraint_type


2 from user_constraints
3 where table_name like 'BOATS';

CONSTRAINT_NAME C
------------------------------ -
PKY P
UNI U
CHK C

Q14) Drop a constraint whose name is chk from the table boats.

SQL> alter table boats drop constraint chk;

Table altered.

SQL> select constraint_name,constraint_type


2 from user_constraints
3 where table_name like 'BOATS';

CONSTRAINT_NAME C
------------------------------ -
PKY P
UNI U

Q15) Create table reserves(sid,bid,day)

SQL> create table reserves(sid number(3),bid number(3),


2 day date default sysdate);
Table created.

Q16) Add foreign key constraints on sid,bin in reserves referencing tables sailors and
boats.

SQL> alter table reserves


2 add constraint fky foreign key(sid) references sailors(sid);

Table altered.

SQL> alter table reserves


2 add constraint fky2 foreign key(bid) references boats;

Table altered.

SQL> select constraint_name,constraint_type


2 from user_constraints
3 where table_name like 'RESERVES';

CONSTRAINT_NAME C
------------------------------ -
FKY R
FKY2 R

Q17) Display all the tables present the database.

SQL> select * from tab;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
BOATS TABLE
RESERVES TABLE
SAILORS TABLE

Q18) Rename sailors table to new_sailors.

SQL> rename sailors to new_sailors;

Table renamed.

SQL> select * from tab;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
BOATS TABLE
NEW_SAILORS TABLE
RESERVES TABLE
Q19) Drop the table reserves.

SQL> drop table reserves;

Table dropped.

SQL> select * from tab;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
BOATS TABLE
NEW_SAILORS TABLE

Q20) Insert values into table boats.

SQL> insert into boats values (&id,'&name','&color');


Enter value for id: 101
Enter value for name: rockz
Enter value for color: red
old 1: insert into boats values (&id,'&name','&color')
new 1: insert into boats values (101,'rockz','red')

1 row created.

SQL> /
Enter value for id: 103
Enter value for name: ranger
Enter value for color: blue
old 1: insert into boats values (&id,'&name','&color')
new 1: insert into boats values (103,'ranger','blue')

1 row created.

SQL> select * from boats;

BID BNAME COLOR


---------- --------------- ----------
101 rockz red
103 ranger blue

Q21) Remove all the rows in the table boats (by using ‘truncate’).

SQL> truncate table boats;

Table truncated.
SQL> select * from boats;

no rows selected

SQL> select * from tab;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
BOATS TABLE
NEW_SAILORS TABLE

SQL> commit;
SQL> exit;

B) Inserting,Deleting,Update,Select On Tables (DML)

Q1) Create table student(sid,sname,mob_no)

SQL> create table student(sid number(2),sname varchar2(10), mob_no number(10));

Table created.

Q2) Insert records into student table

SQL> insert into student values(11,'rayudu',9988776655);

1 row created.

SQL> select * from student;

SID SNAME MOB_NO


---------- ---------- ----------
11 rayudu 9988776655

SQL> insert into student values(&id,'&name',&mob);


Enter value for id: 22
Enter value for name: sudha
Enter value for mob: 9977886655
old 1: insert into student values(&id,'&name',&mob)
new 1: insert into student values(22,'sudha',9977886655)
1 row created.

SQL> /
Enter value for id: 33
Enter value for name: sridhar
Enter value for mob: 9966778855
old 1: insert into student values(&id,'&name',&mob)
new 1: insert into student values(33,'sridhar',9966778855)

1 row created.

SQL> select * from student;

SID SNAME MOB_NO


---------- ---------- ----------
11 rayudu 9988776655
22 sudha 9977886655
33 sridhar 9966778855

Q3) Insert the values for the columns sid,sname only.

SQL> insert into student(sid,sname) values(44,'sudheer');

1 row created.

SQL> select * from student;

SID SNAME MOB_NO


---------- ---------- ----------
11 rayudu 9988776655
22 sudha 9977886655
33 sridhar 9966778855
44 sudheer

SQL> insert into student(sid,mob_no) values(55,9988667755);

1 row created.

SQL> select * from student;

SID SNAME MOB_NO


---------- ---------- ----------
11 rayudu 9988776655
22 sudha 9977886655
33 sridhar 9966778855
44 sudheer
55 9988667755

Q4) create a table std_mob (sid,mob_no) from student table with records also.

SQL> create table std_mob(sid number(2),mob_no number(10));

Table created.

SQL> insert into std_mob (select sid,mob_no from student);

5 rows created.

SQL> select * from std_mob;

SID MOB_NO
---------- ----------
11 9988776655
22 9977886655
33 9966778855
44
55 9988667755

Q5) create a duplicate table from student table with records

SQL> create table dup_std_all as (select * from student);

Table created.

SQL> select * from dup_std_all;

SID SNAME MOB_NO


---------- ---------- ----------
11 rayudu 9988776655
22 sudha 9977886655
33 sridhar 9966778855
44 sudheer
55 9988667755

Q6) create table sailors(sid,sname,rating,age) with sid as primary key.

SQL> create table sailors(sid number(3),sname varchar2(15),rating number(2),age


number(5,2),primary key(sid));
Table created.

Q7) insert values into sailors.

SQL> insert into sailors (sid, sname, rating, age)


2 values (22, 'Dustin', 7, 45.0);

1 row created.

SQL> insert into sailors (sid, sname, rating, age)


2 values (29, 'Brutus', 1, 33.0);

1 row created.

SQL> insert into sailors (sid, sname, rating, age)


2 values (31, 'Lubber', 8, 55.5);

1 row created.

SQL> insert into sailors (sid, sname, rating, age)


2 values (32, 'Andy', 8, 25.5);

1 row created.

SQL> insert into sailors (sid, sname, rating, age)


2 values (58, 'Rusty', 10, 35.0);

1 row created.

SQL> insert into sailors (sid, sname, rating, age)


2 values (64, 'Horatio', 7, 35.0);

1 row created.

SQL> insert into sailors (sid, sname, rating, age)


2 values (71, 'Zorba', 10, 16.0);

1 row created.

SQL> insert into sailors (sid, sname, rating, age)


2 values (74, 'Horatio', 9, 35.0);

1 row created.

SQL> insert into sailors (sid, sname, rating, age)


2 values (85, 'Art', 3, 25.5);
1 row created.

SQL> insert into sailors (sid, sname, rating, age)


2 values (95, 'Bob', 3, 63.5);

1 row created.

Q8) Find all sailor names from sailors.

SQL> select sname from sailors;

SNAME
---------------
Dustin
Brutus
Lubber
Andy
Rusty
Horatio
Zorba
Horatio
Art
Bob

10 rows selected.

Q9) Find all distinct sailor names from sailors.

SQL> select distinct(sname) from sailors;

SNAME
---------------
Andy
Art
Bob
Brutus
Dustin
Horatio
Lubber
Rusty
Zorba

9 rows selected.

Q10) Find sailor names and ratings whose rating is 7.


SQL> select sname,rating from sailors where rating=7

SNAME RATING
--------------- ----------
Dustin 7
Horatio 7

Q11) Find sailor names and ratings whose rating is greater than 7.

SQL> select sname,rating from sailors where rating>7;

SNAME RATING
--------------- ----------
Lubber 8
Andy 8
Rusty 10
Zorba 10
Horatio 9

Q12) Find sailor names and rating whose rating is less than 6 using not.

SQL> select sname,rating from sailors where not(rating>6);

SNAME RATING
--------------- ----------
Brutus 1
Art 3
Bob 3

Q13) Find sailor names and rating whose rating is between 3 and 7.

SQL> select sname,rating from sailors where rating between 3 and 7;

SNAME RATING
--------------- ----------
Dustin 7
Horatio 7
Art 3
Bob 3

Q14) Find sailor names and rating whose name starts with B.

SQL> select sname,rating from sailors where sname like 'B%';

SNAME RATING
--------------- ----------
Brutus 1
Bob 3

Q15) Find sailor names and rating whose name ends with y.

SQL> select sname,rating from sailors where sname like '%y';

SNAME RATING
--------------- ----------
Andy 8
Rusty 10

Q15) Find sailor names and rating whose name contains the substring or.

SQL> select sname,rating from sailors where sname like '%or%';

SNAME RATING
--------------- ----------
Horatio 7
Zorba 10
Horatio 9

Q16) Find sailor names and rating whose name contains exactly 3 characters.

SQL> select sname,rating from sailors where sname like '_ _ _';

SNAME RATING
--------------- ----------
Art 3
Bob 3

Q17) Find sailor names and rating whose name does not contains letter r.

SQL> select sname,rating from sailors where sname not like '%r%';

SNAME RATING
--------------- ----------
Dustin 7
Andy 8
Rusty 10
Bob 3

Q18) Diasplay all sailor names and rating whose name does not contains letter r in
increasing order of rating
SQL> select sname,rating from sailors where sname not like '%r%' order by rating;

SNAME RATING
--------------- ----------
Bob 3
Dustin 7
Andy 8
Rusty 10

Q18) Display sailor names and rating whose name does not contains letter r and display in
decreasing order of rating.

SQL> select sname,rating from sailors where sname not like '%r%' order by rating desc;

SNAME RATING
--------------- ----------
Rusty 10
Andy 8
Dustin 7
Bob 3

Q19) Display sailor names and rating whose rating is 8 in alphabetical order.

SQL> select sname,rating from sailors where rating=8 order by sname;

SNAME RATING
--------------- ----------
Andy 8
Lubber 8

Q20) select sailor name and rating whose rating is either greater than 7 or rating less than
3

SQL> select sname,rating from sailors where rating>7 or rating<3;

SNAME RATING
--------------- ----------
Brutus 1
Lubber 8
Andy 8
Rusty 10
Zorba 10
Horatio 9

6 rows selected.
Q21) create table boats(bid,bname,color) with bid as primary key.

SQL> create table boats(bid integer, bname varchar(10),color varchar(10),


2 primary key(bid));

Table created.

Q22) insert values into boats.

SQL> insert into boats (bid, bname, color)


2 values (101, 'Interlake', 'blue');
1 row created.
SQL> insert into boats (bid, bname, color)
2 values (102, 'Interlake', 'red');
1 row created.
SQL> insert into boats (bid, bname, color)
2 values (103, 'Clipper', 'green');

1 row created.

SQL> insert into boats (bid, bname, color)


2 values (104, 'Marine', 'red');

1 row created.

Q23) Create table reserves(sid,bid,day) where sid,bid,day as primary key and sid,did as
foreign key referencing the tables sailors and boats respectively.

SQL> create table reserves(sid integer,bid integer,day date,


2 primary key(sid,bid,day),
3 foreign key(sid) references sailors,
4 foreign key(bid) references boats);

Table created.

Q24) Insert values into reserves.

SQL> insert into reserves (sid, bid, day)


2 values (22, 101, '10/oct/98');

1 row created.

SQL> insert into reserves (sid, bid, day)


2 values (22, 102, '10/oct/98');
1 row created.

SQL> insert into reserves (sid, bid, day)


2 values (22, 103, '10/aug/98');

1 row created.

SQL> insert into reserves (sid, bid, day)


2 values (22, 104, '10/jul/98');

1 row created.

SQL> insert into reserves (sid, bid, day)


2 values (31, 102, '11/oct/98');

1 row created.

SQL> insert into reserves (sid, bid, day)


2 values (31, 103, '11/jun/98');

1 row created.
SQL> insert into reserves (sid, bid, day)
2 values (31, 104, '11/dec/98');
1 row created.
SQL> insert into reserves (sid, bid, day)
2 values (64, 101, '9/may/98');

1 row created.

SQL> insert into reserves (sid, bid, day)


2 values (64, 102, '9/aug/98');

1 row created.

SQL> insert into reserves (sid, bid, day)


2 values (74, 103, '9/aug/98');
1 row created.

Q25) Find the sailor names who have reserved atleast one boat.

SQL> select a.sname,b.bid from sailors a, reserves b where a.sid=b.sid;

SNAME BID
--------------- ----------
Dustin 101
Dustin 102
Dustin 103
Dustin 104
Lubber 102
Lubber 103
Lubber 104
Horatio 101
Horatio 102
Horatio 103

10 rows selected.

Q26) Find the sailors name who have reserved boat number 103.

SQL> select a.sname,b.bid from sailors a, reserves b where a.sid=b.sid and b.bid=103;

SNAME BID
--------------- ----------
Dustin 103
Lubber 103
Horatio 103

Q27) Find the sailors name who have reserved boat ‘Marine’.

SQL> select a.sname,b.bid from sailors a, reserves b,boats c where a.sid=b.sid and b.bid=c.bid
and c.bname='Marine';

SNAME BID
--------------- ----------
Dustin 104
Lubber 104
Q28) Find the sailors who have reserved red color boat.

SQL> select a.sname, a.rating, c.bname from sailors a, reserves b,boats c where a.sid=b.sid and
b.bid=c.bid and c.color='red'
SNAME RATING BNAME
--------------- ---------- ----------
Dustin 7 Interlake
Dustin 7 Marine
Lubber 8 Interlake
Lubber 8 Marine
Horatio 7 Interlake

Q29) Find the sailors who have reserved either red or blue color boat.

SQL> (select a.sname,c.bname,c.color from sailors a, reserves b,boats c


2 where a.sid=b.sid and b.bid=c.bid and c.color='red')
3 union
4 (select a.sname,c.bname,c.color from sailors a, reserves b,boats c
5 where a.sid=b.sid and b.bid=c.bid and c.color='blue');
SNAME BNAME COLOR
--------------- ---------- ----------
Dustin Interlake blue
Dustin Interlake red
Dustin Marine red
Horatio Interlake blue
Horatio Interlake red
Lubber Interlake red
Lubber Marine red
7 rows selected.

Q30) Find the sailor names who have reserved both red and blue color boats.

SQL> (select a.sname from sailors a, reserves b,boats c


2 where a.sid=b.sid and b.bid=c.bid and c.color='red')
3 intersect
4 (select a.sname from sailors a, reserves b,boats c
5 where a.sid=b.sid and b.bid=c.bid and c.color='blue');
SNAME
---------------
Dustin
Horatio

Q31) Find the sailor names who have reserved red color boat but not blue color boat.
SQL> (select a.sname from sailors a, reserves b,boats c
2 where a.sid=b.sid and b.bid=c.bid and c.color='red')
3 minus
4 (select a.sname from sailors a, reserves b,boats c
5 where a.sid=b.sid and b.bid=c.bid and c.color='blue');
SNAME
---------------
Lubber
Q32) Find all the sailor names who reserved either red or blue color boats.
SQL> (select a.sname from sailors a, reserves b,boats c
2 where a.sid=b.sid and b.bid=c.bid and c.color='red')
3 union
4 (select a.sname from sailors a, reserves b,boats c
5 where a.sid=b.sid and b.bid=c.bid and c.color='blue');

SNAME
---------------
Dustin
Horatio
Lubber

Q33) Find all the sailor names(with duplicates) who reserved either red or blue color boats.
SQL> (select a.sname from sailors a, reserves b,boats c
2 where a.sid=b.sid and b.bid=c.bid and c.color='red')
3 union all
4 (select a.sname from sailors a, reserves b,boats c
5 where a.sid=b.sid and b.bid=c.bid and c.color='blue');

SNAME
---------------
Dustin
Dustin
Lubber
Lubber
Horatio
Dustin
Horatio

7 rows selected.

Q34) Find the sailors who have reserved atleast one boat using nested query.
SQL> select * from sailors where sid in (select sid from reserves);

SID SNAME RATING AGE


---------- --------------- ---------- ----------
22 Dustin 7 45
31 Lubber 8 55.5
64 Horatio 7 35
74 Horatio 9 35

Q35) Find the sailors who have not reserved atleast one boat using nested query.

SQL> select * from sailors where sid not in (select sid from reserves);

SID SNAME RATING AGE


---------- --------------- ---------- ----------
29 Brutus 1 33
32 Andy 8 25.5
58 Rusty 10 35
71 Zorba 10 16
85 Art 3 25.5
95 Bob 3 63.5

6 rows selected.

Q36) Find the sailors whose sid is 22 or 32.


SQL> select * from sailors where sid in (22,32);

SID SNAME RATING AGE


---------- --------------- ---------- ----------
32 Andy 8 25.5
22 Dustin 7 45

Q36) Find the sailors whose who have reserved boat number 103 using nested query.

SQL> select * from sailors where sid in


2 (select sid from reserves where bid=103);

SID SNAME RATING AGE


---------- --------------- ---------- ----------
22 Dustin 7 45
31 Lubber 8 55.5
74 Horatio 9 35

Q37) Find the sailors who have not reserved red color boat.

SQL> select * from sailors where sid not in


2 (select sid from reserves where bid in
3 (select bid from boats where color='red'));

SID SNAME RATING AGE


---------- --------------- ---------- ----------
29 Brutus 1 33
32 Andy 8 25.5
58 Rusty 10 35
71 Zorba 10 16
74 Horatio 9 35
85 Art 3 25.5
95 Bob 3 63.5

7 rows selected.

Q38) Find the sailor names who reserved boat number 103

SQL> select a.sname from sailors a where exists


2 (select * from reserves b where b.bid=103 and a.sid=b.sid);

SNAME
---------------
Dustin
Lubber
Horatio
Q39) Find the sailor names who do not reserved boat number 103

SQL> select a.sname from sailors a where not exists


2 (select * from reserves b where b.bid=103 and a.sid=b.sid);

SNAME
---------------
Brutus
Andy
Rusty
Horatio
Zorba
Art
Bob

7 rows selected.

Q40) Find the sailor names who reserved all the boats.

SQL> select a.sname from sailors a where not exists


2 ((select b.bid from boats b)
3 minus
4 (select c.bid from reserves c where a.sid=c.sid));

SNAME
---------------
Dustin

Q41) Find all the boat details

SQL> select * from boats;

BID BNAME COLOR


---------- ---------- ----------
101 Interlake blue
102 Interlake red
103 Clipper green
104 Marine red

Q42) Modify the color to yellow whose boat id=101.

SQL> update boats set color='yellow' where bid=101;


1 row updated.

SQL> select * from boats;

BID BNAME COLOR


---------- ---------- ----------
101 Interlake yellow
102 Interlake red
103 Clipper green
104 Marine red

Q43)Modify the color to yellow whose boat id<=103

SQL> update boats set color='yellow' where bid<=103;

3 rows updated.

SQL> select * from boats;

BID BNAME COLOR


---------- ---------- ----------
101 Interlake yellow
102 Interlake yellow
103 Clipper yellow
104 Marine red
Q44)Modify the color to green whose boat id not less than or equal to 102

SQL> update boats set color='green' where not(bid<=102);

2 rows updated.

SQL> select * from boats;

BID BNAME COLOR


---------- ---------- ----------
101 Interlake yellow
102 Interlake yellow
103 Clipper green
104 Marine green

Q45)Modify the color of boat to pink if bid<=2,red if bid=3


orange otherwise

SQL> update boats set color=case


2 when bid<=102 then 'red'
3 when bid=103 then 'white'
4 else 'blue'
5 end;
4 rows updated.
Q46)Modify all boat names to ranger.

SQL> update boats set bname='ranger';

4 rows updated.

SQL> select * from boats;

BID BNAME COLOR


---------- ---------- ----------
101 ranger red
102 ranger red
103 ranger white
104 ranger blue

Q46) Modify name to marine and color to pink for boats whose id>=103

SQL> update boats set bname='marine',color='pink' where bid>=103;


2 rows updated.

SQL> select * from boats;

BID BNAME COLOR


---------- ---------- ----------
101 ranger red
102 ranger red
103 marine pink
104 marine pink

Q47) Create a table employee(eid,ename,salary) and insert values into it.

SQL> create table employee(eid number(2),ename varchar2(10),


2 salary number(10,2));

Table created.

SQL> insert into employee values(&id,'&name',&sal);


Enter value for id: 22
Enter value for name: sudhakar
Enter value for sal: 30000
old 1: insert into employee values(&id,'&name',&sal)
new 1: insert into employee values(22,'sudhakar',30000)
1 row created.

SQL> /
Enter value for id: 66
Enter value for name: praneeth
Enter value for sal: 32000
old 1: insert into employee values(&id,'&name',&sal)
new 1: insert into employee values(66,'praneeth',32000)

1 row created.

SQL> /
Enter value for id: 55
Enter value for name: rajesh
Enter value for sal: 33000
old 1: insert into employee values(&id,'&name',&sal)
new 1: insert into employee values(55,'rajesh',33000)

1 row created.

SQL> select * from employee;

EID ENAME SALARY


---------- ---------- ----------
22 sudhakar 30000
66 praneeth 32000
55 rajesh 33000

Q48) Delete an employee whose eid is 22.

SQL> delete from employee where eid=22;

1 row deleted.

SQL> select * from employee;

EID ENAME SALARY


---------- ---------- ----------
66 praneeth 32000
55 rajesh 33000

Q49)Delete all the employees.

SQL> delete from employee;


2 rows deleted.

SQL> select * from employee;

no rows selected

SQL> commit;

Commit complete.

SQL> exit;

Program 2

Aim: Queries (along with sub Queries) using ANY, ALL, IN, EXISTS,
NOTEXISTS, UNION, INTERSECT, Constraints.

Find the sailor names who have reserved atleast one boat.

SQL> select a.sname,b.bid from sailors a, reserves b where a.sid=b.sid;

SNAME BID
--------------- ----------
Dustin 101
Dustin 102
Dustin 103
Dustin 104
Lubber 102
Lubber 103
Lubber 104
Horatio 101
Horatio 102
Horatio 103

10 rows selected.

Find the sailors who have reserved atleast one boat using nested query.
SQL> select * from sailors where sid in (select sid from reserves);

SID SNAME RATING AGE


---------- --------------- ---------- ----------
22 Dustin 7 45
31 Lubber 8 55.5
64 Horatio 7 35
74 Horatio 9 35

Find the sailors who have not reserved atleast one boat using nested query.

SQL> select * from sailors where sid not in (select sid from reserves);

SID SNAME RATING AGE


---------- --------------- ---------- ----------
29 Brutus 1 33
32 Andy 8 25.5
58 Rusty 10 35
71 Zorba 10 16
85 Art 3 25.5
95 Bob 3 63.5

6 rows selected.

Find the sailors name who have reserved boat number 103.

SQL> select a.sname,b.bid from sailors a, reserves b where a.sid=b.sid and b.bid=103;

SNAME BID
--------------- ----------
Dustin 103
Lubber 103
Horatio 103

Find the sailors whose who have reserved boat number 103 using nested query.
SQL> select * from sailors where sid in
2 (select sid from reserves where bid=103);

SID SNAME RATING AGE


---------- --------------- ---------- ----------
22 Dustin 7 45
31 Lubber 8 55.5
74 Horatio 9 35

Find the sailor names who reserved boat number 103 using exists

SQL> select a.sname from sailors a where exists


2 (select * from reserves b where b.bid=103 and a.sid=b.sid);

SNAME
---------------
Dustin
Lubber
Horatio

Find the sailors name who have reserved boat ‘Marine’.

SQL> select a.sname,b.bid from sailors a, reserves b,boats c where a.sid=b.sid and b.bid=c.bid
and c.bname='Marine';

SNAME BID
--------------- ----------
Dustin 104
Lubber 104

Find the sailors who have reserved red color boat.

SQL> select a.sname, a.rating, c.bname from sailors a, reserves b,boats c where a.sid=b.sid and
b.bid=c.bid and c.color='red'
SNAME RATING BNAME
--------------- ---------- ----------
Dustin 7 Interlake
Dustin 7 Marine
Lubber 8 Interlake
Lubber 8 Marine
Horatio 7 Interlake
Q. Find the names of sailors who have not reserved boat 103.

SQL> select sname from sailors where sid not in (select sid from reserves where bid=103);

SNAME
Brutus

Andy

John

Horatio

Zorba

Art

Bob

Q. Find the sailors name and rating where rating is greater than bob and Andy using all.

SQL>

Select sname, rating from sailors where rating>all(select rating from sailors where sname like
‘Bob’ or sname like ‘Andy’);

SNAME RATING

Rusty 10

Zorba 10

Horatio 9

Q. find the sailors name and rating whose rating is greater than Bob and Andy using any.

SQL> select sname, rating from sailors where not rating <=any(select rating from sailors where
sname=’Bob’ or sname=’Andy’);

SNAME RATING

Rusty 10

Zorba 10

Horatio 9

Q. Find the sailors ids,names who have reserved either red or the blue color boat using union
SQL> select sname from sailors s,reserves r,boats b where b.color like ‘red’ and b.bid=r.bid and
r.sid=s.sid union(select sname from sailors s,reserves r,boats b where b.color like ‘blue’ and
b.bid=r.bid and r.sid=s.sid);

SNAME

Dustin

Lubber

Q.find the sailors names who reserved both red and blue color boats using intersect

SQL>select sname from sailors s,reserves r,boats b where b.color=’red’ and b.bid=r.bid and
r.sid=s.sid

Intersect (select sname from sailors s,reserves r,boats b where b.color=’blue’ and b.bid=r.bid and
r.sid=s.sid);

SNAME

Dustin
3. Views

a. Creation and dropping of Views.

Q1) Create a table employee(sid,sname,salary,age)and insert records

SQL> create table employee(eid number(2),ename varchar2(15),


2 salary number(10,2),age number(3));

Table created.

SQL> insert into employee values(&id,'&name',&sal,&age);


Enter value for id: 11
Enter value for name: sudhakar
Enter value for sal: 25000.55
Enter value for age: 32
old 1: insert into employee values(&id,'&name',&sal,&age)
new 1: insert into employee values(11,'sudhakar',25000.55,32)

1 row created.

SQL> /
Enter value for id: 66
Enter value for name: praneeth
Enter value for sal: 26005.25
Enter value for age: 28
old 1: insert into employee values(&id,'&name',&sal,&age)
new 1: insert into employee values(66,'praneeth',26005.25,28)

1 row created.

SQL> select * from employee;

EID ENAME SALARY AGE


---------- --------------- ---------- ----------
11 sudhakar 25000.55 32
66 praneeth 26005.25 28

Q2) Create a view emp(sname,age)from table employee

SQL> create view emp as (select ename,age from employee);

View created.

SQL> select * from tab;


TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
EMP VIEW
EMPLOYEE TABLE

SQL> select * from emp;


ENAME AGE
--------------- ----------
sudhakar 32
praneeth 28
Q3)Insert a new record in employee and verify it in view emp

SQL> insert into employee values(55,'suresh',25005.25,28);

1 row created.

SQL> select * from emp;

ENAME AGE
--------------- ----------
sudhakar 32
praneeth 28
suresh 28

Q4)Update a record in employee and verify it in view emp

SQL> update employee set ename='sudha' where eid=11;

1 row updated.

SQL> select * from emp;

ENAME AGE
--------------- ----------
sudha 32
praneeth 28
suresh 28

Q5)Delete a record in employee and verify it in view emp

SQL> delete from employee where ename='praneeth';

1 row deleted.

SQL> select * from emp;

ENAME AGE
--------------- ----------
sudha 32
suresh 28

Q6)Insert a new record in view emp and verify it in table employee

SQL> insert into emp values('ramesh',35);

1 row created.

SQL> select * from employee;

EID ENAME SALARY AGE


---------- --------------- ---------- ----------
11 sudha 25000.55 32
55 suresh 25005.25 28
ramesh 35
Q7)Update a record in view emp and verify it in table employee

SQL> update emp set ename='sudhakar reddy' where ename='sudha';

1 row updated.

SQL> select * from employee;

EID ENAME SALARY AGE


---------- --------------- ---------- ----------
11 sudhakar reddy 25000.55 32
55 suresh 25005.25 28
ramesh 35

Q8)Delete a record in view emp and verify it in table employee

SQL> delete from emp where ename='sudhakar reddy';

1 row deleted.

SQL> select * from employee;

EID ENAME SALARY AGE


---------- --------------- ---------- ----------
55 suresh 25005.25 28
ramesh 35

Q9)Change schema of the view emp

SQL> create or replace view emp as (select eid,ename,age from employee);


View created.

SQL> desc emp;


Name Null? Type
----------------------------------------- -------- --------------
EID NUMBER(2)
ENAME VARCHAR2(15)
AGE NUMBER(3)

SQL> select * from emp;

EID ENAME AGE


---------- --------------- ----------
55 suresh 28
ramesh 35

Q10)Change the name of view emp to em

SQL> rename emp to em;


Table renamed.

SQL> select * from tab;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
EM VIEW
EMPLOYEE TABLE

Q11) Drop the view em

SQL> drop view em;

View dropped.

SQL> select * from tab;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
EMPLOYEE TABLE

SQL> commit;

SQL> exit;

Creation and dropping of Views.


Q1) Create a table employee(sid,sname,salary,age)and insert records

SQL> create table employee(eid number(2),ename varchar2(15),


2 salary number(10,2),age number(3));

Table created.

SQL> insert into employee values(&id,'&name',&sal,&age);


Enter value for id: 11
Enter value for name: sudhakar
Enter value for sal: 25000.55
Enter value for age: 32
old 1: insert into employee values(&id,'&name',&sal,&age)
new 1: insert into employee values(11,'sudhakar',25000.55,32)

1 row created.

SQL> /
Enter value for id: 66
Enter value for name: praneeth
Enter value for sal: 26005.25
Enter value for age: 28
old 1: insert into employee values(&id,'&name',&sal,&age)
new 1: insert into employee values(66,'praneeth',26005.25,28)

1 row created.

SQL> select * from employee;

EID ENAME SALARY AGE


---------- --------------- ---------- ----------
11 sudhakar 25000.55 32
66 praneeth 26005.25 28

Q2) Create a view emp(sname,age)from table employee

SQL> create view emp as (select ename,age from employee);

View created.

SQL> select * from tab;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
EMP VIEW
EMPLOYEE TABLE
SQL> select * from emp;
ENAME AGE
--------------- ----------
sudhakar 32
praneeth 28
Q3)Insert a new record in employee and verify it in view emp

SQL> insert into employee values(55,'suresh',25005.25,28);

1 row created.

SQL> select * from emp;

ENAME AGE
--------------- ----------
sudhakar 32
praneeth 28
suresh 28

Q4)Update a record in employee and verify it in view emp

SQL> update employee set ename='sudha' where eid=11;

1 row updated.

SQL> select * from emp;

ENAME AGE
--------------- ----------
sudha 32
praneeth 28
suresh 28

Q5)Delete a record in employee and verify it in view emp

SQL> delete from employee where ename='praneeth';

1 row deleted.

SQL> select * from emp;

ENAME AGE
--------------- ----------
sudha 32
suresh 28
Q6)Insert a new record in view emp and verify it in table employee

SQL> insert into emp values('ramesh',35);

1 row created.

SQL> select * from employee;

EID ENAME SALARY AGE


---------- --------------- ---------- ----------
11 sudha 25000.55 32
55 suresh 25005.25 28
ramesh 35
Q7)Update a record in view emp and verify it in table employee

SQL> update emp set ename='sudhakar reddy' where ename='sudha';

1 row updated.

SQL> select * from employee;

EID ENAME SALARY AGE


---------- --------------- ---------- ----------
11 sudhakar reddy 25000.55 32
55 suresh 25005.25 28
ramesh 35

Q8)Delete a record in view emp and verify it in table employee

SQL> delete from emp where ename='sudhakar reddy';

1 row deleted.

SQL> select * from employee;

EID ENAME SALARY AGE


---------- --------------- ---------- ----------
55 suresh 25005.25 28
ramesh 35

Q9)Change schema of the view emp

SQL> create or replace view emp as (select eid,ename,age from employee);


View created.

SQL> desc emp;


Name Null? Type
----------------------------------------- -------- --------------
EID NUMBER(2)
ENAME VARCHAR2(15)
AGE NUMBER(3)

SQL> select * from emp;

EID ENAME AGE


---------- --------------- ----------
55 suresh 28
ramesh 35

Q10)Change the name of view emp to em

SQL> rename emp to em;


Table renamed.

SQL> select * from tab;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
EM VIEW
EMPLOYEE TABLE

Q11) Drop the view em

SQL> drop view em;

View dropped.

SQL> select * from tab;

TNAME TABTYPE CLUSTERID


------------------------------ ------- ----------
EMPLOYEE TABLE

SQL> commit;

SQL> exit;
Program 4

Aim: Queries to facilitate acquaintance of Built-In Functions, String Functions,


Numeric Functions, Date Functions and Conversion Functions.
1 .Character Functions:

Upper Returns char with all letters into upper case

lower Converts the mixed case or uppercase character strings to lowercase

Converts the first letter of each word to upper case and remaining letters to
Initcap
lowercase

Concat Joins values together you are limited to two arguments with concat

Substr This extracts a string of determined length

Length Shows the length of a string as a numeric value

Instr Finds numeric position of named character

Lpad Pads the character value right justified

rpad Pads the character value left justified

Trim Trims heading or trailing characters from a character string

Raplace To replace a set of character (String based)

Translate Change a character to a new described character(character based)


SQL> select upper ('oracle') "UPPER" from dual;
UPPER
------
ORACLE

SQL> select lower ('ORACLE') "LOWER" from dual;


LOWER
------
Oracle

SQL> select initcap('sql functions') "initcap" from dual;


initcap
-------------
Sql Functions

SQL> select concat('sql','functions') from dual;


CONCAT('SQL'
------------
Sqlfunctions

SQL> select substr('sqlfunctions',1,5) from dual;


SUBST
-----
Sqlfu

SQL> select substr('sqlfunctions',2,5) from dual;


SUBST
-----
qlfun
SQL> select substr('sqlfunctions',4,5) from dual;
SUBST
-----
funct

SQL> select length('sqlfunctions') from dual;

LENGTH('SQLFUNCTIONS')
----------------------
12

SQL> select instr('sqlfunctions','f') from dual;


INSTR('SQLFUNCTIONS','F')
-------------------------
4

SQL> select lpad(sal,15,'*') from emp;


LPAD(SAL,15,'*'
---------------
************800
***********1600
***********1250
***********2975
***********1250
***********2850
SQL> select rpad(sal,15,'*') from emp;
RPAD(SAL,15,'*'
---------------
800************
1600***********
1250***********
2975***********

SQL> select trim('s' from 'ssmiths') from dual;


TRIM
----
mith
SQL> select ltrim('ssmiths','s') from dual;
LTRIM
-----
miths
SQL> select rtrim('ssmiths','s') from dual;
RTRIM(
------
ssmith
SQL> select replace('jack and jue','j','bl') from dual;
REPLACE('JACKA
--------------
black and blue
SQL> select translate('jack','j','b') from dual;
TRAN
----
Back

Number Functions:

Round Rounds the value to specified decimal

Trunc Truncates the column, expression, or value to n decimal places


Power Calculates the power of the given value

Mod Finds the remainder of value1 divided by value1

Ceil Takes the height decimal value

Floor Takes the lowest decimal value

SQL> select round(35.823,2), round(35.823,0), round(35.823,-1) from dual;


ROUND(35.823,2) ROUND(35.823,0) ROUND(35.823,-1)
--------------- --------------- ----------------
35.82 36 40

SQL> select trunc(35.823,2), trunc(35.823), trunc(35.823,-2) from dual;


TRUNC(35.823,2) TRUNC(35.823) TRUNC(35.823,-2)
--------------- ------------- ----------------
35.82 35 0

SQL> select mod(5,2) from dual;


MOD(5,2)
----------
1
SQL> select mod(sal,2000) from emp where job like 'SALESMAN';
MOD(SAL,2000)
-------------
1600
1250
1250
1500
SQL> select sal from emp where job like 'SALESMAN';
SAL
----------
1600
1250
SQL> select ceil(35.23), ceil(35.5), ceil(35.6) from dual;

CEIL(35.23) CEIL(35.5) CEIL(35.6)


----------- ---------- ----------
36 36 36

SQL> select floor(35.23), floor(35.5), floor(35.6) from dual;


FLOOR(35.23) FLOOR(35.5) FLOOR(35.6)
------------ ----------- -----------
35 35 35

Date Functions:

SYSDATE is a pseudo column that returns the current date and time. When we select sysdate it
will display in a dummy table called DUAL. Oracle date range between 1st jan 4712 BC and 31st
Dec 4712 AD.

Months_between It returns the numeric value. Finds the no. of months between
date1 and date2, result may be positive or negative.
Add_months It returns the date datatype. Adds n number of calendar months to
date, n must be an integer and it can be negative
Last_day It returns the date datatype. Date of the

Next_day It returns the date datatype. Date of the next specified day of the
week following date1, char may be number representing a day, or a
character

SQL> select sysdate from dual;


SYSDATE
---------
08-JUL-18

SQL> select months_between(sysdate, hiredate) from emp;

MONTHS_BETWEEN(SYSDATE,HIREDATE)
--------------------------------
354.728983
352.632208
352.567692
351.212854
345.374144
14 rows selected.

SQL> select months_between('01-jan-2010', sysdate) from dual;


MONTHS_BETWEEN('01-JAN-2010',SYSDATE)
-------------------------------------
-6.2451325

SQL> select last_day(sysdate) from dual;


LAST_DAY(
---------
31-JUL-18
SQL> select last_day(hiredate),last_day('15-feb-88') from emp;
LAST_DAY( LAST_DAY(
--------- ---------
31-DEC-80 29-FEB-88
28-FEB-81 29-FEB-88
28-FEB-81 29-FEB-88
30-APR-81 29-FEB-88

Sunday 1
Monday 2
Tuesday 3
Wednesday 4
Thursday 5
Friday 6
Saturday 7

SQL> select last_day(hiredate),last_day('15-feb-88') from emp;


LAST_DAY( LAST_DAY(
--------- ---------
31-DEC-80 29-FEB-88
28-FEB-81 29-FEB-88
28-FEB-81 29-FEB-88
30-APR-81 29-FEB-88
30-SEP-81 29-FEB-88
SQL> select next_day(sysdate, 'friday') from dual;
NEXT_DAY(
---------
09-JUL-18

SQL> select next_day(hiredate,'friday'), next_day(hiredate,6) from emp where deptno=10;


NEXT_DAY( NEXT_DAY(
--------- ---------
12-JUN-81 12-JUN-81
20-NOV-81 20-NOV-81
29-JAN-82 29-JAN-82

Month mid value 1-15

Day mid value Sunday

Year mid value 30-jun

SQL> select round(sysdate,'day') from dual;


ROUND(SYS
---------
11-JUL-18

SQL> select round(sysdate,'year') from dual;


ROUND(SYS
---------
01-JAN-19

SQL> select round(sysdate,'month') from dual;


ROUND(SYS
---------
01-JUL-18

SQL> select trunc(sysdate,'month'), trunc(sysdate,'year') from dual;


TRUNC(SYS TRUNC(SYS
--------- ---------
01-JUL-10 01-JAN-18

Conversion Functions:
To_char(number | date,[‘fmt’] Converts numbers or date to character format fmt
To_number(char) Converts char, which contains a number to a NUBER
To_date Converts the char value representing date, into a date value
according to fmt specified. If fmt is omitted, format is DD-
MM-YYYY
SQL> select to_char(3000, '$9999.99') from dual;
TO_CHAR(3
---------
$3000.00

SQL> select to_char(sysdate, 'fmday, ddth month yyyy') from dual;


TO_CHAR(SYSDATE,'FMDAY,DDTHMON
------------------------------
thursday, 8th july 2018
SQL> select to_char(sysdate, 'hh:mi:ss') from dual;
TO_CHAR(
--------
03:04:27

SQL> select to_char(sal,'$9999.99') from emp;


TO_CHAR(S
---------
$800.00
$1600.00
$1250.00
$2975.00
$1250.00

SQL> select empno,ename, job,sal from emp where sal>to_number('1500');


EMPNO ENAME JOB SAL
---------- ---------- --------- ----------
7499 ALLEN SALESMAN 1600
7566 JONES MANAGER 2975
7698 BLAKE MANAGER 2850
7782 CLARK MANAGER 2450
7788 SCOTT ANALYST 3000
7839 KING PRESIDENT 5000
7902 FORD ANALYST 3000


EXERCISE-5

i. AIM:Create a simple PL/SQL program which includes declaration section,


executable section and exception –Handling section
SQL> create table marks(sid varchar(10),flat number(6),java number(6),dbms number(6));

Output: Table created

SQL> select * from marks;

SID FLAT JAVA DBMS

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

1 56 76 86

2 45 55 65

23 24 44 34

3 10 20 30

4 50 60 70

Program:

declare

sno marks.sid%type;

m1 marks.flat%type;

m2 marks.java%type;

m3 marks.dbms%type;

avg number(10,4);

begin

sno:='&sno';

select sid,flat,java,dbms into sno,m1,m2,m3 from marks where sid=sno;

avg:=(m1+m2+m3)/3;
if(avg>=70) then

dbms_output.put_line('First Class');

elsif(avg>50 and avg<=69)

dbms_output.put_line('Second Class');

elsif(avg>40 and avg<=50)

dbms_output.put_line('Third Class');

else

dbms_output.put_line('Fail');

end if;

exception

when no_data_found then

dbms_output.put_line('Student Does Not Exist');

end;

Output: 5a.sql

SQL> set serveroutput on;

SQL> start F:/sqlprograms/5a.sql;

Enter value for sno: 1

old 8: sno:='&sno';

new 8: sno:='1';

First Class

PL/SQL procedure successfully completed.

SQL> start F:/sqlprograms/5a.sql;

Enter value for sno: 5


old 8: sno:='&sno';

new 8: sno:='5';

Student Does Not Exist

PL/SQL procedure successfully completed.

ii. AIM: Insert data into student table and use COMMIT, ROLLBACK
and SAVEPOINT in PL/SQL block.
SQL> create table student s(sno number(5), sname varchar(10), address varchar(10),

2 phone_number number(10));
Output: Table Created

insert into students values(5,'lubber','JK',12345);

insert into students values(6,'george','HP',12346);

savepoint a;

insert into students values(7,'Jack','MP',12347);

insert into students values(8,'Harry','GUJ',12348);

rollback to a;

insert into students values(9,'avi','vij',12349);

insert into students values(10,'abhi','AP',12350);

commit;

Output: 5b.sql

SQL> start F:/sqlprograms/5b.sql;

1 row created.

1 row created.

Savepoint created.

1 row created.
1 row created.

Rollback complete.

1 row created.

1 row created.

Commit complete.

Commit complete.

SQL> select * from students;

SNO SNAME ADDRESS PHONE_NO

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

5 lubber JK 12345

6 george HP 12346

9 avi vij 12349

10 abhi AP 12350
Exercise-6
Aim:Develop a program that includes the features NESTED IF, CASE and
CASE expression. The program can be extended using the NULLIF and
COALESCE functions.
Program:

declare

fname varchar2(10);

lname varchar2(10);

ch number;

res varchar2(10);

res1 varchar2(10);

begin

fname:='&fname';

lname:='&lname';

ch:=&ch;

when 1 then

res:=nullif(fname,lname);

dbms_output.put_line('res is:'||res);

if res=fname and lname is null then

dbms_output.put_line('lname is null');

elsif res=lname and fname is null then

dbms_output.put_line('fname is null');

elsif res is not null then

dbms_output.put_line('They are different');

else

dbms_output.put_line('Both are equal');


end if;

when 2 then

res:=COALESCE(fname,lname);

res1:=COALESCE(lname,fname);

dbms_output.put_line('res is:'||res);

if res=lname then

dbms_output.put_line('Fname is null');

elsif res=fname then

dbms_output.put_line('Lname is null');

elsif res is null and res1 is null then

dbms_output.put_line('Both are null');

else

dbms_output.put_line('Both are different');

end if;

else

dbms_output.put_line('Enter right option');

end case;

end;

Output: 6.sql

SQL> set serveroutput on;

1. SQL> start D:/SQL/6.sql;

Enter value for fname: hello

old 8: fname:='&fname';

new 8: fname:='hello';
Enter value for lname: hello

old 9: lname:='&lname';

new 9: lname:='hello';

Enter value for ch: 1

old 10: ch:=&ch;

new 10: ch:=1;

res is:

both are null

PL/SQL procedure successfully completed.

2.SQL> start D:/SQL/6.sql;

Enter value for fname: hello

old 8: fname:='&fname';

new 8: fname:='hello';

Enter value for lname: world

old 9: lname:='&lname';

new 9: lname:='world';

Enter value for ch: 1

old 10: ch:=&ch;

new 10: ch:=1;

res is:hello

They are different

PL/SQL procedure successfully completed.

3.SQL> start D:/SQL/6.sql;

Enter value for fname: hello

old 8: fname:='&fname';
new 8: fname:='hello';

Enter value for lname:

old 9: lname:='&lname';

new 9: lname:='';

Enter value for ch: 2

old 10: ch:=&ch;

new 10: ch:=2;

res is:hello

Lname is null

PL/SQL procedure successfully completed.

4. SQL> start D:/SQL/6.sql;

Enter value for fname:

old 8: fname:='&fname';

new 8: fname:='';

Enter value for lname:

old 9: lname:='&lname';

new 9: lname:='';

Enter value for ch: 2

old 10: ch:=&ch;

new 10: ch:=2;

res is:

Both are nulls

PL/SQL procedure successfully completed.


Exercise-7

Aim: Program development using WHILE LOOPS, numeric FOR LOOPS,


nested loops using ERROR Handling, BUILT –IN Exceptions, USE defined
Exceptions, RAISEAPPLICATION ERROR.
1. Program on FOR loop using PL/SQL

declare

i number(5);

begin

FOR i IN 1..10 LOOP

dbms_output.put_line(i);

END LOOP;

END;

Output: for.sql

SQL> start D:/SQL/for.sql;

10

PL/SQL procedure successfully completed.


2. Program on WHILE loop using PL/SQL

declare

a number:=10;

begin

while a<20 LOOP

dbms_output.put_line('value of a:'||a);

a:=a+1;

END LOOP;

END;

Output:

SQL> start D:/SQL/while.sql;

value of a: 10

value of a: 11

value of a: 12

value of a: 13

value of a: 14

value of a: 15

value of a: 16

value of a: 17

value of a: 18

value of a: 19

PL/SQL procedure successfully completed.


3. Program on raise exception handling using PL/SQL

declare

x number(5);

d number(5);

q number(5);

begin

x:=&divident;

d:=&divisor;

if((d!=0)) then

q:=x/d;

dbms_output.put_line('the quotient is'||q);

else

raise zero_divide;

end if;

exception

when zero_divide then dbms_output.put_line('division with 0 is not possible');

end;

Output: raiseex.sql

1.SQL> start D:/SQL/raiseex.sql;

Enter value for divident: 4

old 6: x:=&divident;

new 6: x:=4;

Enter value for divisor: 2

old 7: d:=&divisor;
new 7: d:=2;

the quotient is 2

2. SQL> start D:/SQL/raiseex.sql;

Enter value for divident: 4

old 6: x:=&divident;

new 6: x:=4;

Enter value for divisor: 0

old 7: d:=&divisor;

new 7: d:=0;

division with 0 is not possible

PL/SQL procedure successfully completed.


Exercise-8

Aim: Programs development using creation of procedures, passing


parameters IN and OUT of PROCEDURES
Program:

Proc_cal.sql

declare

x number(10);

y number(10);

begin

x:=&x;

if(x>0) then

fact1(x,y);

dbms_output.put_line('the factorial is' ||y);

end if;

end;

Proc.sql

create procedure fact1(n in number, f out number) is

i integer;

begin

f:=1;

for i in 1 .. n loop

f:=f*i;

end loop;

end;
/

Output:

SQL> start D:/SQL/proc_cal.sql;

Enter value for x: 5

old 5: x:=&x;

new 5: x:=5;

the factorial is120

PL/SQL procedure successfully completed.


Exercise-9

Functions:

• Functions is also known as named pl/sql block that accepts


some inputs and perform some operaion and return a value
• The functions must always returns a value
• We can call the function from sql statement/pl/sql/or any
front end application
Syntax :
Create or replace function<function_name>(parameter if any)
Retun <type>
Is/as
Variable <datatype>;
begin
statements;
end;
/

Drop function:
We can easily drop the function
Syntax:
Drop function <function_name>;
Program:
AIM:Program development using creation of stored functions,invoke
functions in SQL statements and write complex functions

create or replace function rectangle(l in number,b in number)


return number
as
area number;
begin
area:=l*b;
return area;
end;
/
Output:
Pl/sql program output:
declare

l number;

b number;

area number;

begin

l:= ' &rectangle_length ';

b:= ' &rectangle_breadth ';

area:=rectangle(l,b);

dbms_output.put_line('The area of rectangle is '||area);

end;

/
Exercise-10

Cursors:
Oracle creates a private memory area, known as the context area. Cursor will points to that
contex area(private area or temperory memory area)

A cursor holds the rows (one or more) returned by a SQL statement.

Befor submitting result to the user each query will processor at cursor

Types of cursors:

1.Implict cursor

2. Explict cursors
1.Implict cursor: It can be created by oracle engine and managed by oracle engine

i.e.., memory allocation and deallocation

Each and every sql statements is using cursor area

Properties:

➢ %Isopen
➢ %Found
➢ %NotFound
➢ %RowCount
By default Each and every property begin with SQL
sql%Isopen
sql%Found
sql%NotFound
sql%RowCount

All these properties will return either ture or false

Explict Cursor:
These cursors can be done by the user and managed by the user
Explicit cursor is more efficient when comapared with implicit cursor,because it requires
less space

Explicit cursor requires 4 steps:


1. Declare cursor
2. Open cursor
3. Fetch(data from cursor)
4. Close cursor
Declare cursor:
Cursor name should be declared at the declaration section
Syntax:
Cursor <curs_name> IS
Select <colnames>
From <table_name>
Where <condition>;
Open cursor:
It can be represent at the execution section
Syntax:
Open <cursor_name>;

Fetch:
All the data it will available in the cursor
The record ptr will points only single row every time it will fetch each record/row by
using loops
Syntax:
Fetch <cursor_name> INTO variables;

Close cursor:
When all records are processed then close the cursor
Syntax:
Close< cursor_name>;

Program:

create an employ table and retrieve the name of employ whose salary is Greater than
25000 by PL/SQL

Create table employ(eid,ename,salary);


SQL> set serveroutput on;

Declare
emp_rec varchar(30);

cursor emp_cur is

select ename from employee where salary>25000;

Begin

Open emp_cur;

Loop

fetch emp_cur into emp_rec;

exit when emp_cur%notfound;

dbms_output.put_line(emp_rec);

end loop;

close emp_cur;

end;

Output:

Deepika

Ramesh

sai

PL/SQL Procedure successfully completed

10)Develop programs using features parameters in a CURSOR, FOR UPDATE CURSOR,


WHERE CURRENT of clause and CURSOR variables

Advanced cursors

PARAMETER CURSORS

An explicit cursor may accept a list of parameters. Each time you open the cursor, you can pass
different arguments to the cursor, which results in different result sets.
Syntax:

Cursor<cursor_name>(parameter_list) is select stmt;

To open the cursor with parameter we you use the following syntax:

Syntax:

Open <cursorname>(parameter_valu);
FOR UPDATE CURSOR:

FOR UPDATE statement allows you to lock the records in the cursor. The record locks are
released when the next commit or rollback statement is issued.

Syntax
Cursor <cursor_name> is

Select stmt

For update of <colname> NOWAIT;

Program

declare

cursor c1 is select ename from employee where dno=10 for update of salary NOWAIT;

begin

update employee set salary=salary+50 where dno=10;


end;

WHERE CURRENT of clause

Program:
Exercise-11

11)AIM:Develop programs using before and after triggers,row and


statement

Trigger: A trigger is a stored procedure in database which automatically invokes whenever a


special event in the database occurs. For example, a trigger can be invoked when a row is
inserted into a specified table or when certain table columns are being updated.

Syntax:
Create or replace trigger [trigger_name]
[before | after]
{insert | update | delete}
on [table_name]
[for each row]
[trigger_body]

create or replace trigger t1

after insert or update or delete

on office
for each row

when(new.id>0)

begin

dbms_output.put_line('trigger fried');

end;

Output:

Trigger is created

Create table office (id number(3),name varchar2(14),location varchar2(10));

Table created

Insert :

Insert into office values(111,’amazone’,’hyd’);

Trigger fried

1 row is inserted

Updated:

Update office set location=’viz’ where location=’hyd’;

Trigger fried

1 row updated

Delete:

Delete from office where id=111;

1 row deleted
Exercise-12

12.Create a table and perform the search operation on table using


indexing and non-indexing techniques
CREATE INDEX syntax:

CREATE INDEX index_name ON table_name;

Example:
Create index id1 on emp;

Single-Column Indexes

A single-column index is created based on only one table column. The basic syntax is as follows.

CREATE INDEX index_name


ON table_name (column_name);
Example:
Create index id2 on emp(salary);
Unique Indexes
Unique indexes are used not only for performance, but also for data integrity. A unique index
does not allow any duplicate values to be inserted into the table. The basic syntax is as follows.

Syntax:

CREATE UNIQUE INDEX index_name


on table_name (column_name);

example:
create unique index id3 on emp (empid);
Composite Indexes
A composite index is an index on two or more columns of a table. Its basic syntax is as follows.
CREATE INDEX index_name
on table_name (column1, column2);

DROP INDEX
An index can be dropped using SQL DROP command

DROP INDEX index_name;


Example drop index id1;

You might also like