Module 1: DB2 Introduction
DB2 vs VSAM
VSAM files and DB2 tables are the two main ways a COBOL program stores data on the mainframe. This page compares them so you know when each one fits.
VSAM in one paragraph
- VSAM (Virtual Storage Access Method) stores data in files: KSDS, ESDS and RRDS.
- A COBOL program opens the file and reads or writes records with file I/O verbs such as READ and WRITE.
- Each file is independent; matching related data across files is the programmer's own logic.
- Sharing a file between many programs at once is limited and must be managed manually.
DB2 in one paragraph
- DB2 stores data in tables, and programs access the data with SQL instead of file I/O verbs.
- Many programs and users can read and update the same tables concurrently; DB2 handles the locking.
- Tables are related through primary keys and foreign keys, so related data is linked by design.
- You can ask new questions with a SELECT typed on the spot, without writing a new program.
DB2 vs VSAM: the comparison
- Access method: VSAM uses COBOL file I/O (READ, WRITE on a file); DB2 uses SQL (SELECT, INSERT, UPDATE, DELETE on tables).
- Data organization: VSAM stores records in files (KSDS, ESDS, RRDS); DB2 stores rows in tables inside tablespaces.
- Relationships: VSAM files are independent and related data must be matched by program logic; DB2 tables relate through primary and foreign keys.
- Concurrent sharing: VSAM sharing is limited and manual; DB2 is built for many users and programs working on the same data at once.
- Ad-hoc queries: VSAM needs a new program for every new question; DB2 answers with a SELECT.
- Data integrity: VSAM leaves validation to the program; DB2 enforces keys, NOT NULL and referential rules itself.
- Recovery: VSAM recovery is the shop's own procedure; DB2 provides logging, security and recovery facilities.
- Under the covers: DB2 itself stores table data in VSAM data sets on disk, allocated through STOGROUPs.
Same idea, two styles: example
- Reading one employee by key, first as a VSAM file read, then as DB2 SQL:-
* VSAM file read in COBOL READ EMP-FILE AT END MOVE 'Y' TO EOF-FLAG END-READ. * Same idea against a DB2 table EXEC SQL SELECT LNAME, FNAME INTO :WS-LNAME, :WS-FNAME FROM EMPLOYEE WHERE EMPNO = :WS-EMPNO END-EXEC.
- The VSAM READ fetches the next record from an open file; key handling and end-of-file logic are yours.
- The EXEC SQL block asks DB2 for exactly the row whose EMPNO matches, and puts the columns into COBOL host variables (:WS-LNAME, :WS-FNAME).
