Home > AS/400 Tips > iSeries programmer tips > Revisit your database naming conventions
iSeries 400 Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ISERIES PROGRAMMER TIPS

Revisit your database naming conventions


Paul Tuohy
03.10.2004
Rating: -2.65- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   





There is something that has been annoying me for quite a while. It is one of those niggling little things that we have been doing for so long that we take it for granted. We do it for historical reasons and take it for granted that that is the way we have to do it. I am talking about the naming conventions we use on our databases.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

      * Field Reference File (FLDREF)
 
                R FLDREFR     
                  CUSN           8          TEXT('Customer Code')
                  NAME          30          TEXT('Supplier')
 
 
      * Customer File (CUSFIL)
                                            REF(FLDREF)
                R CUSFILR
                  CSCUSN    R               REFFLD(CUSN)
                  CSNAME    R               REFFLD(NAME)


Figure 1: Field Reference File and referencing Physical File

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Figure 1 shows a snippet of what I am talking about. We have a field reference file (FRF) and a physical file (PF) that refers to it. The field names on the FRF are a maximum of four characters long because we use a unique two character prefix on the PF to identify the file to which the field belongs. Does this look familiar? OK, maybe your database uses a suffix instead of a prefix.


Paul Tuohy

The length of the field names had to do with good old RPG III where the maximum length of a field name could not exceed six characters. We all know that RPG IV can handle the full 10-character field name and, of course, we are starting to use these in our databases.

But why the need for a unique identifier? The reason being that all fields in an RPG program are global, so we need to know which file a field came from and we need to ensure that input from one file does not accidentally overwrite input from another file -- which is what would happen if we had two files with fields of the same name.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

      * Customer File (CUSTOMER)
                                            REF(FIELDREF)
                R CUSTOMERR
                  CUSTCODE  R
                  NAME      R
 
 
      * Supplier File (SUPPLIER)
                                            REF(FIELDREF)
                R SUPPLIERR
                  SUPPCODE  R
                  NAME      R



Figure 2: DDS for two files with the same name.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Figure 2 shows the DDS for two files (a Customer File and a Supplier File), both of which contain a field called NAME.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

     FCustomer  IF   E           K Disk                                                             
     FSupplier  IF   E           K Disk                                                             
             
               Read Customer;
               Read Supplier;



Figure 3: RPG IV Program reading records with duplicate field names.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

Figure 3 shows an RPG program that opens these two files and reads a record from each. When the program reads a record from the Customer file it will place a value in the NAME field, then when it reads a record from the Supplier file it will overwrite the value in the NAME field -- hence the requirement for a unique identifier on each file.

This seems like a very reasonable premise, but it is using a very large hammer to crack a very small nut and leads us into some coding practices that may cause larger problems then the one they aim to cure.

Let us start with the database. It is a relational database. It is a normalized relational database. This means that a column (field) should be defined in one table (physical file) and one table only -- the only exception being "key," fields which are used to identify relationships, e.g. an invoice number on both the invoice header and invoice detail files.

The instance shown in Figures 2 and 3 would be the result of a badly designed database. The Customer name and Supplier name are not the same entities -- although they are both names. Think of NAME as being a data type as opposed to a field and it should have been taken care of in the field reference file, as shown in Figure 4. The NAME field is defined as a "generic" name, and the actual fields (CUSNAME and SUPPNAME) refer back to it for their definitions.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

      * Field Reference File (FLDREF)
 
                R FLDREFR     
                  NAME          30          TEXT('Generic Name')
 
                  CUSNAME   R               REFFLD(NAME *SRC)
                                            TEXT('Customer Name')
                  SUPPNAME  R               REFFLD(NAME *SRC)
                                            TEXT('Supplier Name')



Figure 4: RPG IV Program reading records with duplicate field names.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

So, in an RPG program the only fields that should be "double defined" are key reference fields, and more then likely we will have the same value in them, since we use them to link between files (e.g. use a CHAIN operation to get the record from the invoice header and use a READE loop to read all corresponding records from the invoice details file). No big benefit here, from unique identifiers.

On the other hand, think of the problems that unique identifiers cause. We have all had that old chestnut during testing where we can't understand why we are getting that duplicate record error until we notice that we are writing records with a blank key field -- because we forgot to move the "parent" key field to the "child" key field. It would not have been a problem if we were using the same field name on both files.

Worse still, what about using unique field names on display files and print files -- now we have to move all the fields, not just key reference fields. You just have to remember that when you add a new field to a file and any corresponding display files, you must put in the appropriate MOVE operations as well.

There seems to be much less code when we are not using unique identifiers.

Now, there are some cases where we might have a problem with the same field name on multiple files. After all, what we do in our programs can often be weird and wonderful. The example in Figures 2 and 3 could well arise if the two files came from applications provided by different software vendors.

If you need unique field names, why not try one of the following:

  1. Define a logical file, which renames the fields that you want to use (as shown in Figure 5). This is what we should have been doing for RPG III programs!
  2. Use the PREFIX keyword on the F Spec (as shown in Figure 6). This accomplishes exactly what one was trying to achieve with unique identifiers on the database.
  3. Use a QUALIFIED data structure (requires V5R1 or later) (as shown in Figure 6).

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

      * Supplier File (SUPPLIER)
                                            REF(FIELDREF)
                R SUPPLIERR
                  SUPPCODE  R
                  NAME      R
 
      * Logical View of SUPPLIER file to rename NAME field
 
                R SUPPLIERM                 PFILE(SUPPLIER)
                  SUPPCODE
                  NEWNAME                   RENAME(NAME)



Figure 5: Renaming a field in a logical view.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

     FSupplier  IF   E           K Disk    Prefix(PT)                                               
                         
     D Record        E DS                  ExtName(Supplier)                                        
                         
            Chain Key  Supplier Record;                   



Figure 6: Renaming a field in a logical view.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

I was always taught that my design should handle what normally happens and that I should code for the exception. In RPG a clash of field names should be the exception and not the norm. By having unique identifiers on our database field names, we are treating these exceptions as if they are the norm, and that in turn forces us to have extra code in nearly all our programs to handle the unique naming.

---------------------------
About the author: Paul Tuohy is CEO of ComCon, an iSeries consulting company. He is the author of Re-Engineering RPG Legacy Applications and is one of the quoted industry experts in the IBM Redbook "Who Knew You Could Do That With RPG IV?"

==================================
MORE INFORMATION
==================================

  • Display duplicate fields command
    Search400.com member Martin Cubitt provides a tool that will compare two physical files and report common fields. This is useful, he says, when a program uses two files and you need to check which fields could potentially be overwritten in the program.
  • Processing database records
    A user asks, "If I have a database, how can I select the records I want to process based on the value in a given field in the table? I have an RPG program that processes all records in the table correctly, but I want to limit the records processed by the company field." Site expert Jim Mason helps out.
  • DB2/400 field level security
    A user asks, "I'm currently running V4R5 on a model 720. I'm using Crystal Report Writer and want to apply security in DB2/400 at the field level. For instance, I want departments to have access to their own payroll data, but no one else's. Is this possible?" Yes, says security expert Paul Jury, there are a couple ways.


Rate this Tip
To rate tips, you must be a member of Search400.com.
Register now to start rating these tips. Log in if you are already a member.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
iSeries CL programming
Taking advantage of CL advancements, starting with V5R3
Checking in on your IBM i authorization lists
Running PHP open source applications: NOBODY needs authority
Simplify the process of converting a spool file from iSeries into an Excel spreadsheet
CL program for daily backups
An automated CL method of moving a query from AS/400 to Excel
Changing user password expiration
Eight steps for creating program documentation using AS/400 utilities
DAYSPAST CLLE program for AS/400: Compares object creation date with today's date
Advanced Job Scheduler help

Application Development
iSeries calling an .exe
Top 10 programmer tips
Formatted work job scheduler
Convert system date and time
Mixing free format code with embedded SQL
SQL update a field in one file from a field in another file
Webcasts for iSeries programmers
Programming advice from the pros
Easy code copying via the drag and drop method
Setting FTP time-outs

DB2 UDB for iSeries
Expert advice on DB2
Make your database easy to read
Top 10 tips from our experts
DB2 expert Kent Milligan offers advice
Schedule stored procedures in DB2 UDB
Special features make creating tables simple
How to use DB2's cross-reference files to help manage your database -- Part II
How to use DB2's cross-reference files to help manage your database -- Part I
DB2/400 and DB2 UDB
'Twas the Night Before Christmas in IT

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



iSeries Security - Security Tools, Physical Security and System Security
HomeNewsTopicsITKnowledge ExchangeTipsBlogsAsk the ExpertsMultimediaWhite PapersProducts
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts