Sequential file, Sequential access,Indexed file, Dynamic access

Sequential file, Sequential access

       ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT PERSON-FILE
ASSIGN TO 'A:\PERSON.DAT'
ORGANIZATION IS LINE SEQUENTIAL.

Indexed file, Dynamic access

       ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT PERSON-FILE
ASSIGN TO 'A:\PERSON.DAT'
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS PERSON-KEY
ALTERNATE RECORD KEY IS PERSON-ALTERNATE-KEY
WITH DUPLICATES
FILE STATUS IS IO-FILE-STATUS.

One field must be designated the primary key. The primary key must be unique within the file; no two records in the file may have the same value for the primary key field. For example, social security number is a unique value per person.

The alternate key is optional. And, the alternate key is not required to be unique. For example, a data file might contain records for more than one person with a common name such as John Smith. An alternate key such as name may have duplicate values within the file, but the records could be distinguished by their unique primary key (e.g., social security number).

There may be multiple alternate key fields. For example, a data file might have social security number as its primary key, name as one alternate key, and telephone number as another alternate key.

Dynamic mode in Cobol allows the program both random and sequential access as needed to an indexed file.