Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 38 ยท File Handling

Indexed and Relative Files

Learn COBOL indexed and relative file organizations - RECORD KEY, ALTERNATE KEY, access modes, READ by key, START, REWRITE, DELETE, and how VSAM KSDS maps to these statements.

A sequential file is perfect when you process everything. But a customer calls and you need one record out of ten million: reading from the top would take minutes. COBOL has two organizations for direct access: indexed files, where each record is found by a key, and relative files, where each record is found by its position number. Indexed files are the workhorse of business systems and map onto IBM's VSAM key-sequenced data sets (KSDS) on the mainframe and onto ISAM-style files (GnuCOBOL uses Berkeley DB, VBISAM or similar) elsewhere.

Indexed files

An indexed file stores records together with an index on a field called the record key. The key must be unique:

COBOL
           SELECT ACCT-FILE ASSIGN TO "accounts.idx"
               ORGANIZATION IS INDEXED
               ACCESS MODE IS DYNAMIC
               RECORD KEY IS ACCT-NO
               ALTERNATE RECORD KEY IS ACCT-NAME
                   WITH DUPLICATES
               FILE STATUS IS WS-ACCT-STATUS.
       ...
       FD  ACCT-FILE.
       01  ACCT-REC.
           05  ACCT-NO       PIC 9(8).
           05  ACCT-NAME     PIC X(25).
           05  ACCT-BALANCE  PIC S9(9)V99 COMP-3.
  • RECORD KEY names a field inside the record. Its values are unique and records are kept in key order.
  • ALTERNATE RECORD KEY adds another index; with WITH DUPLICATES several records may share a value (file status 02 signals a duplicate was written or read).
  • ACCESS MODE says how you will use the file:
Mode Allowed reading Typical use
SEQUENTIAL In key order, from the first record or from a START Reports, bulk loads
RANDOM By key, one record at a time Online lookups, updates from transactions
DYNAMIC Both, in the same program Find a key then read onward
flowchart LR
    K["Key 00001234"] --> I["Index"]
    I --> R["Data record"]
    A["Alternate key 'SMITH'"] --> I2["Alternate index"]
    I2 --> R

The program asks for a key; the index finds the record without scanning the file.

Random reads

To fetch a record by key, move the key into the key field and READ:

COBOL
           MOVE 12345678 TO ACCT-NO
           READ ACCT-FILE
               INVALID KEY
                   DISPLAY "No such account"
               NOT INVALID KEY
                   DISPLAY ACCT-NAME
           END-READ

READ file here has no NEXT because we want the record named by the key. INVALID KEY catches status 23. To read by an alternate key use READ ACCT-FILE KEY IS ACCT-NAME.

Sequential and dynamic reads

In dynamic mode READ file NEXT RECORD steps forward in key order. START positions the file first, without reading a record:

COBOL
           MOVE 20000000 TO ACCT-NO
           START ACCT-FILE KEY >= ACCT-NO
               INVALID KEY
                   DISPLAY "No account from 20000000"
           END-START
           PERFORM UNTIL AT-EOF
               READ ACCT-FILE NEXT RECORD
                   AT END SET AT-EOF TO TRUE
                   NOT AT END PERFORM SHOW-ACCOUNT
               END-READ
           END-PERFORM

START accepts =, >, >=, <, <= against a key. This is how you read "all accounts from 20000000 upward", or all names beginning with a prefix.

Writing, replacing and deleting

Statement Effect Notes
WRITE rec Adds a record INVALID KEY if the key already exists (status 22). In sequential access, keys must arrive ascending (status 21 otherwise).
REWRITE rec Replaces the record just read The file must be open I-O. In sequential access, a successful READ must come immediately before. The key must not change.
DELETE file Deletes the record Random: the key in the record area names it. Sequential: the last record read. File is I-O.
COBOL
           OPEN I-O ACCT-FILE
           MOVE 12345678 TO ACCT-NO
           READ ACCT-FILE INVALID KEY PERFORM NOT-FOUND END-READ
           ADD 250.00 TO ACCT-BALANCE
           REWRITE ACCT-REC
               INVALID KEY DISPLAY "Rewrite failed"
           END-REWRITE

DELETE uses the file name, REWRITE and WRITE the record name. This is a frequent source of compile errors.

Relative files

A relative file is like a numbered array on disk: the record's relative record number (1, 2, 3, ...) determines where it lives.

COBOL
           SELECT SLOT-FILE ASSIGN TO "slots.rel"
               ORGANIZATION IS RELATIVE
               ACCESS MODE IS RANDOM
               RELATIVE KEY IS WS-SLOT-NO
               FILE STATUS IS WS-SLOT-STATUS.

WS-SLOT-NO is a numeric item in Working-Storage (not in the record). Moving 17 to it and issuing READ fetches record 17. A relative file has no index overhead and is fast, but you must map your business key to a number (for example MOD of a hash, or the key itself if keys are dense 1..n). Gaps are allowed: READ of an empty slot returns INVALID KEY. Relative files are less common today, but they suit small fixed tables and are the model for VSAM RRDS.

VSAM on the mainframe

On z/OS the data set type is chosen when it is defined with the IDCAMS utility:

  • KSDS (key-sequenced) = COBOL INDEXED.
  • ESDS (entry-sequenced) = COBOL SEQUENTIAL with ORGANIZATION IS SEQUENTIAL and VSAM storage.
  • RRDS (relative-record) = COBOL RELATIVE.

You can think of a KSDS as a B-tree on the key. The COBOL source above works unchanged; only the ASSIGN name (a DD) and the JCL differ. Note that VSAM file status codes in the 9x range carry extra feedback (JCL for COBOL jobs). Alongside VSAM, many shops use DB2 tables instead (embedded SQL).

Modelling the behavior

The Python model below imitates an indexed file with a sorted list and shows READ by key, START >= and sequential READ NEXT. It also reproduces status 22 and 23. It is a stand-in for explanation only.

Python Stand-in: indexed file with READ, START, WRITE, REWRITE and DELETE

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output shows the duplicate error 22, the successful read, the not-found 23, then records 20 and 40 in key order (30 was deleted, 20 was renamed) and finally end status 10.

Python Stand-in: relative record numbers as slots in a table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Here codes 17 and 33 both map to record 2, so the third write reports a collision. That shows why relative files need a strategy for keys that do not map one-to-one.

Choosing an organization

  • Process all records in order, no lookups: sequential.
  • Lookups and updates by a business key, millions of records: indexed.
  • Dense numeric keys, small tables, speed matters: relative.
  • Complex queries, many users, ad-hoc reports: a database.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which clause names the unique key of an indexed file?

    RECORD KEY names the primary key, which must be unique. Alternate keys can allow duplicates.
  2. 2. What does START ACCT-FILE KEY >= ACCT-NO do?

    START only positions; you follow it with READ NEXT.
  3. 3. Which open mode is required for REWRITE and DELETE?

    Changing or removing existing records needs I-O.
  4. 4. A VSAM KSDS corresponds to which COBOL organization?

    KSDS is key-sequenced and is accessed as an INDEXED file. ESDS maps to sequential and RRDS to relative.
  5. 5. Which file status reports a WRITE with an existing primary key?

    22 is the duplicate key condition; 23 is record not found.

Summary

  • Indexed files give direct access by RECORD KEY; alternate keys add more access paths.
  • ACCESS MODE is SEQUENTIAL, RANDOM or DYNAMIC; START positions, READ NEXT moves forward.
  • WRITE adds, REWRITE replaces the record just read, DELETE removes; handle INVALID KEY or status 21, 22, 23.
  • Relative files address records by number; VSAM provides KSDS, ESDS and RRDS equivalents.

Next: Sorting and merging