File Organization in COBOL
File organization describes how the records are organized in a file.
Types of file organization
1. Sequential file organization
A sequential file contains records used to stored and accessed in sequential order.
The key attributes of sequential file organization:- Records are read in sequential order. For example; It is necessary to read all previous four records to read fifth record.
- Records are written in sequential order so that new record is always inserted at the end of the file.
- It is not possible to delete, shorten or lengthen a record after inserting records into a sequential file.
- The order of the record is inserted then it cannot be changed.
- In sequential file organization, updation of record is possible. If the new record length is same as the old record length, then record can be overwritten.
Syntax for sequential organization:
INPUT-OUTPUT SECTION
FILE-CONTROL
SELECT file-name ASSIGN TO dd-name-jcl
ORGANIZATION IS SEQUENTIAL
2. Indexed sequential file organization
An index sequential file consists of records that are accessed sequentially.
Indexed sequential file consists of two parts:- Data File contains the records in sequential scheme.
- Index File contains the primary key and address of the primary key in a data file.
The key attributes of indexed sequential file organization:- In indexed sequential file organization, records are read sequentially similarly in sequential file organization.
- If the primary key is known then records are accessed randomly.
- An alternate index is generated to fetch the records.
Syntax:
INPUT-OUTPUT SECTION
FILE-CONTROL
SELECT file-name ASSIGN TO dd-name-jcl
ORGANIZATION IS SEQUENTIAL
RECORD KEY IS primary-key
ALTERNATE RECORD KEY IS rec-key
3. Relative file organization
A relative file consists of records ordered by their relative address.
The key attributes of the relative file organization:- In a relative file organization, records are read sequentially similarly in the indexed sequential file organization.
- The record is accessed by using a relative key.
- The record is inserted using relative key and relative address is calculated using this key.
- Relative file gives the fastest access to the records.
- It has a disadvantage, if some intermediate records are missing, it occupies the space.
Syntax:
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT file-name ASSIGN TO dd-name-jcl
ORGANIZATION IS RELATIVE
RELATIVE KEY IS rec-key