Weitere ähnliche Inhalte
Kürzlich hochgeladen (20)
Ds 7
- 1. Introduction to Data Access Methods
Objectives
In this lesson, you will learn to:
Describe the different methods by which data records
can be accessed from disk files, and state the
advantages and disadvantages of each method
List the pros and cons of various data structures to
store an index
Implement insertion and deletion of records in a data
file
Implement insertion and deletion of fields in a
database structure file
Introduction to Data Access Methods/Lesson 7/Slide 1 of 46
©NIIT
- 2. Introduction to Data Access Methods
Access Methods
Data on disk files is usually organised as records
containing several fields, one of which is often used
as a key field.
There are several methods employed to access such
data records from disk files.
Each of the access methods requires a particular type
of organisation of the disk file as well.
Introduction to Data Access Methods/Lesson 7/Slide 2 of 46
©NIIT
- 3. Introduction to Data Access Methods
Sequential Access
Sequential access is the simplest but the least
efficient access method employed.
Sequential access requires reading of all data from a
file until the desired record is arrived at.
As per this method, if a couple of million records were
involved in the processing, an average of a million
records would be read every time a single record was
required.
Introduction to Data Access Methods/Lesson 7/Slide 3 of 46
©NIIT
- 4. Introduction to Data Access Methods
Random Access
According to this access method, in a data file
containing fixed length records, it is possible to access
any particular record whose relative position in the file
is known.
In random access, the record number is used to
calculate the offset address. Access on key field value
is not possible.
Introduction to Data Access Methods/Lesson 7/Slide 4 of 46
©NIIT
- 5. Introduction to Data Access Methods
Indexed Access
In a real life application, the user would seldom know
the record number, therefore a table containing a list
of all key fields of each record in the data file should
be created along with the offset positions of the
beginning of the records in the file.
Key Field Offset
A123 0
A124 200
A135 400
A146 600
R259 800
R624 1000
Introduction to Data Access Methods/Lesson 7/Slide 5 of 46
©NIIT
- 6. Introduction to Data Access Methods
Indexed Access (Contd..)
The indexed table should be sorted in ascending
order of the key field values.
This method is slower, as the number of records
increase, the overhead of the time taken to search the
index also increases.
Indexed access is the most commonly used access
method on database applications.
The loss in access speed can be minimized by the
use of carefully selected algorithms and data
structures.
Introduction to Data Access Methods/Lesson 7/Slide 6 of 46
©NIIT
- 7. Introduction to Data Access Methods
Representing an Index
A key factor of this method is the quick and efficient
way to access the elements of the index itself.
The ways to store the index are as follows:
Using Arrays
Using Linked List
Using Binary Trees
Introduction to Data Access Methods/Lesson 7/Slide 7 of 46
©NIIT
- 8. Introduction to Data Access Methods
Using Arrays to Store an Index
This is the simplest way to store the index.
Each element of an array could contain two parts:
The key value of a record in a data file
The offset position of this record from the
beginning of the file.
The problems that arise are the following:
Finding the required element normally involves a
sequential search through the array
For use as an index, the array would first have to
be sorted on the key values.
Introduction to Data Access Methods/Lesson 7/Slide 8 of 46
©NIIT
- 9. Introduction to Data Access Methods
Using Arrays to Store an Index (Contd..)
Corresponding insertions or deletions are not easy
to handle in an array.
Introduction to Data Access Methods/Lesson 7/Slide 9 of 46
©NIIT
- 10. Introduction to Data Access Methods
Using Linked List to Store an Index
The Linked List helps to store the index in the sorted
order.
The insertion and deletion in a Linked list involves
only the routine processing.
In order to get the required node, the Linked List
needs to be searched sequentially.
A figure representing the linked list is as follows:
Introduction to Data Access Methods/Lesson 7/Slide 10 of
©NIIT
46
- 11. Introduction to Data Access Methods
Using Binary Trees to Store an Index
Use of Binary Tree to store an index is much more
efficient than the use of Linked List.
It contains the data in sorted order.
All left child-nodes contain data less than, and right
child-nodes contain data greater than that of their
parent-nodes.
Consider the following figure:
Introduction to Data Access Methods/Lesson 7/Slide 11 of
©NIIT
46
- 12. Introduction to Data Access Methods
Using Binary Trees to Store an Index (Contd..)
In the previous figure, there are several nodes at
different levels of the tree which have NULL left or
right pointers.
Introduction to Data Access Methods/Lesson 7/Slide 12 of
©NIIT
46
- 13. Introduction to Data Access Methods
Problem Statement 7.P.1
Referring to the following figures, specify the number of
comparisons made in the search of the linked list and
binary tree for the following key values.
Introduction to Data Access Methods/Lesson 7/Slide 13 of
©NIIT
46
- 14. Introduction to Data Access Methods
Problem Statement 7.P.1(Contd..)
key values are :
7
1
6
4
Introduction to Data Access Methods/Lesson 7/Slide 14 of
©NIIT
46
- 15. Introduction to Data Access Methods
An Exercise in Data Structures
The assumptions made regarding the database files
are:
1. The records are stored in a data file in the
sequence in which they are encountered.
2. This data file has been created by a simple data
entry program, which accepts the field values,
validates them and stores them in the data file.
3. The structure of the data file is stored in another
file. This file stores information about each field of
the data file- the field name, type of the field and
width of the field.
Introduction to Data Access Methods/Lesson 7/Slide 15 of
©NIIT
46
- 16. Introduction to Data Access Methods
Creating an Index for a Data File
An index can be created containing key value and
offset pairs for each record in a database.
The keys in the index must be inserted in sorted
order.
The index is not supposed to contain all of the data in
the data record.
The data items required in the index are as follows:
key field value
position at which each record begins in the data
file
Introduction to Data Access Methods/Lesson 7/Slide 16 of
©NIIT
46
- 17. Introduction to Data Access Methods
Creating an Index for a Data File (Contd..)
A class to store this information can be declared as
follows:
class record_data
{
char key [5];
long offset;
};
A pointer to an element of this type of structure can be
declared as follows:
record_data *rec_ptr;
Introduction to Data Access Methods/Lesson 7/Slide 17 of
©NIIT
46
- 18. Introduction to Data Access Methods
Creating an Index for a Data File (Contd..)
The key value can be read directly from the file and
offset position can be find out through tellg() function.
Now write the data in the index file using following:
fileobj.write((char*)rec_ptr,sizeof(rec_ptr));
Later on data need to be stored in a linked list.
Introduction to Data Access Methods/Lesson 7/Slide 18 of
©NIIT
46
- 19. Introduction to Data Access Methods
Creating an Index for a Data File (Contd..)
The declaration of a node of this linked list can be
made as shown below:
class list
{
record_data *rec;
list *next;
}
To access the node from the linked list, object of
linked list need to be declared as:
list *data_ptr;
Introduction to Data Access Methods/Lesson 7/Slide 19 of
©NIIT
46
- 20. Introduction to Data Access Methods
Just a Minute:
2. Identify the fastest and slowest access methods out of
those, given below:
a. Indexed
b. Random
c. Sequential
3. Which access method is likely to be used in a
database access program dealing with a large
number of records?
Introduction to Data Access Methods/Lesson 7/Slide 20 of
©NIIT
46
- 21. Introduction to Data Access Methods
Just a Minute (Contd..):
2. Of the following, which are used in indexed access
and which are used in random access?
a. Offset addresses.
b. Key field values.
Introduction to Data Access Methods/Lesson 7/Slide 21 of
©NIIT
46
- 22. Introduction to Data Access Methods
Problem Statement 7.P.2
Complete the makenode() function given below:
list *makenode ()
{
list *newnode;
:
:
return newnode; /* Return a pointer to the newly
allocated linked list node *l
}
Introduction to Data Access Methods/Lesson 7/Slide 22 of
©NIIT
46
- 23. Introduction to Data Access Methods
Creation of a Linked List Index
The first step to be taken is to make all the required
class declarations.
The steps involved in the creation of the index are:
Sequential reading of records from the database
file.
Allocation of memory for a node of the linked list
index .
Assignment of key and offset values of the
database record to the appropriate elements in the
structure of the linked list node.
Introduction to Data Access Methods/Lesson 7/Slide 23 of
©NIIT
46
- 24. Introduction to Data Access Methods
Creation of a Linked List Index (Contd..)
Insertion of the node into the appropriate position
in the linked list so as to keep it sorted in order of
the key values
These steps can be repeated until there are no more
data records in the database file.
Introduction to Data Access Methods/Lesson 7/Slide 24 of
©NIIT
46
- 25. Introduction to Data Access Methods
Problem Statement 7.P.3
Complete the write_index() function given below to
write the linked list index to an index file oil disk.
/* Function to write the linked list index sequentially to
an index file on disk */
void write index ()
{
fstream *fw;
list *cur
char off [11];
cur = start; /* Designate the current node as start*/
Introduction to Data Access Methods/Lesson 7/Slide 25 of
©NIIT
46
- 26. Introduction to Data Access Methods
Problem Statement 7.P.3 (Contd..)
fw.fopen (index, ios::app);
if (start) /* If the linked list index is not empty */
{
while (cur) /* Keep doing the following steps until the
pointer to the next node is WILL */
{
:
}
}
Introduction to Data Access Methods/Lesson 7/Slide 26 of
©NIIT
46
- 27. Introduction to Data Access Methods
Problem Statement 7.P.3(Contd..)
else
{
cout“nnThe list is emptyn”;
fclose(fw);
}
Introduction to Data Access Methods/Lesson 7/Slide 27 of
©NIIT
46
- 28. Introduction to Data Access Methods
Allocating Memory for a Linked List Node
Whenever a new entry is required in the index,
rnake_index() function is used to allocate memory for
a linked list node.
The make_index() function returns a pointer to a
linked list node.
A single node of the Linked List can be represented
as shown in the following figure:
Introduction to Data Access Methods/Lesson 7/Slide 28 of
©NIIT
46
- 29. Introduction to Data Access Methods
Allocating Memory for a Linked List Node
(Contd..)
Every linked list node has a data node associated with
it, and memory for that has to be allocated as well.
Introduction to Data Access Methods/Lesson 7/Slide 29 of
©NIIT
46
- 30. Introduction to Data Access Methods
Assigning Data to the New Node
When the getdata() function is used in make_index()
function, the following assumptions are made:
Two arguments are passed to getdata()
®A pointer to the new node
®A pointer to the database file stream
The key occupies 4 bytes and the rest of the
record occupies 18 bytes.
Introduction to Data Access Methods/Lesson 7/Slide 30 of
©NIIT
46
- 31. Introduction to Data Access Methods
Writing Linked List Index to Disk
A copy of the index data is written to an index file on
the disk.
Writing the linked list index to an index file involves
sequentially accessing successive nodes of the linked
list and dumping the key and offset to a file.
Introduction to Data Access Methods/Lesson 7/Slide 31 of
©NIIT
46
- 32. Introduction to Data Access Methods
Inserting and Deleting Records from a Database
Handling Insertions
The data file has to be modified each time an insertion
takes place.
Data does not physically have to be inserted in the
data file.
Data is just appended to the file and the appropriate
insertion is made in the linked list along with the new
key and offset values.
After the data for a new record is input from the
keyboard, the data values have to be used in two
ways:
The new record has to be written at the end of the
database file. Introduction to Data Access Methods/Lesson 7/Slide 32 of
©NIIT
46
- 33. Introduction to Data Access Methods
Inserting and Deleting Records from a
Database (Contd..)
The key value and the offset position of the
beginning of the new record have to be stored in
the linked list node.
Introduction to Data Access Methods/Lesson 7/Slide 33 of
©NIIT
46
- 34. Introduction to Data Access Methods
Problem Statement 7.P.4
Specify the order in which the following steps should
be carried out to create a linked list containing field
details from the database structure file:
a. Attach node at the end of the linked list.
b. Read details of a field from the database structure
file.
c. Declare the structures for the linked list node.
d. Allocate memory for a node.
e. Copy field contents into the node.
Introduction to Data Access Methods/Lesson 7/Slide 34 of
©NIIT
46
- 35. Introduction to Data Access Methods
Problem Statement 7.P.4 (Contd..)
After the data for a new record is input from the
keyboard, the data values have to be used in two
ways:
The new record has to be written at the end of the
database file.
The key value and the offset position of the
beginning of the new record have to be stored in
the linked list node.
Introduction to Data Access Methods/Lesson 7/Slide 35 of
©NIIT
46
- 36. Introduction to Data Access Methods
Problem Statement 7.P.5
State whether True or False
Once the database structure is changed, it is not
possible to meaningfully maintain the data contained
in the database.
Introduction to Data Access Methods/Lesson 7/Slide 36 of
©NIIT
46
- 37. Introduction to Data Access Methods
Handling Deletions
In order to delete the records from the database,
delete the record reference from the linked list and,
thus, from the index.
Introduction to Data Access Methods/Lesson 7/Slide 37 of
©NIIT
46
- 38. Introduction to Data Access Methods
Problem Statement 7.D.1
Write a program to perform insertion and deletion of
records from a database file
The assumptions need to be made regarding the
database file are:
2. The records are stored in a data file in the sequence
in which they are entered.
3. This data file has been created by a simple data
entry program, which accepts the field values,
validates them and stores them in the data file.
4. Now make the index file using this database file.
Introduction to Data Access Methods/Lesson 7/Slide 38 of
©NIIT
46
- 39. Introduction to Data Access Methods
Binary Tree Indexes
Binary Trees are special form of Linked Lists.
The code to declare the binary tree node is as follows:
class data
{
char key [5];
long offset;
};
Introduction to Data Access Methods/Lesson 7/Slide 39 of
©NIIT
46
- 40. Introduction to Data Access Methods
Binary Tree Indexes (Contd..)
class tree_node
{
data *dat;
tree_node *left;
tree_node *right;
};
Introduction to Data Access Methods/Lesson 7/Slide 40 of
©NIIT
46
- 41. Introduction to Data Access Methods
Handling Insertions
The way through which the key value and offset were
read from a data file and inserted into a position in a
linked list, can be followed to insert a node, whose
structure declarations are given, into the binary tree in
the correct (sorted) position.
Introduction to Data Access Methods/Lesson 7/Slide 41 of
©NIIT
46
- 42. Introduction to Data Access Methods
Reading a Binary Tree Index
inorder() traversal algorithm can be used to read the
data sequentially from the index.
Data can be searched for in the index using a
minimum of comparisons which are characteristic of
binary tree usage.
Introduction to Data Access Methods/Lesson 7/Slide 42 of
©NIIT
46
- 43. Introduction to Data Access Methods
Problem Statement 7.P.5
As part of a team developing a database application,
you have been asked to do the following:
a. Accept a filename as command line parameter
b. Create the structure of the data file as per user
requirement.
c. Accept data as per the defined structure.
Introduction to Data Access Methods/Lesson 7/Slide 43 of
©NIIT
46
- 44. Introduction to Data Access Methods
Summary
In this lesson, you learned that:
There are several methods by which data on disk can
be accessed, three of which are:
Sequential access
Random access
Indexed access
Linked lists and binary trees are ideal data structures
to store an index. The advantages of using these
structures are: efficient insertions, deletions and
searches
Introduction to Data Access Methods/Lesson 7/Slide 44 of
©NIIT
46
- 45. Introduction to Data Access Methods
Summary (Contd..)
To create an index file, given a data file:
Create a node to store the key offset pair of a
record.
Store the offset of the first byte of the record in the
offset part of the INFO of the node. The offset is
found using the tellg() function.
Read a record and store the key value of the
record in the key part of the INFO of the node
Insert the node in the appropriate position of the
linked list so that it remains sorted
The above steps have to repeated till there are no
more records in the file
Introduction to Data Access Methods/Lesson 7/Slide 45 of
©NIIT
46
- 46. Introduction to Data Access Methods
Summary (Contd..)
To insert a record in the data file:
Accept the data for the new record, and append it
at the end of the data file
Create a node containing the key and offset of
this record and insert it in the linked list.
To delete a record from the data file:
Change the first byte of the record to ‘D’ to
indicate that the record has been marked for
deletion.
Delete the corresponding node from the linked
list. Physical deletion of the record in the data file
is not necessary.
Introduction to Data Access Methods/Lesson 7/Slide 46 of
©NIIT
46
Hinweis der Redaktion
- Lower Bound and Upper Bound to denote the first element number and the last element number respectively
- Lower Bound and Upper Bound to denote the first element number and the last element number respectively
- Lower Bound and Upper Bound to denote the first element number and the last element number respectively
- Lower Bound and Upper Bound to denote the first element number and the last element number respectively