Know Your Database: One Query to Inventory Every Table

Here is a question most shops cannot answer on the spot: how many tables and files are in your production library, and what does each one do?

Your database already knows. Ask it:

SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE,
       COLUMN_COUNT, TABLE_TEXT
  FROM QSYS2.SYSTABLES
 WHERE TABLE_SCHEMA = 'YOURLIB'
 ORDER BY TABLE_NAME;

This returns every table and file in the library, its type, its column count, and its description. You can always add the column LAST_ALTERED_TIMESTAMP and sort by it using ORDER BY so that you see the date and time the file was last changed. If you want to pretty this up, you can also add the ability to just show the first 10 characters of the Table_SCHEMA (the library name in IBM i language).

Run it and a few things will stand out:

  • The files with a blank TABLE_TEXT. Those are the ones nobody ever documented, and they are often the oldest and most important.
  • The logical files in this library; remember, this doesn’t show dependency.
  • Objects you forgot were still there at all.

This is the heart of what I keep telling people lately, and what my Using IBM i Services and SQL to Manage Your IBM i session is built around. Your system will describe itself if you ask in SQL. The catalog is right there, queryable, and most admins have never looked.

Run it against your main library this week. Knowing what lives in your database is the first step to documenting it, cleaning it up, or handing it to the next person with confidence.

Get new posts by email

Practical IBM i tips on SQL services, security and backup, sent when I publish. No spam, unsubscribe anytime.

Leave a Reply