Module-4 Lecture Notes
Module-4 Lecture Notes
Data Storage: Overview of Physical Storage Media - Magnetic disk Flash storage -
RAID-File and Record Organization-Indexing and Hashing, Ordered Indices - B+Tree
Index File-Static Hashing -Dynamic Hashing Query Processing: Overview-measures
of Query Cost.
• The PL/SQL programming language was developed by Oracle Corporation in the late
1980s as Oracle relational database.
• Stands for Procedural Language extension to SQL.
EXCEPTION This section starts with the keyword EXCEPTION. This optional
<exception handling> section contains exception(s) that handle errors in the program.
END;
DECLARE
message varchar2(20):= 'Hello, World!';
BEGIN
dbms_output.put_line (message);
END;
/
Output−
Hello World
DECLARE
a integer := 10;
b integer := 20;
c integer;
BEGIN
c := a + b;
dbms_output.put_line('Value of c: ' || c);
END;
/
declare
a number := &a;
b number := &b;
c number := &c;
begin
if a>b and a>c
then
dbms_output.put_line('greatest number is ' ||a);
else
dbms_output.put_line('greatest number is ' ||c);
end if;
end;
/
The following program assigns values from the above table to PL/SQL variables using the
SELECT INTO clause of SQL −
DECLARE
c_id [Link]%type := 1;
c_name [Link]%type;
c_addr [Link]%type;
c_sal [Link]%type;
BEGIN
SELECT name, address, salary INTO c_name, c_addr, c_sal FROM customers
WHERE id = c_id;
dbms_output.put_line ('Customer ' ||c_name || ' from ' || c_addr || ' earns ' || c_sal);
END;
/
Output-
Customer Ramesh from Ahmedabad earns 2000
PL/SQL procedure completed successfully
declare
veid [Link]%type:=&veid;
vename [Link]%type;
vdno [Link]%type;
begin
select eid,ename,dno into veid,vename,vdno from emp where eid=veid;
dbms_output.put_line(veid||vename||vdno);
end;
/
------------------------------------------------------------------------------------------------------
declare
my_emprow emp%rowtype;
no number:=&no;
begin
select * into my_emprow from emp where eid=no;
dbms_output.put_line(my_emprow.eid||my_emprow.ename||my_emprow.dno);
end;
/
Prepared and compiled by
Bhupendra Panchal, Asst. Professor, CSE
PL/SQL – Records
• For example, you want to keep track of your books in a library. You might want
to track the following attributes about each book, such as Title, Author, Subject,
Book ID. A record containing a field for each of these items allows treating a
BOOK as a logical unit and allows you to organize and represent its
information in a better way.
• The %ROWTYPE attribute enables a programmer to create table-based and cursor based
records.
DECLARE
emp_rec emp%rowtype;
BEGIN
SELECT * into emp_rec
FROM emp
WHERE eid = 5;
dbms_output.put_line('Emp ID: ' || emp_rec.eid);
dbms_output.put_line('Emp Name: ' || emp_rec.ename);
dbms_output.put_line('Emp Dept: ' || emp_rec.dno);
END;
/
DECLARE
CURSOR emp_cur is
SELECT eid, ename, dno FROM emp;
emp_rec emp_cur%rowtype;
BEGIN
OPEN emp_cur;
LOOP
FETCH emp_cur into emp_rec;
EXIT WHEN emp_cur%notfound;
DBMS_OUTPUT.put_line(emp_rec.eid ||' '||emp_rec.ename ||' '||emp_rec.dno);
END LOOP;
END;
/
DECLARE
type student is record
(rno varchar(50), name varchar(50), branch varchar(10));
s1 student;
s2 student;
BEGIN
-- Student 1 specification
[Link] := '1';
[Link] := 'Ajay';
[Link] := 'CSE';
-- Student 1 specification
[Link] := '5';
[Link] := 'Aman';
[Link] := 'IT';
-- Print Student 1 record
dbms_output.put_line('Student 1 Rollno : '|| [Link]);
dbms_output.put_line('Student 1 Name : '|| [Link]);
dbms_output.put_line('Student 1 Branch : '|| [Link]);
-- Print Student 2 record
dbms_output.put_line('Student 2 Rollno : '|| [Link]);
dbms_output.put_line('Student 2 Name : '|| [Link]);
dbms_output.put_line('Student 2 Branch : '|| [Link]);
END;
/
Prepared and compiled by
Bhupendra Panchal, Asst. Professor, CSE
PL/SQL – Cursor
• Oracle creates a memory area, known as the context area, for processing an
SQL statement, which contains all the information needed for processing the
statement; for example, the number of rows processed, etc.
• A cursor is a pointer to this context area. PL/SQL controls the context area
through a cursor. A cursor holds the rows (one or more) returned by a SQL
statement.
• For INSERT operations, the cursor holds the data that needs to be inserted. For UPDATE
and DELETE operations, the cursor identifies the rows that would be affected.
BEGIN
UPDATE customer SET salary = salary + 500 where cid=6;
IF sql%notfound THEN
dbms_output.put_line('no customers selected’);
END IF;
END;
/
• Explicit cursors are user-defined cursors for gaining more control over the context area.
• An explicit cursor should be defined in the declaration section of the PL/SQL Block.
• It is created on a SELECT Statement which returns more than one row.
DECLARE
v_cid [Link]%type;
v_cname [Link]%type;
v_age [Link]%type;
v_salary [Link]%type;
CURSOR cur_customer is
SELECT cid, cname, age, salary FROM customer;
BEGIN
OPEN cur_customer;
LOOP
FETCH cur_customer into v_cid, v_cname, v_age, v_salary;
EXIT WHEN cur_customer%notfound;
dbms_output.put_line(v_cid||' '||v_cname||v_age ||' '||v_salary);
END LOOP;
CLOSE cur_customer;
END;
/
Prepared and compiled by
Bhupendra Panchal, Asst. Professor, CSE
PL/SQL – Cursor Cont..
declare
cursor c1 is
select name, hdate from emp1;
ename [Link]%type;
hiredate [Link]%type;
begin
open c1;
loop
fetch c1 into ename, hiredate;
exit when c1%notfound;
dbms_output.put_line(ename||hiredate);
end loop;
close c1;
end;
/
Program 14: Display details of first 5 highly paid employees using cursors.
declare
cursor emp_cur is
select * from emp1 order by salary desc;
emp_row emp_cur %rowtype;
begin
open emp_cur;
loop
fetch emp_cur into emp_row;
exit when emp_cur %rowcount>5;
dbms_output.put_line (emp_row.ssn||' '||emp_row.name||emp_row.hdate||'
'||emp_row.salary);
end loop;
dbms_output.put_line ('Total rows selected:'||emp_cur%rowcount);
close emp_cur;
end;
/
• A stored procedure or procedure is a PL/SQL block that performs one or more specific
tasks.
• PL/SQL subprograms are named PL/SQL blocks that can be invoked with a set of
parameters. PL/SQL provides two kinds of subprograms −
• Functions − These subprograms return a single value; mainly used to compute and
return a value.
• Procedures − These subprograms do not return a value directly; mainly used to
perform an action.
The following example creates a simple procedure that displays the string 'Hello World!' on
the screen when executed.
The above procedure named 'greetings' can be called with the EXECUTE keyword as −
EXECUTE greetings;
Hello World
PL/SQL procedure successfully completed.
The following table lists out the parameter modes in PL/SQL subprograms −
IN
An IN parameter lets you pass a value to the subprogram. It is a read-only parameter. Inside
the subprogram, an IN parameter acts like a constant.
OUT
An OUT parameter returns a value to the calling program. Inside the subprogram, an OUT
parameter acts like a variable. You can change its value and reference the value after
assigning it.
IN OUT
An IN OUT parameter passes an initial value to a subprogram and returns an updated value to
the caller. It can be assigned a value and the value can be read.
This program finds the minimum of two values. Here, the procedure takes two numbers
using the IN mode and returns their minimum using the OUT parameters.
declare
a number;
b number;
c number;
procedure findmin(x in number, y in number, z out number) is
begin
if x < y then
z:= x;
else
z:= y;
end if;
end;
begin
a:= &a;
b:= &b;
findmin(a, b, c);
dbms_output.put_line(' minimum number is : ' || c);
end;
/ Prepared and compiled by
Bhupendra Panchal, Asst. Professor, CSE
PL/SQL –Procedure Example 2- (in out parameter)
This procedure computes the square of value of a passed value. This example shows how
we can use the same parameter to accept a value and then return another result.
declare
a number;
procedure squarenum(x in out number) is
begin
x := x * x;
end;
begin
a:= &a;
squarenum(a);
dbms_output.put_line(' square of given number is: ' || a);
end;
/
Creating a Function
create or replace function total_emp
return number is
total number(2) := 0;
begin
select count(*) into total from emp1;
return total;
end;
/
Calling a Function
DECLARE
c number(2);
BEGIN
c := total_emp();
dbms_output.put_line('Total no. of Customers: ' || c);
END;
/
declare
a number;
b number;
c number;
function findmax(x in number, y in number)
return number is
z number;
begin
if x > y then
z:= x;
else
z:= y;
end if;
return z;
end;
begin
a:= &a;
b:= &b;
c := findmax(a, b);
dbms_output.put_line(' maximum number is: ' || c);
end;
/ Prepared and compiled by
Bhupendra Panchal, Asst. Professor, CSE
PL/SQL –Functions Example 3-
The following program calculates the factorial of a given number by calling itself recursively
declare
num number;
factorial number;
function fact(x number)
return number is
f number;
begin
if x=0 then
f := 1;
else
f := x * fact(x-1);
end if;
return f;
end;
begin
num:= #
factorial := fact(num);
dbms_output.put_line(' factorial '|| num || ' is ' || factorial);
end;
/ Prepared and compiled by
Bhupendra Panchal, Asst. Professor, CSE
PL/SQL –Functions Program-12
Write a PL/SQL block to delete a record. If delete is successful return 1 else return 0.
• Triggers are stored programs, which are invoked by Oracle engine automatically
whenever a specified event occurs.
• Trigger is stored into database and invoked repeatedly, when specific condition match.
• Triggers are written to be executed in response to any of the following events −
• A database manipulation (DML) statement (DELETE, INSERT, or UPDATE)
• A database definition (DDL) statement (CREATE, ALTER, or DROP).
• A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP or
SHUTDOWN).
Benefits of Triggers-
• Generating some derived column values automatically
• Enforcing referential integrity
• Event logging and storing information on table access
• Auditing
• Imposing security authorizations
• Preventing invalid transactions
Prepared and compiled by
Bhupendra Panchal, Asst. Professor, CSE
PL/SQL –Triggers
DECLARE
Declaration-statements
BEGIN
Executable-statements
EXCEPTION
Exception-handling-statements
END;
declare
a varchar2 (10);
begin
select to_char (sysdate,'hh:mm:ss’) into a from dual;
if a > ‘[Link]'
then
raise_application_error (-20500,'you can not do this operation now');
end if;
end;
WRITE A DATA BASE TRIGGER, WHICH ACTS JUST LIKE PRIMARY KEY AND
DOES NOT ALLOW DUPLICATE
declare
a number;
begin
if :[Link] is null then
raise_application_error(-20001, 'id can not be null');
end if;
select count(*) into a from test where id=:[Link];
if a >0 then
raise_application_error(-20001,'unique key rule is voilated');
end if;
end;
/
DECLARE
a int:=10;
b int:=0;
answer int;
BEGIN
answer:=a/b;
dbms_output.put_line('the result after division is'||answer);
exception
WHEN zero_divide THEN
dbms_output.put_line('dividing by zero please check the values again');
dbms_output.put_line('the value of a is '||a);
dbms_output.put_line('the value of b is '||b);
END;
DECLARE
temp varchar(20);
BEGIN
SELECT g_id into temp from geeks where g_name=‘Aman';
exception
WHEN no_data_found THEN
dbms_output.put_line('ERROR');
dbms_output.put_line('there is no name as Aman');
end;
The first column is the Search key that contains a copy of the primary key or candidate key
of the table. These values are stored in sorted order so that the corresponding data can be
accessed quickly.
The second column is the Data Reference or Pointer which contains the address of the disk
block where that particular key value can be found.
If the index is created on the basis of the primary key of the table, then it is known as
primary indexing. These primary keys are unique to each record and contain 1:1 relation
between the records.
As primary keys are stored in sorted order, the performance of the searching operation is
quite efficient.
The primary index can be classified into two types: Dense index and Sparse index.
Dense Index:
For every search key value in the data file, there is an index record.
This record contains the search key and also a reference to the first data record with that
search key value.
Sparse Index:
The index record appears only for a few items in the data file. Each item points to a block
as shown.
To locate a record, we find the index record with the largest search key value less than or
equal to the search key value we are looking for.
We start at that record pointed by the index record and proceed along with the pointers in
the file (that is, sequentially) until we find the desired record.
B1 1 Ram 21 Indore Key (Roll No) Pointer
2 Aman 23 Bhopal
3 Amit 22 Ujjain 1 B1
4 Anuj 21 Indore
5 B2
B2 5 ……………………
6 …………………… 9 B3
7 ……………………
8 …………………… 13 B4
B3 9 ……………………
10 ……………………
11 ……………………
12 …………………… Index Table
B4 13 ……………………
14 ……………………
15 ……………………
16 ……………………
:
Prepared and compiled by
Hard disk Bhupendra Panchal, Asst. Professor, CSE
Clustering Index
A clustered index can be defined as an ordered data file. Sometimes the index is created on
non-primary key columns which may not be unique for each record.
In this case, to identify the record faster, we will group two or more columns to get the unique
value and create index out of them. This method is called as clustering index.
The records which have similar characteristics are grouped, and indexes are created for these
group.
A secondary index just tells us where the data lies, i.e. it gives us a list of virtual pointers or
references to the location where the data is actually stored.
In secondary indexing, to reduce the size of mapping, another level of indexing is introduced.
In this method, the huge range for the columns is selected initially so that the mapping size of
the first level becomes small. Then each range is further divided into smaller ranges.
The mapping of the first level is stored in the primary memory, so that address fetch is faster.
The mapping of the second level and actual data are stored in the secondary memory (hard
disk).
It requires more time as compared to the clustered index because some amount of extra work
is done in order to extract the data by further following the pointer. In the case of a clustered
index, data is directly present in front of the index.
In the B tree, all the leaf nodes must be at the same level, whereas, in the
case of a binary tree, the leaf nodes can be at different levels.
In the case of minimum children, the leaf nodes have zero children, the root
node has two children, and the internal nodes have a ceiling of m/2.
Each node can have maximum (m-1) keys. For example, if the value of m is
5 then the maximum value of keys is 4.
If we perform insertion in the B tree, then the node is always inserted in the
leaf node.
As we can observe in the above B+ tree node structure that it contains n-1
key values (K1 to Kn-1) and n pointers (P1 to Pn).
The search key values which are placed in the node are kept in sorted
order. Thus, if i<j then ki<kj.
In the above figure, the node contains three key values, i.e., 9, 16, and 25.
The pointer that appears before 9, contains the key values less than 9 represented by
ki.
The pointer that appears before 16, contains the key values greater than or equal to 9
but less than 16 represented by kj.
The pointer that appears before 25, contains the key values greater than or equal to
16 but less than 25 represented by kn.
First, we will fetch for the intermediary node which will direct to the leaf node that can
contain a record for 55.
So, in the intermediary node, we will find a branch between 50 and 75 nodes. Then at the end,
we will be redirected to the third leaf node.
Here DBMS will perform a sequential search to find 55.
It will go to the 3rd leaf node after 55. It is a balanced tree and a leaf node of this tree is
already full, so we cannot insert 60 there.
In this case, we have to split the leaf node, so that it can be inserted into tree without affecting
the fill factor, balance and order.
The 3rd leaf node has the values (50, 55, 60, 65, 70) and its current root node is 50. We will
split the leaf node of the tree in the middle so that its balance is not altered. So we can group
(50, 55) and (60, 65, 70) into 2 leaf nodes.
If these two has to be leaf nodes, the intermediate node cannot branch from 50. It should have
60 added to it, and then we can have pointers to a new leaf node.
In this case, we have to remove 60 from the intermediate node as well as from the 4th leaf
node too. If we remove it from the intermediate node, then the tree will not satisfy the rule of
the B+ tree. So we need to modify it to have a balanced tree.
After deleting node 60 from above B+ tree and re-arranging the nodes, it will show as
follows:
Key h(k)
Hash Function – Hash function is a mapping function that maps all the set
of search keys to actual record address.
Generally, hash function uses primary key to generate the hash index –
address of the data block. Hash function can be simple mathematical
function to any complex mathematical function.
Hash Index- The prefix of an entire hash value is taken as a hash index.
Delete a Record- To delete a record, we will first fetch the record which is supposed to
be deleted. Then we will delete the records for that address in memory.
Update a Record- To update a record, we will first search it using a hash function, and
then the data record is updated.
If we want to insert some new record into the file but the address of a data bucket
generated by the hash function is not empty, or data already exists in that address.
This situation in the static hashing is known as bucket overflow. This is a critical
situation in this method.
To overcome this situation, there are various methods. Some commonly used methods
are as follows:
1. Open Hashing or Linear Probing
2. Close Hashing or Overflow chaining
• Is the process of selecting the most efficient query evaluation plan from among
the many strategies usually possible for processing the given query, specially
when the query is complex.
• Objective is to make a such system that can minimize the cost of query
evaluation by constructing a query evaluation plan.
• One aspect is relational algebra, where system can find the equivalent
expression for given relation algebra.
• The goal of query optimization is to reduce the system resources required to
fulfill a query and ultimately provide the user with the correct result set faster.
• Query Processing- Includes translation of high level queries into low level expressions
that can be used at physical level of the file system.
• Each relational algebra operation can be evaluated using one of several different
algorithms
– Correspondingly, a relational-algebra expression can be evaluated in many ways.
• Query Optimization- It is the process in which multiple query evaluation plans are
examined and a most efficient plan is identified for execution.
• e.g. number of tuples in each relation, size of tuples, etc.
• For example, a user wants to fetch the records of the employees whose salary is greater
than or equal to 10000. For doing this, the following query is undertaken:
SQL> select B,D from R,S where R.A=‘c’ and S.E=2 and R.C=S.C;
πB,D πB,D
| |
σR.A=‘c’ ^ S.E=2 ^ R.C=S.C |x|
| / \
x σR.A=‘c’ σ S.E=2
| |
/ \ S
R
R S
84