It is easy to add records to an unordered Sequential file because we can simply add them to
the end of the file by opening the file for
EXTEND (e.g.
OPEN EXTEND UnorderedFile). But it is not really
possible to delete records from an unordered Sequential file. And it is not feasible to
update records in an unordered Sequential file.
To add records to an unordered Sequential File the file must be opened for
EXTEND. Opening a file for
EXTEND, positions the Next Record Pointer (NRP) at the
end of the file, so that when records are written to the file, they are appended to the end.
The animation below demonstrates how records are added to an unordered file. As in all the
examples in this tutorial, the records to be added are contained in a transaction file. The
transaction file records are applied to the unordered file and the result is an
unordered file that has all the transaction file records appended to the end of it.
While it is easy to add records to an unordered Sequential file it is not really feasible to
delete or update records in an unordered Sequential file.
Records in a Sequential file can not be deleted or updated "in situ". The only way to
delete records from a Sequential file, is to create a new file which does
not contain them; and the only way to update records in a Sequential file,
is to create a new file which contains the updated records. Because both these operations
rely on record matching, they do not work for unordered Sequential files.
Updates to Sequential files are normally done in batch mode. That is, all the updates are
gathered together into a transaction file, and then applied to the target file in one go.
The target file is often called the master file.
There are few problems if we are just adding records to the end of a file, but if we want to
update or delete records, then there must be some way of identifying the record we want to
update or delete. A "key field" is normally used to achieve this.
A key field, is a field in the record whose value is unique to that record. For instance, in
a student record, the StudentId is normally used as the key field. Without a key field to
identify the record we require, we cannot delete or update records.
When we apply delete or update transaction records to a master file, we compare the key
field in the transaction record with that in the master file record. If there is a
match, we know we have to update or delete the master file record.
If both files are ordered on the key field, then this
record matching operation functions correctly, but if either the transaction or the
master file is unordered, record matching cannot work.
Suppose we have an unordered transaction file and an unordered master file. The transaction
file contains records which indicate which records in the master file we want to delete. To
delete the records we need to create a new master file which does not contain the deleted
records.
The animation below demonstrates what happens when we attempt to batch-delete records from
an unordered Sequential file.