File Organization


File organization is the method used to store and arrange data records within a file. It specifies how information is physically stored in the memory and impacts how efficiently data can be accessed and managed.

COBOL primarily supports three types of file organizations -

  • Sequential File Organization
  • Indexed File Organization
  • Relative File Organization

The selection of file organization is important because it affects how efficiently a program can access and manage the data. We will discuss about all file organizations below that helps to select the best file organization based on requirement.

Sequential File Organization -


Sequential file organization applies to the sequential files (PS - Physical sequential file) that use the QSAM access method. A sequential file consists of records that are stored and accessed sequentially. The main attributes of the sequential file organization are -

  1. Records are stored in the same order as they are written.
  2. Records can be read in sequential order (entry sequential) from file beginning to end. For example, if we need to read the 5th record, we need to read five times to get the desired record.
  3. Record physical deletion or length change is not possible once it is written to the file.
  4. Updating a record (REWRITE) is possible. A record can be updated with new data or replaced with a new record if both have the same length.
  5. A new record is always written at the end of the file.
  6. Records sorting is not possible.
  7. The file may contain duplicate records because the key is not applicable.
  8. Accessing is slow because reading is allowed only in sequential order.

Syntax -

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT logical-file-name ASSIGN TO DSName 
    ORGANIZATION IS SEQUENTIAL

Example - Declaring a sequential file in program.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT empfile ASSIGN TO input01
    ORGANIZATION IS SEQUENTIAL

Indexed File Organization -


Indexed file organization applies to the KSDS file that uses the Virtual storage access method (VSAM). A KSDS file consists of records that are stored and accessed using the key value. The key is unique and combination of field or fields (defined by the developer and part of the record). VSAM file has two components -

  • DATA - Contains the actual data.
  • INDEX - Contains the key information along with its pointers to record stored in memory.

The main attributes of the indexed file organization are -

  1. Records are written in key sequential order.
  2. Records can be read sequentially (one by one without key value), randomly (using key-value), or dynamically (a combination of the above two).
  3. Physical deletion of the record is allowed, and the memory released will be reused.
  4. Updating a record (REWRITE) is possible.
  5. New records will be written where the key value fits in sorting order.
  6. All the records in the file are sorted order based on key values.
  7. Duplicate records cannot be written into the file because of the key.
  8. Accessing records is faster because we can read them dynamically or randomly.

Syntax -

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT logical-file-name ASSIGN TO DSName 
    ORGANIZATION IS INDEXED
	RECORD KEY IS primary-key
	ALTERNATE KEY IS alt-key

Example - Declaring indexed file in program with key emp-no and alternate key emp-dept.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT empfile ASSIGN TO input01
    ORGANIZATION IS INDEXED
	RECORD KEY IS emp-no
	ALTERNATE KEY IS emp-dept

Relative File Organization -


Relative file organization applies to the RRDS file that uses the Virtual storage access method (VSAM). An RRDS file consists of records that are stored and accessed using the RRN value. Relative Record Number (RRN) is unique and used as an implicit key (generated by the system and not part of the record).

The main attributes of the indexed file organization are -

  1. Records are written in Record Relative Number (RRN) order.
  2. Records can be read sequentially (one by one without RRN value), randomly (using RRN-value), or dynamically (combination of the above two).
  3. Physical deletion of the record is allowed, but the memory is not reused.
  4. Updating a record (REWRITE) is possible.
  5. New records will be written sequentially and set RRN value for it.
  6. All the records in the file are sorted order based on RRN.
  7. Duplicate records can't be written into the file.
  8. Records accessing is faster than indexed files as the records can be read using RRN.

Syntax -

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT logical-file-name ASSIGN TO DSName 
    ORGANIZATION IS RELATIVE
	RELATIVE KEY IS rrn

Example - Declaring Relative file in program with key emp-rrn.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT empfile ASSIGN TO input01
    ORGANIZATION IS RELATIVE
	RELATIVE KEY IS emp-rrn