Download presentation
Presentation is loading. Please wait.
Published byCharles Shelton Modified over 9 years ago
1
Chapter 15 Relative Files
2
File organization that converts key field to actual disk address to find location of record No need to look up disk address in index Convert key to disk address and access record directly Records may be accessed both sequentially and randomly
3
SELECT for Relative Files SELECT file-name-1 ASSIGN to implementor-name-1 [ORGANIZATION IS] RELATIVE [ACCESS IS SEQUENTIAL [RELATIVE KEY IS data-name-1] RANDOMRELATIVE KEY IS DYNAMIC data-name-1 [FILE STATUS IS data-name-2].
4
SELECT for Relative Files RELATIVE KEY clause Optional if ACCESS is SEQUENTIAL Otherwise, required ACCESS IS DYNAMIC allows both sequential and random access in same program FILE STATUS field used same way as with indexed files
5
FD for Relative Files RELATIVE KEY not part of record In separate WORKING-STORAGE entry If key is a three digit field and SELECT clause is Relative Key is R-Key Entry in WORKING-STORAGE is 01R-KeyPic 9(3).
6
Creating Relative Files When created sequentially, either computer or user can supply keys If RELATIVE KEY clause omitted, computer supplies keys First record placed in relative record location 1 (RELATIVE KEY = 1) Second record in relative record location 2 (RELATIVE KEY = 2), etc.
7
Processing Relative Files WRITE … INVALID KEY to write record to relative file READ … AT END to read sequentially READ … INVALID KEY to read randomly Move key value of record to locate to RELATIVE KEY before executing READ
8
Processing Relative Files REWRITE … INVALID KEY to update DELETE … INVALID KEY to remove record from file
9
Relative Keys Sometimes key field not feasible to use as relative key For example, a five digit Trans-No with values from 00001 to 99999 with only 1000 actual records would be wasteful 99999 record locations would need to be allocated but only a small portion used
10
Converting to Relative Keys Methods called hashing used to convert key field into relative record number Simple hashing method Divide Trans-No by 1009 Giving Num Remainder Rel-Key Rel-Key will be number from 0 to 1008 Add 1 to get relative record number from 1 to 1009, enough positions for 1000- record file
11
Relative Files Hashing algorithm used when: Creating relative file - each record's key field used to calculate RELATIVE KEY for positioning record in file Accessing file randomly - convert inquiry or transaction record's key to RELATIVE KEY before reading
12
ORGANIZATION: RELATIVE ACCESS MODE: SEQUENTIAL ENVIRONMENT DIVISION.... INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT EXAMPLE-FILE ASSIGN TO “EXAM.DAT” ORGANIZATION IS RELATIVE ACCESS MODE IS SEQUENTIAL.... DATA DIVISION. FILE SECTION. FD EXAMPLE-FILELABEL RECORDS ARE STANDARD RECORD CONTAINS 100 CHARACTERS. * 01 EXAMPLE-REC.... I/O STATEMENTS BY OPEN MODE OPEN MODE: INPUT OPEN INPUT EXAMPLE-FILE. READ EXAMPLE-FILE AT END MOVE “Y” TO WS-EOF-FLAG. CLOSE EXAMPLE-FILE.
13
ORGANIZATION: RELATIVE ACCESS MODE: SEQUENTIAL OPEN MODE: OUTPUT OPEN OUTPUT EXAMPLE-FILE. WRITE EXAMPLE-REC. CLOSE EXAMPLE-FILE. OPEN MODE: INPUT-OUTPUT OPEN I-O EXAMPLE-FILE. DELETE EXAMPLE-FILE RECORD. READ EXAMPLE-FILE AT END MOVE “Y” TO WS-EOF-FLAG. REWRITE EXAMPLE-REC. CLOSE EXAMPLE-FILE.
14
ORGANIZATION: RELATIVE ACCESS MODE: RANDOM ENVIRONMENT DIVISION.... INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT EXAMPLE-FILE ASSIGN TO “EXAM.DAT” ORGANIZATION IS RELATIVE ACCESS MODE IS RANDOM RELATIVE KEY IS WS-KEY.... DATA DIVISION. FILE SECTION. FD EXAMPLE-FILELABEL RECORDS ARE STANDARD RECORD CONTAINS 100 CHARACTERS. * 01 EXAMPLE-REC.... WORKING-STORAGE SECTION. 77 WS-KEYPIC 999 VALUE ZEROS. … I/O STATEMENTS BY OPEN MODE OPEN MODE: INPUT OPEN INPUT EXAMPLE-FILE. COMPUTE WS-KEY = expression. READ EXAMPLE-FILE INVALID KEY MOVE “Y” TO WS-READ-ERROR-FLAG. CLOSE EXAMPLE-FILE.
15
ORGANIZATION: RELATIVE ACCESS MODE: RANDOM OPEN MODE: OUTPUT OPEN OUTPUT EXAMPLE-FILE. COMPUTE WS-KEY = expression. WRITE EXAMPLE-REC INVALID KEY MOVE “Y” TO WS-WRITE-ERR-FLAG NOT INVALID KEY ADD 1 TO WS-WRITE-CTR. CLOSE EXAMPLE-FILE. OPEN MODE: INPUT-OUTPUT OPEN I-O EXAMPLE-FILE. COMPUTE WS-KEY = expression. DELETE EXAMPLE-FILE RECORD INVALID KEY MOVE “Y” TO WS-DELETE-ERR-FLAG. COMPUTE WS-KEY = expression. READ EXAMPLE-FILE AT INVALID KEY MOVE “Y” TO WS-READ-ERR-FLAG. REWRITE EXAMPLE-REC INVALID KEY MOVE “Y” TO WS-REWRITE-ERR-FLAG. COMPUTE WS-KEY = expression. WRITE EXAMPLE-REC INVALID KEY MOVE “Y” TO WS-WRITE-ERR-FLAG. CLOSE EXAMPLE-FILE.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.