Professional Documents
Culture Documents
Version 2.0
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
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.
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,
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
1-Sep-99
10-Jun-00
350
CO40
CR56
Aline Stewart
PG36
10-Oct-00
1-Dec-01
370
CO93
PG16
1-Nov-02
1-Aug-03
450
CO93
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.
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
10-Oct-00
1-Dec-01
370
CO93
CR56
PG16
Aline Stewart
1-Nov-02
1-Aug-03
450
CO93
Tony Shaw
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
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) 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.
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
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:
19
AS/400 Objects:
20
AS/400 Objects:
Menu: The menu allows users to select the task they would
like to perform without having to use the system commands.
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.
22
Physical File
A
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
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
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.
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
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
29
Example
30
Keywords
31
Keywords
32
Physical Files
File Level Keywords Unique, LIFO,FIFO,FCFO,REF Record TEXT
Level Keywords
33
34
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).
35
Physical Files
Common
RUNQRY *N CUSTOMER DSPPFM CUSTOMER WITHIN STRSQL - SELECT * FROM CUSTOMER DLTF CUSTOMER DSPFFD CUSTOMER DSPFD CUSTOMER CLRPFM CUSTOMER - Clears the physical File. CHGPFM DSPDBR
36
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
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).
39
Logical Files
A Logical File is the logical view of a Physical file. It provides a access path to the physical File.
A Non- Join logical file can be either a simple logical file which contains only one record format or multi record format.
40
Logical Files
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
Region HY VZ LC BO
42
Logical Files
Dept AS UX LN CS
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
Logical File
44
45
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
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
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
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
49
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
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
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
DB2/400
54
DFU
Is
STRDFU
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.
Once a trigger is added to the physical file, all members of that specified file are affected by the trigger.
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
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
Interactive SQL
The
AS/400 also provides a SQL interface to the native environment through a interactive SQL interface.
STRSQL
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 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