AS400 DB2 Notes

download AS400 DB2 Notes

of 8

Transcript of AS400 DB2 Notes

  • 8/12/2019 AS400 DB2 Notes

    1/8

    Physical Files

    Physical files contain data and are subdivided into database & source physical files.

    Physical files is the only type of file that actually contains data. Physical files are used whenthere can be any number of IDENTICA!"#$%ATTED entries 'each entry bein( a record)

    %ecord #ormat* A record format is a template that defines the fields contained in each record.This template has the name of the fields+ the type+ len(th and other characteristics.

    %ecord format is a set of fields.

    #ield is a sub"set of record 'or) a record format.

    #ield definition is nothin( but definin( a part of total characters in a record len(th.

    Definition means """"""", (ivin( name+ type+ len(th and other characteristics.

    #or E(*

    Assume that every new employee has to declare his-her various details while oinin( yourcompany. The various details include name+ a(e+ (ender+ s/ill+ nationality+ permanentaddress+ temporary address etc. 0e want one employee to fill up his-her details in one paperand we put all those papers in one file folder. Assume now that we had (iven a white paper toeach new employee and as/ed them to fill up above said details.

    1ecause we had (iven them ust white papers to fill up particular details+ variouscombinations of fillin( up the details are possible. Please chec/ below for a few possibleways.2stemployee*

    Name* 3arsh 4umarA(e * 526ender* ale7/ill * C pro(rammerNationality* IndianPermanent Address* DelhiTemporary Address* 1an(alore

    5ndemployee*

    Naresh 4umar+ 55+ ale+ A7899 pro(rammer+ Indian+ umbai+ Pune

    :rdEmployee*

    7eema 4hanna+ #emale+ 5;7/ill* %P6 pro(rammer

    Address* New Delhi

  • 8/12/2019 AS400 DB2 Notes

    2/8

    This way we can create N number of possible way of fillin( up details on a white paper. This is(enerally un"acceptable and not understandable in future readin(.

    7o+ the or(ani

  • 8/12/2019 AS400 DB2 Notes

    3/8

    #ield :* s/ill+ Type Alphanumerics+ len(th 59 #iled 8* Nationality+ type alphabetics+ len(th 59 #ield ;* Permanent Address+ Type Alphanumerics+ len(th ?99 #ield @* Temporary Address+ Type Alphanumerics+ len(th ?99

    Employee 7alary file record format

    #ield 2* Name+ type alphabetics+ len(th 599 #ield 5* 7/ill+ type alphanumeric+ len(th 59 #ield :* eperience years+ type floatin(+ len(th 8'two fields for decimal value) #ield 8* present proect+ type alphanumerics+ len(th 89 #ield ;* salary+ type floatin(+ len(th B

    7o+ the 3% creates a soft copy of a form based on its record format 'or blue print) and ta/es aprint fillin( up details of a salary details of an employee. That paper becomes the record ofemployee salary. And the file this paper is stored is called Employee salary file.

    7o+ now we understood the difference between field and format+ difference between formatand actual record+ difference between record and file.

    Assume that the 3% separately written format of each form in one paper. 7o+ if 3% ismaintainin( 299 files+ then there are 299 record formats 'one record format for each file).Thatmeans 299 record format papers. 'Each paper containin( record format of a different file). Ifthe 3% /eeps all those 299 record format papers 'blue prints) in one file+ we can call that fileas %ecord format file.

    If we apply this formattin( concept to A7-899 physical files 'which stores data)+ definin( name+type and len(th of a field is called field definition. If we define all fields and (ive a name to thisset of fields+ that name is called =%ecord format name=. The place where we store all recordformats of various files is called 7ource physical file 'E>uivalent to %ecord format file). Itcontains record format details of each physical file. 7ource Physical file contains members.$ne member contains record format details of one physical file.

    E(* If we want to create a physical file record format for employee physical file+ we have tofirst create a member in source physical file. Inside the member+ we must first (ive the nameof the record format. Then define name of each field+ its type and its len(th.

    1efore that+ we need to learn various types of data available in A7-899.

    A7899 Data Types* A for Character P for Pac/ed Decimal 7 for oned Decimal 1 for 1inary # for floatin( point 3 for headecimal for date T for time for timestamp

  • 8/12/2019 AS400 DB2 Notes

    4/8

    Fsin( above mentioned data types+ pro(rammers can define 'describe data) various fields ina record format. 1esides (ivin( names to each field+ it is mandatory to (ive name to a recordformat. These definitions are called Data Description 7pecifications. And record format detailsof one physical file is called a DD7 member.

    A typical Employee details record format in A7-899 is (iven below*

    % EP%EC#

    Empname 599A Empa(e 87 9 6ender 8A 7/ill 59A Nationality 59A Peraddr ?99A Tempaddr ?99A

    A pro(rammer can enter this source in a source physical file usin( 7ource Entry Ftility '7EF)editor. After enterin( the source+ it should be saved and press #: to come out of the screen.$nce the source is saved in a DD7 member+ one should compile the source in DD7 memberto create a memory location for the file to store data in the (iven format. $nly throu(hcompilin(+ the blue print transforms into an actual physical memory location with re>uiredformat. '$ne can find details of 7EF+ how to enter source and compile from 6oo(le).

    $ne should understand the difference between source file and physical file. 7ource filecontains the blue print of format details of a physical file. 1ut a physical file is actual formattedmemory location created usin( record format details in source physical file. 7o+ details ofrecord format in a source physical file is called =source code of a physical file in pro(rammer

    ar(on.

    Compilin( a source code creates a physical file obect 'table obect). At the time ofcompilation+ one has to mention how many records should be created in the file. If we don=tmention+ 29/ records will be created by default. It means 29/ rows of data is allocated in thementioned si

  • 8/12/2019 AS400 DB2 Notes

    5/8

    se(re(ated data is called =member= of that database physical file.

    1ut actually when we compile our DD7 source member+ it creates a formatted physical fileobect with empty records. 1ut+ all empty records constitute one member by default. Thismember can be accessed by its name. 1y default the first member created by defaultcontains the same name as physical file name. All 29/ records will be allocated to this first

    default member which has same name as physical file name. 0e can add more members toa database physical file usin( ADDP# command. Please refer to I1 red boo/s on how touse this command. There are various commands available in A7-899 to use various membersof a database physical file.

    7o now we /now what a field is+ what a record format is+ what a physical file is+ what a datamember is and how to create a database physical file. $nce we create database physical file+we can enter data.

    Enterin( data does not actually end the story. It actually starts the whole story ofpro(rammin(. 1ecause the very purpose of enterin( data is to use it for later. The =use= wordcovers a whole ran(e of activity li/e retrievin( data+ modifyin( data+ deletin( data etc.

    et us first spea/ about a mechanism involved in retrievin( data.

    Access Path 'or) Inde* 0hen data is stored in a physical file+ it is stored in #I#$ order bydefault. It means the first record that is stored will be the first record that will be retrievedwhen file is accessed for data.

    1ut the >uestion is+ how the $7-899 /nows what first record is and what is net record... soonG

    Apart from fields defined in a record format+ $7-899 adds another hidden field to each datarecord in file. That field name is %elative %ecord Number '%%N H nothin( but a serialnumber).

    E(* A normal record format and data

    Employee number Employee name Employee salary Employee 6rade

    8;@?2 3arish /umar 2999999 A

    ;@B:2 4usum 7hetty 2599999 1

    82 7andesh /apoor 29;9999 A

    %ecord format with %%N attached

    %%N EPN$ EPNAE EP7A EP6%ADE

    2 8;@?2 3arish /umar 2999999 A

    5 ;@B:2 4usum 7hetty 2599999 1

    : 82 7andesh /apoor 29;9999 A

  • 8/12/2019 AS400 DB2 Notes

    6/8

    %%N is a field that notes down which record arrived first and which record arrived net and soon.... it notes down the se>uence number of each number in the manner of their arrival in tothe physical file.

    That is how the $7-899 /nows the se>uence of arrival of records into file. 7o when recordsare retrieved from a file+ the records are retrieved in the se>uence of their arrival. And hencethe name Arrival se>uence Access path. %%N is li/e the pa(e number in a boo/. The firstpa(e is opened first.

    1ut what if you /now a word 'or topic headin( or concept in the boo/) and want to find its firstoccurrence in the boo/ and second occurrence and so on...G

    There is one conventional method. !ou read throu(h each word in each pa(e startin( frompa(e one till the last pa(e and note down the se>uence of occurrence of the word you areloo/in( for. This way you /now when the word first occurred and what pa(e numbers havethis word. 1ut it re>uires lot of effort.

    Assume that you want to repeat the same procedure for same concept every day. $n top ofthat assume you repeat this procedure to find out occurrences of various concepts+ headin(sand topics etc. It consumes a lot of time and most the effort is un"necessary too. Then what isthe solutionG

    $n the first attempt of this procedure+ it is wise to note down the occurrence se>uence+perhaps in a piece of paper. Note down the occurrence of various topics pa(e wise or topicwise. This is called indein(. !ou can see such indees at the end of each tet boo/. In theseindees+ various topics discussed in a boo/ are (iven in alphabetical order with pa(enumbers (iven for each topic. In this way+ you can (o to the pa(e number directly after findin(the topic name which is (iven in inde in alphabetical order.

    7o+ one can eplore the boo/ in two ways. Either via readin( each pa(e from pa(e 2 orreferrin( to inde+ one can ump to re>uired topic. 7o+ in the second way+ one need a topicname and inde. Topic name 'or) topic word is the /ey to eplore the boo/ in second way.This topic word can be called =4ey 0ord=.

    7imilarly+ one may want to find a particular value 'column value) in a particular record 'row) ofa file. The column value is called the field and row is called record in A7-899 physical file.

    A(ain+ we can read throu(h each record and value in the field of that record. If we apply theindein( concept to physical files that will help to complete our search for data >uic/ly.

    1ut there is a difference between applyin( indein( concept to a tet boo/ and to an A7-899physical file. In A7-899 physical file+ we only need to /now which record contains the leastvalue of a particular field and which record contains net least value... and so on. $r+ we onlyneed to /now which record contains the hi(hest value of a particular field and which recordcontains net hi(hest value... and so on. The $7-899 will note down such se>uence if we tellit to store such details.

    1ut remember that li/e topic name or topic word in tet boo/ indein(+ here in A7-899 field

  • 8/12/2019 AS400 DB2 Notes

    7/8

    name is the /ey to search its hi(hest or lowest value or any value. 7o+ when we tell to$7-899+ it should maintain an inde of values of a field either in ascendin( order ordescendin( order. 'Actually various other orders are also available).

    #or E(*

    Chec/ this table+ which is in arrival se>uence access path*

    %%N EPN$ EPNAE EP7A EP6%ADE

    2 8;@?2 3arish /umar 2999999 A

    5 ;@B:2 4usum 7hetty 2599999 1

    : 82 7andesh /apoor 29;9999 A

    If we want an inde of records based on employee number in ascendin( order. 3ere EPN$is the /ey field to create re>uired inde.

    3ere is how it loo/s when we create an inde based on EPN$ ascendin( *

    %%N EPN$ EPNAE EP7A EP6%ADE EPN$*A7C

    2 8;@?2 3arish /umar 2999999 A %%N2*7EJ2

    5 ;@B:2 4usum 7hetty 2599999 1 %%N5*7EJ:

    : 82 7andesh/apoor

    29;9999 A %%N:*7EJ5

    This se>uence filed is used when you want to retrieve the data in ascendin( se>uence ofEPN$ field. This is retrievin( data in se>uence of /ey value. This is called 4eyed se>uencein A7-899.

    7o+ we now learn two A7-899 terms here+ Arrival se>uence access path and /eyed se>uenceaccess path. These terms spea/s of how data is retrieved from a file. If we want the recordsin the same order which they were stored 'or) arrived into file+ that se>uence is called arrivedse>uence access path. 1ut+ we want records to be retrieved in the order of value of a /eyfield+ that se>uence is called /eyed se>uence access path.

    Note* 0ithout an inde eistin( first+ one cannot retrieve the data usin( inde. #or an inde toeist before retrieval+ there are three ways to create an inde. They are*

    2) Declarin( a /ey field while writin( source for a physical field. This is permanent.

    5) Creatin( a lo(ical file with /eyed se>uence access path on top of physical file. This alsoPermanent.

    :) 0hen neither the physical file nor the lo(ical file contains re>uired /eyed se>uence'it

  • 8/12/2019 AS400 DB2 Notes

    8/8

    means that either P# or # donKt have /eyed paths or they have /eyed se>uences based ondifferent /ey fields)+ one can temporarily create re>uired /eyed se>uence access path usin($verride database file and $pen >uery file commands. $pen >uery file command is powerfulutility that no serious application pro(rammin( is possible without usin( it.

    Now let us understand the concept of =resources= of a database physical file. A file = resource=simply means what the file =offers= for use to pro(rams and users. A file can (ive not only it=sdata but also its access path'inde) details+ details of its obect structure'maimum number ofrecords it can store+ record format name+ field names+ field types and len(th+ names of /eyfields+ it=s members etc.). 0ithout usin( all these resources+ no command or pro(ram canaccess a database physical file.