You are on page 1of 60

DB2/400

Version 2.0

Release Date: 25/06/2010

HCL Technologies Ltd.


DB2/400 ver 2.0
1

Database Management Systems


A

system whose overall purpose is to record and maintain information A database is a repository for stored data and programs to manipulate it. Data ModelsA data model is a collection of concepts for describing data A Schema is a description of a particular collection of data using the given data model The relational model is the most widely used model today
2

Levels of Abstraction

Many Views and single Conceptual and Physical Schema Views Describe how users see the data Conceptual Schema defines the logical structure (Fields)and relation between them. Physical Schema defines the physical files and Indexes

Relational DBMS

Based on Relational Mathematics principles Data is represented in terms of rows and columns of a table Addresses all types of relations Easy to design No anomalies for insert/delete/update

Relational Terminology

Tuple (Row) Attribute (Column) Relation (Table)

Integrity Constraints
A unique key can uniquely identify each row in a table A primary key is a combination of columns which uniquely specify a row. It is a special case of unique keys. One difference is that primary keys have an implicit NOT NULL constraint while unique keys do not.

The foreign key identifies a column or set of columns in one (referencing) table that refers to a column or set of columns in another (referenced) table. The columns in the referencing table must reference the columns of the primary key or other superkey in the referenced table.

Integrity Rules Entity Integrity-Primary key cannot have null value.

Referential Integrity-Foreign key should be the primary key in the referenced table.

Normalization
Normalization

- process of removing data redundancy by decomposing relations in a Database. The decomposition approach starts with one relation and the relation is decomposed into more number of relations to remove insert, delete and update anomalies.
1NF,

2NF, 3NF and BCNF can be achieved by this approach.

Un normalized
ClientNo cName propertyNo pAddress rentStart rentFinish rent ownerNo oName
Tina Murphy PG4 PG16 6 lawrence St,Glasgow 5 Novar Dr, Glasgow 1-Jul-00 31-Aug-01 350 CO40

CR76

John kay

1-Sep-02

1-Sep-02

450

CO93

Tony Shaw

PG4

6 lawrence St,Glasgow 2 Manor Rd, Glasgow 5 Novar Dr, Glasgow

1-Sep-99

10-Jun-00

350

CO40

Tina Murphy Tony Shaw Tony Shaw

CR56

Aline Stewart

PG36

10-Oct-00

1-Dec-01

370

CO93

PG16

1-Nov-02

1-Aug-03

450

CO93

First Normal Form


First Normal Form is a relation in which the intersection of each There are two approaches to removing repeating groups from unnormalized tables:

Removes the repeating groups by entering appropriate data in the empty columns of rows containing the repeating data. Removes the repeating group by placing the repeating data, along with a copy of the original key attribute(s), in a separate relation. A primary key is identified for the new relation.

First Normal Table(1st Approach)


ClientNo
CR76

propertyNo
PG4

cName
John Kay John Kay Aline Stewart

pAddress
6 lawrence St,Glasgow 5 Novar Dr, Glasgow 6 lawrence St,Glasgow

rentStart
1-Jul-00

rentFinish
31-Aug-01

rent
350

ownerNo
CO40

oName
Tina Murphy Tony Shaw Tina Murphy Tony Shaw

CR76

PG16

1-Sep-02

1-Sep-02

450

CO93

CR56

PG4

1-Sep-99

10-Jun-00

350

CO40

CR56

PG36

Aline Stewart

2 Manor Rd, Glasgow

10-Oct-00

1-Dec-01

370

CO93

CR56

PG16

Aline Stewart

5 Novar Dr, Glasgow

1-Nov-02

1-Aug-03

450

CO93

Tony Shaw

1NF with the second approach


ClientNo
CR76 CR56

cName
John Kay Aline Stewart

ClientNo
CR76

propertyNo
PG4

pAddress
6 lawrence St,Glasgow
5 Novar Dr, Glasgow 6 lawrence St,Glasgow 2 Manor Rd, Glasgow 5 Novar Dr, Glasgow

rentStart
1-Jul-00

rentFinish
31-Aug-01

rent
350

ownerNo
CO40

oName
Tina Murphy
Tony Shaw Tina Murphy Tony Shaw Tony Shaw

CR76

PG16

1-Sep-02

1-Sep-02

450

CO93

CR56

PG4

1-Sep-99

10-Jun-00

350

CO40

CR56

PG36

10-Oct-00

1-Dec-01

370

CO93

CR56

PG16

1-Nov-02

1-Aug-03

450

CO93

Full functional dependency


Full functional dependency indicates that if A and B are attributes of a relation, B is fully functionally dependent on A if B is functionally dependent on A, but not on any proper subset of A. A functional dependency AB is partially dependent if there is some attributes that can be removed from A and the dependency still holds.

Second Normal Form (2NF)


Second normal form (2NF) is a relation that is in first normal form and every non-primary-key attribute is fully functionally dependent on the primary key. The normalization of 1NF relations to 2NF involves the removal of partial dependencies. If a partial dependency exists, we remove the function dependent attributes from the relation by placing them in a new relation along with a copy of their determinant.

2NF ClientRental relation


ClientNo
CR76 CR56

cName
John Kay Aline Stewart

ClientNo
CR76 CR76 CR56 CR56 CR56

propertyNo
PG4 PG16 PG4 PG36 PG16

rentStart
1-Jul-00 1-Sep-02 1-Sep-99 10-Oct-00 1-Nov-02

rentFinish
31-Aug-01 1-Sep-02 10-Jun-00 1-Dec-01 1-Aug-03

Client

Rental PropertyOwner
propertyNo
PG4
PG16 PG36

pAddress
6 lawrence St,Glasgow
5 Novar Dr, Glasgow 2 Manor Rd, Glasgow

rent
350
450 370

ownerNo
CO40
CO93 CO93

oName
Tina Murphy
Tony Shaw Tony Shaw

Third Normal Form (3NF)


Transitive dependency A condition where A, B, and C are attributes of a relation such that if A B and B C, then C is transitively dependent on A via B (provided that A is not functionally dependent on B or C).

Third normal form (3NF) A relation that is in first and second normal form, and in which no non-primary-key attribute is transitively dependent on the primary key.
The normalization of 2NF relations to 3NF involves the removal of transitive dependencies by placing the attribute(s) in a new relation along with a copy of the determinant.

3NF ClientRental relation


Client
ClientNo
CR76 CR56

Rental
cName
John Kay Aline Stewart

ClientNo
CR76 CR76 CR56 CR56 CR56

propertyNo
PG4 PG16 PG4 PG36 PG16

rentStart
1-Jul-00 1-Sep-02 1-Sep-99 10-Oct-00 1-Nov-02

rentFinish
31-Aug-01 1-Sep-02 10-Jun-00 1-Dec-01 1-Aug-03

PropertyOwner
propertyNo
PG4 PG16 PG36

Owner
rent
350 450 370

pAddress
6 lawrence St,Glasgow 5 Novar Dr, Glasgow 2 Manor Rd, Glasgow

ownerNo
CO40 CO93 CO93

ownerNo
CO40 CO93

oName
Tina Murphy Tony Shaw

System Concepts

Object Oriented Technology :Everything on AS/400 that can be stored or retrieved is contained in an Object. A user is not concerned with the space his object occupies. The system allocates space automatically. The Licensed Internal Code that sits above the hardware with an Object Based Kernel. The Kernel has been written in C++ and is fully Object Oriented giving all the advantages of flexibility, code reuse, programming efficiency, and error reduction that come from Object Oriented Programming.

17

Single Level Storage :

The AS/400 system is a shared system in which all portions of main and auxiliary storage are addressed as though they are within a single area (or level). The system uses the object name to determine where the object exists in the system. operations cannot be performed on an object that is not in main storage, the system moves a part or all of the object into main storage as it is needed and moves it back into auxiliary storage when it is not needed. This transfer is controlled by the system and does not require control by the user or programmer

18

AS/400 Objects:

Libraries: A library is an object that is used to group related


objects and to find objects by name. Two different objects with the same can exist in the same library, only if their objects types differ. However, two objects with the same name and type can exist in different libraries. There are three types of libraries : Systems User Product

Folders: A folder is a named object that is used as a directory


for documents and other folders.

Files: File is an object that contains either a set of related


records

19

AS/400 Objects:

Programs: A program is an object containing a set of


instructions that tell the system where to get information, how to process it and where to put the results.

Job Queue: The jobq manages the batch request submitted


by the users.

Out Queue: As the job processes a request to print data it


gets data from a database file and uses the print device file to format the data.

Message Queue: Communication between programs


between jobs, between users, between users and programs and between users and the system occurs through messages.

Data Queue: Programs can set up data queues to be used


by the entire application so that all programs can refer to a single set of data and variables passed to the programs through the queue.

20

AS/400 Objects:

User Profiles: A user profile is an object that identifies a


particular user or a group of user to the AS/400 system. The user is known in the system by user profile name.

Menu: The menu allows users to select the task they would
like to perform without having to use the system commands.

Subsystem: A subsystem is a single, predefined operating


environment through which the system coordinates the work flow and resource use.

21

DB2/400
Database Files are files that contain data or provide access to data. Physical files Type(*FILE) Attribute (PF) Logical Files Type(*FILE) Attribute(LF)

PF can have only one record format. Field Reference files are files that contain no data but contain only descriptions of the fields. Source Physical files are also database files.

DB2/400 ver 1.0

22

Physical File
A

physical file is composed of four important parts,


Object Header : That contains regular object information Record Format : Describes the format in which information is stored. Access path : Describes the order in which the records are retrieved. Data Members - Contains Data

DB2/400 ver 1.0

23

Physical Files
Data Members
Physical File

Member 1
DB2/400 ver 1.0

Member 2

Member 3

24 the only two files which contain The PF s and Source Physical files are data and are the only files that contain members.

Physical Files

DDS describes structure the physical file.

File -- blank name type; optional; when used, must precede record type Record -- R name type; one required for physical file Field -- blank name type; describes fields; follows record; almost always present Key -- K name type; optional; follows field entries
File and field Naming convention

File Name Max of 10 Characters Record Format Max of 10 Characters Field Names should be unique.

A * in the 7th column of the DDS represents that as a comment line.


25

DB2/400 ver 1.0

Field-Level Entries

After you insert a record-format line, you can enter field-level specifications. For these, you leave the Name Type field blank. But you must enter values for:

Name (field name) Length (number of characters or digits) Data Type (e.g., character, zoned decimal, packed decimal, binary, date, time)

For numeric fields, you must also enter the number of decimal positions. Name. For record and field names, use from one to 10 characters, Embedded blanks are not allowed in a name. Within a record format, field names must be unique. Length. Length is the number of characters or digits in the field. For numeric data types, the length specification really means the number of digits in the field. For date, time, and timestamp types, you do not specify a value for length; it is determined by type, and for date fields, by format. The values shown (in the previous slide) for date, time, and timestamp data types are the number of characters needed to display the stored values, not the number of bytes required for disk storage.
DB2/400 ver 1.0
26

Field-Level Entries
Data Types Abbreviation P S B F F A H L T Z Data type Packed decimal Zoned decimal Binary Floating-point (short) Floating-point (long) Character Hexadecimal Date Time Timestamp

For zoned decimal(length 9), then the true length of the field is 9 (bytes and digits). But if the same field is typed P for packed decimal, then the value 9 specified for length really means nine digits, and the true field length is five bytes. (Length in bytes of a packeddecimal field can be calculated as (Total digits / 2) + 1, throwing away any decimal part.) This difference in length is due to the way in which numeric data is stored in packed-decimal format: two digits per byte except for the rightmost
27

DB2/400 ver 1.0

Field-Level Entries
Advantages of storing numeric data in packed-decimal format: The field itself is smaller (requiring fewer bytes of storage space) Arithmetic and logic operations are more efficient (requiring fewer intermediate conversion steps)

Minor disadvantages to storing numbers as packed decimal: A zoned-decimal numeric field of a physical file can be redefined as a character field through a logical file
NOTE: If you dont explicitly provide a data type, the default is A (alphanumeric/ character) when no decimal-positions value is specified. If you provide a decimalpositions value and do not specify a data type, DDS defaults to the numeric packeddecimal (P) data type.
DB2/400 ver 1.0
28

Field-Level Entries
If you do not use keyword DATFMT, the default is *ISO format whose display format is yyyy-mm-dd (10 characters). Timestamp data includes both the date and the time and is formatted as yyyymm-dd-hh.mm.ss.uuuuuu (26 characters), where uuuuuu represents millionths of a second

DB2/400 ver 1.0

29

Example

30

Keywords

31

Keywords

32

Physical Files
File Level Keywords Unique, LIFO,FIFO,FCFO,REF Record TEXT

Level Keywords

Field Level Keywords CMP,COLHDG,DATFMT,DATSEP,DFT,REFFLD,TEXT,TIM SEP,VALUES , range


Key Field -Level Keywords DESCEND,ABSVAL

DB2/400 ver 1.0

33

DB2/400 ver 1.0

34

Physical Files Members

ADDPFM) command adds a named member to a physical file, which must already exist on the system. The maximum number of members that can be added to the file is specified for the Maximum members (MAXMBRS) parameter on the Create Physical File (CRTPF) command or the Change Physical File (CHGPF) command. Each member added has the same attributes as those defined in the physical file, its own set of data records, and its own access path, as specified in the data description specifications (DDS).

DB2/400 ver 1.0

35

Physical Files
Common

commands On physical Files

RUNQRY *N CUSTOMER DSPPFM CUSTOMER WITHIN STRSQL - SELECT * FROM CUSTOMER DLTF CUSTOMER DSPFFD CUSTOMER DSPFD CUSTOMER CLRPFM CUSTOMER - Clears the physical File. CHGPFM DSPDBR

DB2/400 ver 1.0

36

Physical Files Constraints


Adding a Unique Constraint ADDPFCST FILE(MYLIB/LOCATIONS) TYPE(*UNQCST) KEY(REGION) CST(Personnel_by_REGION) This command adds a unique constraint to the LOCATIONS file in the MYLIB library. The field that defines the access path is REGION. The name of the access path is Personnel_by_REGION. Adding a Referential Constraint ADDPFCST FILE(ADMN/PERSONNEL) TYPE(*REFCST) KEY(REGION) CST(1994Hires) PRNFILE(MYLIB/LOCATIONS) PRNKEY(REGION) DLTRULE(*CASCADE) UPDRULE(*RESTRICT) This command adds a referential constraint to the PERSONNEL file in the ADMN library. The field that defines the access path is REGION, which is also the key for the parent file LOCATIONS in the MYLIB library. The name of the access path is 1994Hires. According to the delete rule of cascade, if a record in the LOCATIONS file is subsequently deleted, and that record matches a record in the PERSONNEL file, the record also will be deleted from the PERSONNEL file. According to the update rule of restrict, subsequent changes to the LOCATION file records defined in the constraint are restricted at the beginning of the update request.
DB2/400 ver 1.0
37

Adding a Check Constraint ADDPFCST FILE(PERSONNEL/SALARY) TYPE(*CHKCST) CST(Upper_Salary_Limit) CHKCST('EMPSAL <= 100000') This command adds a check constraint to the SALARY file in the PERSONNEL library. The check constraint will ensure an employee's salary may be a maximum of 100,000. RMVPFCST

Triggers ADDPFTRG

*PRIKEY/*UNQCST/*REFKEY DELETE/UPDATE/ADDITION

DB2/400 ver 1.0

38

Data dictionary

A Data dictionary is to setup a file with all the data fields used in the application, but with no data. whenever the field is needed the DDS for the file points back to this "dictionary" file (using REF or REFFLD).

DB2/400 ver 1.0

39

Logical Files

A Logical File is the logical view of a Physical file. It provides a access path to the physical File.

Non Join Logical Files

A Non- Join logical file can be either a simple logical file which contains only one record format or multi record format.

Join Logical Files

Joins 2 - 32 physical files.

DB2/400 ver 1.0

40

Logical Files

Non Join logical file - Single Record format


Employee 1000 1001 1002 1003 If file is keyed by state Employee 1000 1003 1002 1001 State AP UP MP HA State AP HA MP UP

DB2/400 ver 1.0

41

Logical Files

Non Join logical file - Accessing two files - Union on common key
Employee State 1000 AP 1001 UP 1002 MP 1003 HA If file is keyed by state
Employee 1000 1000 1000 1001 1001 1002 1002

Employee 1000 1000 1001 1002


State AP HY VZ UP LC MP BO

Region HY VZ LC BO

DB2/400 ver 1.0

42

Logical Files

Join logical file - Joining more than two files.


Employee Dept 1000 AS 1001 UX 1002 LN 1003 CS If two files are joined by the field.
Employee 1000 1001 1002 1003

Dept AS UX LN CS

Dept Description AS/400 Unix Lexis-Nexis Client Server

Dept Description AS/400 Unix Lexis-Nexis Client Server

Natural join, - Only matching from both files Left outer join - All records from primary missing secondary defaults Self join - Joining a file to itself.
43

DB2/400 ver 1.0

Logical File

DB2/400 ver 1.0 44

44

Multi format Logical File

DB2/400 ver 1.0 45

45

Logical File Member


If the DTAMBRS parameter is specified on the CRTLF or ADDLFM command as in the following example: DTAMBRS((PF1 M1) (PF2 (M1 M2)) (PF1 M1) (PF2 (*NONE)) (PF3 M3))

DB2/400 ver 1.0

46

Record format LOGRCD2 is associated with physical file member M1 in PF1 and M1 and M2 in file PF2. Record format LOGRCD3 is associated with M1 in PF1 and M3 in PF3. No members in PF2 are associated with LOGRCD3. If the same physical file name is specified on more than one PFILE keyword, each occurrence of the physical file name is handled as a different physical file.

Logical Files
Non

Join Logical file

File Level Keywords REFACCPTH, DYNSLT,UNIQUE,FIFO... Record Level Keywords PFILE Field Level Keywords SST,CONCAT,RENAME Key Field -Level Keywords DESCEND,ABSVAL Select Omit level keywords

DB2/400 ver 1.0

47

Logical Files
Join

Logical file

File Level Keywords JDFTVAL Record Level Keywords JFILE,JOIN,JFLD Field Level Keywords JREF, Key Field -Level Keywords DESCEND,ABSVAL Select Omit level keywords

DB2/400 ver 1.0

48

Logical File
Example
EMPNO 200 400 700 EMPNO 200 400

of a self join
EMPNAM Srinivas Sanjay S Murali EMPNAM Srinivas Sanjay MGRNO 400 700 800 MGR Name Sanjay S Murali

DB2/400 ver 1.0

49

Join Logical File

50

Commands

CPYF

CPYF FROMFILE(PERSONNEL/PAYROLL) TOFILE(TESTPAY/PAYROLL) MB ROPT(*ADD) CRTFILE(*YES) ERRLVL(10) Because MBROPT(*ADD) is specified, the copied records are added to any existing records in the to-file member. Because RCDFMT(*NONE) is assumed, the to-file TESTPAY/PAYROLL must have the same record format as the from-file. If more than ten recoverable errors occur during the copy operation, the operation ends. CPYSRCF CPYSRCF FROMFILE(QGPL/QCLSRC) TOFILE(MYLIB/CLSRC) FROMMBR(PGMA) This command copies records from member PGMA of database source file QCLSRC which is in the QGPL library. CRTDUPOBJ CRTDUPOBJ OBJ(FILEA) FROMLIB(LIB1) OBJTYPE(*FILE) TOLIB(LIB2) DATA(*YES) The file named FILEA in library LIB1 is duplicated and stored in library LIB2. Authorities granted for FILEA are granted to the new FILEA in LIB2. The data records, constraints, and triggers associated with FILEA in library LIB1 are copied to FILEA in LIB2.

WRKOBJ DSPOBJD
51

DB2/400 ver 1.0

Difference

CPYF copy file member is to copy file content to an existing file. If file does not exist then user can create a new file. The structure will be based on source file.

CRTDUPOBJ create duplicate object, create clone exactly as the source file with structure and content with its link to program and logical file and its authority setting as well. no need to recompile pgm that use this file

DB2/400 ver 1.0

52

Save Files
A save file can be used with save and restore commands to

contain data that would otherwise be written to tape or diskette. A save file can also be used like a database file to read or write records that contain save or restore information. A save file can also be used to send objects to another user on network.

Create a save file Save Object/ Libraries into the save file Save onto tape or Ship via FTP

CRTSAVF SAVLIB,SAVOBJ RSTLIB,RSTOBJ DSPSAVF


53

DB2/400 ver 1.0

DB2/400

DB2/400 ver 1.0

54

DFU
Is

a tool used to perform operations on data physical files. Can be used :


Enter data, Update files File inquiries

STRDFU

DB2/400 ver 1.0

55

Triggers

A trigger defines an event that is to occur with a delete, insert, update or read (any change) operation on a file.

The trigger program can be specified to be called before or after a change operation occurs.

A maximum of 300 triggers can be added to one physical file.

Once a trigger is added to the physical file, all members of that specified file are affected by the trigger.

DB2/400 ver 1.0

Trigger program will run for each insert of the record. Say you copied 100 records. Trigger program will be run for 100 times 56

Uses of triggers

Provide consistent auditing Prevent invalid transactions Enforce complex business rules Enforce complex security authorizations Provide automatic event logging Automatically generate derived column values Preserving data consistency across different database tables

Commands

ADDPFTRG RMVPFTRG
57

DB2/400 ver 1.0

WRKQRY

The Work with Query (WRKQRY) command shows the Work with Queries display. You can use this display to create, display, change, copy, delete, or print a query definition, or to run a query. IBM provided query language QUERY/400 This interface is easy to use but is different from PDM. you can write a query without ever naming it. Of course, you must name it if you wish to save it. CommandWRKQRY
58

DB2/400 ver 1.0

Interactive SQL
The

AS/400 also provides a SQL interface to the native environment through a interactive SQL interface.

STRSQL

DB2/400 ver 1.0

59

Journal Management
A journal is an AS/400 objects used primarily to record changes to data in a physical file member and optionally, changes to a logical files access path.
Records

file opens, File closes, system IPLs, user generated entries and other types of Activity related to physical file.
When

data is changed, a journal always puts after image entries into a journal receiver. Optionally, you can specify that the journal should put both before image and after images into the journal receiver.
Use

Journal Management to recover critical data in AS/400 files.

Journal

Management automatically creates a separate copy of file changes and, If necessary, uses the copy to recover data in file. CommandsCRTJRNRCV CRTJRN
DB2/400 ver 1.0
60

You might also like