RPG Academy: Database Modernization - Methodology, Part 10

Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

 

Last month, I promised to show another way to generate automatic unique keys for your records. Here it is, using DB2’s ROWID data type. Keep reading to find out how it works.

The Identity column and the Sequence object, covered in the previous TechTip, are not the only ways to generate unique keys. ROWID can also be used, and, as you’ll see in a moment, it’s easy to use, but it has its caveats.

ROWID

The ROWID data type is a value that uniquely identifies a row in a table. A column or host variable can have a ROWID data type. A ROWID column enables queries to be written that navigate directly to a row in the table. Each value in a ROWID column must be unique. The database manager maintains the values permanently, even across table reorganizations. When a row is inserted into the table, the database manager generates a value for the ROWID column, unless you specify one. If a value is specified, it must be a valid row ID value that was previously generated by the DB2 database engine. The internal representation of a ROWID value is transparent to the user. The value is never subject to CCSID conversion because it is considered to contain BIT data. ROWID columns contain values of the ROWID data type, which returns a 40-byte VARCHAR value that is not regularly ascending or descending.

Each table can have only one ROWID, and a row ID value can only be assigned to a column, parameter, or host variable with the same data type. For the value of the ROWID column, the column must be defined with the GENERATED BY DEFAULT or OVERRIDING SYSTEM VALUE option. A unique constraint is implicitly added to every table that has a ROWID column, which guarantees that every ROWID value is unique. In other words, a ROWID is similar to a “regular” column defined with the UNIQUE constraint or one of the code-based IDs you’re currently using in your programs. The difference is that this one, like the previous two I presented, is managed automatically by the database engine.

There’s another important difference: a ROWID operand cannot be directly compared to any data type. To compare the bit representation of a ROWID in SQL, you must first cast the ROWID to a character string. In RPG, there is no data type that directly matches with the ROWID data type. It’s possible to circumvent this issue by using the keyword SQLTYPE in the Definition specifications. This allows you to define host variables capable of holding the ROWID value. Here’s how you need to define these variables:

DCL-S MyRowID SQLTYPE(ROWID)

These are just a few of the interesting and fairly advanced SQL features you can take advantage of. I encourage you to explore DB2’s treasure trove and figure out which of its gems can help you improve your database even further.

Subseries Wrap-Up

This subseries provided an overview of the database modernization process, starting with some database theory, which included a discussion of the following topics:

  • Conceptual, logical, and physical models
  • Database normalization
  • Entity Relationship Diagrams

Then I moved on to some of the tools that might help during the Modernization process:

  • IBM Data Studio
  • IBM InfoSphere Data Architect
  • Adsero Optima Foundation

These two topics set the stage for the real subject of this subseries: the database modernization process. I presented an overview of the three database modernization steps process that will allow you to improve database performance, reduce the number of database-related lines of code in your programs, and increase security:

The database modernization process is complex. This subseries provided an overview; it’s not a complete guide on how to proceed. Whole books have been written about it. Here’s a list of those I consider most useful:

The RPG Academy Series will continue, discussing UI modernization, and it’ll use some information from these books. You don’t need to read them from top to bottom, but I can tell you from experience that the Redbooks, in particular, make quite an interesting read!

Rafael Victoria-Pereira

Rafael Victória-Pereira has more than 20 years of IBM i experience as a programmer, analyst, and manager. Over that period, he has been an active voice in the IBM i community, encouraging and helping programmers transition to ILE and free-format RPG. Rafael has written more than 100 technical articles about topics ranging from interfaces (the topic for his first book, Flexible Input, Dazzling Output with IBM i) to modern RPG and SQL in his popular RPG Academy and SQL 101 series on mcpressonline.com and in his books Evolve Your RPG Coding and SQL for IBM i: A Database Modernization Guide. Rafael writes in an easy-to-read, practical style that is highly popular with his audience of IBM technology professionals.

Rafael is the Deputy IT Director - Infrastructures and Services at the Luis Simões Group in Portugal. His areas of expertise include programming in the IBM i native languages (RPG, CL, and DB2 SQL) and in "modern" programming languages, such as Java, C#, and Python, as well as project management and consultancy.


MC Press books written by Rafael Victória-Pereira available now on the MC Press Bookstore.

Evolve Your RPG Coding: Move from OPM to ILE...and Beyond Evolve Your RPG Coding: Move from OPM to ILE...and Beyond
Transition to modern RPG programming with this step-by-step guide through ILE and free-format RPG, SQL, and modernization techniques.
List Price $79.95

Now On Sale

Flexible Input, Dazzling Output with IBM i Flexible Input, Dazzling Output with IBM i
Uncover easier, more flexible ways to get data into your system, plus some methods for exporting and presenting the vital business data it contains.
List Price $79.95

Now On Sale

SQL for IBM i: A Database Modernization Guide SQL for IBM i: A Database Modernization Guide
Learn how to use SQL’s capabilities to modernize and enhance your IBM i database.
List Price $79.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: