Upload
vishnu400
View
200
Download
0
Embed Size (px)
Citation preview
DB2400 HANDOUT
DB400H01 Ver 1.0.0 5/9/2001 Page 1 of 10
DB2/400
Objective
To make the participants familiar with the DB2/400 concepts which includes creation of Physical
Files, data entry using DFU, creation of views and interactive SQL/400 sessions.
Features of DB2/400
DB2/400 is an RDBMS which comes installed along with the system. The whole of the DB2/400
component is an integral part of the LIC. DB2/400 supports upto 11 CODD rules and thus is a
very strong RDBMS. There are primarily two types of files that can be created on the AS/400.
They are :
1) Physical Files and
2) Logical Files
These files can be comfortably accessed from any of the High Level Languages used on the
AS/400 like RPG/400, CL/400, COBOL/400, C/400, etc. The data integrity is maintained
through various database constraints and triggers.
Data Types in DB2/400
The following are the prime data types supported by DB2/400. They can be represented by a
single character assigned for each data type in the column of data type in the DDS.
A – alphanumeric – 32766 characters
P – packed decimal – 31 digits, 30 decimal positions
S – zoned decimal - 31 digits, 30 decimal positions
L – Long date – 4 bytes
Z – Time stamp – 10 bytes
T – Time – 6 bytes
F – Floating point – 1 – 9 digits in single precision & 1 – 17 digits in double precision
H – Hexadecimal – 32766 bytes
Physical Files
A physical file is an AS/400 object with the object type *FILE and the attribute PF. A physical
file is composed of three components, namely,
1) Record Format
2) Access Path
3) Data Members
1) Record Format – It comprises of the formal definition of the fields and their respective
attributes within the file. It is normally coded using the DDS keywords through the SEU.
2) Access Path – It defines the path as to how the records in the Physical File would be
accessed.
3) Data Members – It is this segment of the Physical File which stores data physically. One PF
can have many data members, each data member in turn having many records in it. However,
all the data members share the same record format and the access path.
DB2400 HANDOUT
DB400H01 Ver 1.0.0 5/9/2001 Page 2 of 10
This architecture can be represented pictorially as follows:
The maximum length of the record format cannot exceed 32766 bytes. A physical file can have
maximum of 120 key fields. It can have a maximum of 32766 members. The maximum no. of
fields that a PF allows is 8000.
The record format can be described using the SEU and the DDS keywords. A physical file can be
created using the command
CRTPF (FILE/LIBRARY)
This creates an object of the type *FILE and with the special attribute as PF-DTA. Such a PF has
the default no. of members as 1 and the maximum no. of records as 10000 and an extended
capacity of 1000 incremented thrice (that is, 13000 records). However, by pressing F4, we can
change the options and specify the desired values.
The members can be added by using the command ADDPFM. Change to the members can be
made by CHGPFM. Deletion of a member is possible through RMVM.
Data entry into a PF can be made in three ways, viz.,
1) DFU – Data file utility – The command is STRDFU. This supports insert, update
delete of the records, but only one at a time.
2) IDDU – Interactive Data Definition Utility – The command is STRIDD.
3) Interactive SQL – STRSQL is the command to start an SQL session and using the
DML commands, data manipulation can be achieved.
The records can be viewed using the RUNQRY command
RUNQRY QRYFILE(LIBRARY/FILE)
Suppose, a change in the PF structure is required after data entry, we can change it through the
SEU and re-compile it but the entire data is lost in this case as the object is deleted and recreated.
RECORD FORMAT
ACCESS PATH
DATA
MEMBER
DB2400 HANDOUT
DB400H01 Ver 1.0.0 5/9/2001 Page 3 of 10
So, an alternative for this is the command CHGPF where in the options for mapping and dropping
the fields can be specified. This however retains the data to the maximum possible extent and
any data entry made thereafter is appended to the previous data.
To copy the PF object the command used is CPYF which has got various options for specifying
the source and the destination files and also to create/replace the file and its members and also to
map/drop the fields.
The other commands related to the PFs are :
CLRPFM – Clears the PF member.
DLTF – Deletes the PF object.
DSPPFM – Displays the records in physical file member.
DSPFFD – Displays the file field information.
DSPDBR – Displays the data base relation with other files.
Physical File Keywords
The keywords define the special attributes of every field and are entered in the function column in
the DDS. There are 4 levels of keywords that can be defined in the PF, viz.,
1. File Level
2. Record Level
3. Field Level
4. Key field Level
1) File Level – Keywords entered at the file level, i.e., before any other line of code in a
PF, are known as file level keywords.
The file level keywords are :
UNIQUE – Defines the key field entry as unique, i.e., no duplicate key values could
exist in the PF.
FIFO – First In First Out – The records within the group of repetitive key field data
retrieved in the order they were ordered..
LIFO – Last In First Out – The records within the group of repetitive key field data
are accessed in the reverse order of their entry.
REF – Gives the reference to some other Physical File which can be used to refer to
the fields individually for their attributes.
2) Record Level – Keywords entered at the Record Format Level.
The record level keywords are :
FORMAT – When you want to refer to the entire format of a pre-existing PF we use
this keyword. However the record format name can be changed, and also the key
DB2400 HANDOUT
DB400H01 Ver 1.0.0 5/9/2001 Page 4 of 10
field can be changed provided it is one of the existing fields. Defining any fields
while using this keyword will result in a compile time error.
TEXT – Is used to provide some documention/description to the file.
3) Field Level – These keywords are provided at the respective field level.
COLHDG – Column Heading – Once specified, can be used in reports and screens.
COLHDG(‘Employee Code’)
The column heading ‘Employee Code’ Will appear in report and screens.
COLHDG(‘Employee’ ‘Code’) the heading will appear in two lines.
COMP – Compares the value entered with that specified along with this keyword.
Data entered in the field must satisfy condition specified in COMP keyword otherwise system
restricts and gives error.
COMP(GT 6000) value in this field must be greater than 6000.
The operators like GT are
LT = Less Than
EQ= Equal
NE=Not Equal
LE=Less than equal
GE=Greater than or equal
VALUES – Allows data entry of only the specified values.
RANGE – Allows data entry between the specified range of values only.
CHECK – Checks for mandatory entry or mandatory fill or both for a given field.
This keyword can be specified only against character type of field.
CHECK(ME) stands for mandatory entry. That means this field can not be left with null.
CHECK(MF) stands for mandatory fill. User must enter data full of this field.
DFT – Specifies a default value for a field when a value has not been entered.
REFFLD – Allows to specify the reference of a given field from an external file other
than the one specified at the REF.
TEXT – A description for the field can be specified here.
ALIAS – Allows the entry of an alternative name for a field.
4) Key-field Level – These keywords are specified at the key-field level.
DESCEND – Processes the records in the descending order of the key value.
DB2400 HANDOUT
DB400H01 Ver 1.0.0 5/9/2001 Page 5 of 10
ABSVAL – Allows only absolute value entry into the key field.
DIGIT – Sorts the records based on the numeric value in the alphanumeric key field.
SIGNED – Sorts the records based on the signed value of the key field value.
Columns . . . : 1 71 Edit STDLIB/MANSAM
SEU==> MASTERT
FMT PF .....A..........T.Name++++++RLen++TDpB......Functions++++++++++++++++++
*************** Beginning of data *************************************
0001.00 A UNIQUE
0002.00 A R MASTREC TEXT(‘MASTER FILE’)
0003.00 A ACCNO 5P 0
0004.00 A ACCTYPE 1A VALUES('R' 'S')
0005.00 A ACCNM 20A COLHDG(‘CUSTOMER NAME’)
0006.00 A ACCADD 40A
0007.00 A ACCBAL 10P 2
0008.00 A OPENBAL 10P 2
0009.00 A ACCOPDATE L
0010.00 A ACCSTATUS 1A VALUES('O' 'C')
0011.00 A DURATION 3P 0 VALUES(12 24 36)
0012.00 A K ACCNO DESCEND
****************** End of data ****************************************
Constraints
A PF can be applied with various constraints which can be added by the command ADDPFCST.
There are four kinds of constraints which can be applied to a PF. They are:
1) Primary Key – specified by *PRIKEY option
2) Reference (Foreign key) – specified by *REFCST option
3) Check Constraint – specified by *CHKCST
4) Unique Key – specified by *UNQCST option
DB2400 HANDOUT
DB400H01 Ver 1.0.0 5/9/2001 Page 6 of 10
Columns . . . : 1 71 Edit STDLIB/MANSAM
SEU==> TRANST
FMT PF .....A..........T.Name++++++RLen++TDpB......Functions++++++++++++++++++
*************** Beginning of data *************************************
0001.00 A UNIQUE FIFO
0002.00 A R TRANREC TEXT(‘TRANSACTION FILE’)
0003.00 A TACCNO 5P 0
0004.00 A TRANTYPE 1A VALUES('D' 'W')
0005.00 A TRANAMT 10P 2 CHECK(ME)
0006.00 A DATEOFTRAN L DATFMT(*EUR)
0007.00 A K ACCNO
****************** End of data ****************************************
Logical Files
A logical file is a database file used to view data stored in one or more physical files in the
desired manner. It is an AS/400 object of the type *FILE and attribute LF. The features of
logical files are :
1) They don’t physically contain data.
2) Access path can be changed as per the requirement.
3) A PF can be processed by multiple LFs.
4) Two or more LFs may share the same access path.
5) Records could be selected or omitted from the PF in a specified criteria.
6) LFs can have more than one record format but each record format must relate to one
or more PFs and include at least one key field. The record format lengths may vary.
7) An LF with multiple record format may process the data from more than one PF
treating them as data from a single file.
8) The PF fields’ attributes may be changed in an LF but the data in the PF is not
affected.
9) A maximum of 32 PFs can be joined or unionized using these files.
There are two broad categories of Logical Files, viz.,
a) Non-Join Logical Files and b) Join Logical Files
Non-Join Logical Files
DB2400 HANDOUT
DB400H01 Ver 1.0.0 5/9/2001 Page 7 of 10
A non-join logical file processes each record individually from one or more physical files. A
non-join logical file can have from 1 to 32 record formats. It can be used for record update in
case it is referring to just one PF. In addition to the 4 levels of keywords used in PF, there is one
select/omit level keyword in non-join logical files.
1) File level – Apart from that used in the PF an additional keywords DYNSLT and
ACCPTH are used. DYNSLT is used to specify the dynamic selection and omission
criteria. ACCPTH is used to refer to the access path from some other PF.
2) Record level – PFILE is used to specify which PF/PFs the LF is referring to. A
maximum of 32 PFs could be referred to in a LF.
3) Field level –
CONCAT – used to concatenate two or more fields.
SST – used to substring a field.
RENAME – used to rename an existing field of PF.
4) Key-Field level – They are the same as applicable for a PF.
5) Select/Omit level – When the DYNSLT is used at the FILE level, the S or O options
are specified at the select/omit level to specify the dynamic selection/omission
criteria of records from the PF. However, the S should always follow the occurance
of O. Two consecutive O or S or a combination of them implies an OR condition,
whereas, one O or S followed by another condition is an AND condition.
Union
Records from multiple PFs having a common record format accessed together in a non-join
logical file is called a Union. This can be put in three different ways :
1) All the PFs involved should have exactly the same record format.
2) The record format of one PF is a part of the record format of another PF.
3) Both the files may be having totally different record format but the common fields
are put together in the UNION.
Columns . . . : 1 71 Edit STDLIB/MANSAM
SEU==> TRALF
FMT LF .....A..........T.Name++++++RLen++TDpB......Functions++++++++++++++++++
*************** Beginning of data *************************************
0001.00 A R TRANREC PFILE(STDLIB/TRANST)
0002.00 A K DATEOFTRAN
****************** End of data ****************************************
Note : This will create a non-join logical file on a single PF and includes all fields from the
record format of TRANST.
DB2400 HANDOUT
DB400H01 Ver 1.0.0 5/9/2001 Page 8 of 10
Columns . . . : 1 71 Edit STDLIB/MANSAM
SEU==> TRALF
FMT LF .....A..........T.Name++++++RLen++TDpB......Functions++++++++++++++++++
*************** Beginning of data *************************************
0001.00 A R TRANREC PFILE(STDLIB/TRANST)
0002.00 A ACCNO RENAME(TACCNO)
0003.00 A DATEOFTRAN
0004.00 A K DATEOFTRAN
****************** End of data ****************************************
Note : This will create a non-join logical file on a single PF and includes only the named fields
from the record format of TRANST.
Columns . . . : 1 71 Edit STDLIB/MANSAM
SEU==> TRALF
FMT LF .....A..........T.Name++++++RLen++TDpB......Functions++++++++++++++++++
*************** Beginning of data *************************************
0001.00 A R TRANREC PFILE(TRANST1 TRANST2)
0002.00 A FORMAT(TRANST1)
0003.00 A K TACCNO
****************** End of data ****************************************
Note : This will create a non-join logical file on a multiple PFs (assuming TRANST1 TRANST2)
and includes the record format from TRANST1. This is also a type of a UNION.
Join Logical Files
Fields from multiple PFs put together to form a record format based on a common field which
may have same or different name but common attributes is known as a Join Logical File. Such
files are read-only files. There can be only one record format for a join logical file. The levels of
keywords in the join logical file are file level, record level, join level, field level, and key field
level keywords.
1) File Level – Same as PF.
JDFTVAL – Used to create a special type of Join Logical File discussed later.
2) Record Level – In addition to that used in the PF, we use,
JFILE – Used to join the different files. Maximum 32.
3) Join Level –
JOIN – Used to specify the join in between any two files used in the JFILE.
JFLD – Used to specify the field on which the files are being joined.
JDUPSEQ – Used to process the records from PF that have duplicate join fields.
DB2400 HANDOUT
DB400H01 Ver 1.0.0 5/9/2001 Page 9 of 10
4) Field Level –
JREF – It is used to refer to the definition of a field defined in one of the PFs
involved in the join.
5) Key Field level – Same as PF.
There are two types of Join Logical files, viz., Intersection and Left Outer Join.
Intersection – By default, the Join Logical file is an intersection.
Left Outer Join – By specifying the keyword, JDFTVAL at the file level, we achieve a left outer
join. In this case, the records of the primary file which do not have a secondary match are also
included in the join and the fields are padded with default values.
DEPARTMENT :
----------------
0001.00 A UNIQUE
0002.00 A R DEPTREC
0003.00 A DEPTNO 2P 0
0004.00 A DEPTNAME 10A
0005.00 A LOCT 10A
0006.00 A K DEPTNO
EMPLOYEE1 :
-----------------
0001.00 A UNIQUE
0002.00 A R EMPREC
0003.00 A EMPCD 5P 0
0004.00 A EMPNM 20A
0005.00 A DEPT 2P 0 COLHDG('DEPARTMENT')
0006.00 A BASIC 7P 2
0007.00 A DA 7P 2
0008.00 A SEX 1A VALUES('M' 'F')
0009.00 A K EMPCD DESCEND
0010.00 A K DEPT
DYNSLT CONCEPT :
----------------------
0001.00 A DYNSLT
0002.00 A R JOINREC JFILE(DEPARTMENT EMPLOYEE1)
0003.00 A J JOIN(DEPARTMENT EMPLOYEE1)
DB2400 HANDOUT
DB400H01 Ver 1.0.0 5/9/2001 Page 10 of 10
0004.00 A JFLD(DEPTNO DEPT)
0005.00 A DEPTNO
0006.00 A DEPTNAME
0007.00 A LOCT
0008.00 A EMPCD
0009.00 A EMPNM
0010.00 A TEMP 1 I SST(EMPNM 1 1)
0011.00 A SEX
0012.00 A K DEPTNO
0013.00 A* K EMPCD
**NOTE:2 KEY FIELDS NOT TO BE SPECIFIED**
0014.00 A S DEPTNO COMP(EQ 10)
0015.00 A S TEMP VALUES('M')
***************************************************
JDUPLICATE SEQUENCE :
-----------------------------
0001.00 A DYNSLT
0002.00 A R JOINREC JFILE(DEPARTMENT EMPLOYEE1)
0003.00 A J JOIN(DEPARTMENT EMPLOYEE1)
0004.00 JFLD(DEPTNO DEPT)
0005.00 JDUPSEQ(EMPCD *DESCEND)
0006.00 A DEPTNO
0007.00 A DEPTNAME
0008.00 A LOCT
0009.00 A EMPCD
0010.00 A EMPNM
0011.00 A TEMP 7 I SST(LOCT 1 7)
0012.00 A SEX
0013.00 A K DEPTNO
0015.00 A S DEPTNO COMP(EQ 10)
0017.00 A S TEMP VALUES('UNIT112')
JDFTVAL :
0001.00 A JDFTVAL
0002.00 A DYNSLT
0003.00 A R JOINREC JFILE(DEPARTMENT EMPLOYEE1)
0004.00 A J JOIN(DEPARTMENT EMPLOYEE1)
0005.00 JFLD(DEPTNO DEPT)
0006.00 JDUPSEQ(EMPCD *DESCEND)
0007.00 A DEPTNO
0008.00 A DEPTNAME
0009.00 A LOCT
0010.00 A EMPCD
0011.00 A EMPNM
0012.00 A TEMP 7 I SST(LOCT 1 7)
0013.00 A TEMP1 1 I SST(EMPNM 1 1)
0014.00 A SEX
0015.00 A K DEPTNO
0017.00 A O TEMP1 VALUES('S')
0018.00 A S DEPTNO COMP(GT 10)
0019.00 A S TEMP VALUES('UNIT112')
*******************************************************************************
SELFJOIN : A Join is formed on the same PF.
0001.00 A R JOINREC JFILE(SELFPF SELFPF)
0002.00 A J JOIN(1 2)
0003.00 JFLD(MGRCD EMPCD)
0004.00 A EMPCD JREF(1)
DB2400 HANDOUT
DB400H01 Ver 1.0.0 5/9/2001 Page 11 of 10
0005.00 A DEPT JREF(1)
0006.00 A EMPNM JREF(1)
0008.00 A MGRCD JREF(1)
0009.00 A MGRNM JREF(2) RENAME(EMPNM)
0010.00 A K EMPCD
SELFPF :
INPUT FILE :
EMPCD EMPNM DEPARTMENT BASIC MGRCD
1 MANISH 10 141.34 50
2 RAJESH 20 992.92 50
3 YOGESH 10 992.82 60
4 RAJ 30 2,511.12 60
5 UNMESH 20 992.02 50
50 BARU 40 70,000.00 70
60 RAJU 40 45,456.55 70
70 BHARAT 50 99,999.99 70
OUTPUT RESULT :
EMPCD EMPNM DEPARTMENT BASIC MGRCD MGR NAME
1 MANISH 10 141.34 50 BARU
2 RAJESH 20 992.92 50 BARU
3 YOGESH 10 992.82 60 RAJU
4 RAJ 30 2,511.12 60 RAJU
5 UNMESH 20 992.02 50 BARU
50 BARU 40 70,000.00 70 BHARAT
60 RAJU 40 45,456.55 70 BHARAT
70 BHARAT 50 99,999.99 70 BHARAT