Metadata information about database instances is required for the smooth operation of any database system. The database system uses this information when serving user requests, in the form of either DML statements or calls from database utilities. For database administrators, the metadata helps in the tuning and optimization of a database instance. For database developers, this information helps in finding the logical and physical structure of database objects such as tables, views, columns, indexes, triggers, and many others. Most of the graphical database development tools available in the market use metadata information to display details about database objects. IBM's DB2 Development Center IDE is an example of such a tool.
This article explains how the IBM DB2 UDB Ver. 8.2 (DB2) database stores metadata information pertaining to an instance. It elaborates on how DB2 stores the metadata about database objects such as tables, views, indexes, and triggers. Further, it explains how database developers can use this metadata information to find out the logical and physical structure, as well as the state and validity, of database objects. Knowing how metadata is stored is very useful when you need the details of an object in the database but have limited or no access to graphical tools.
IBM DB2 database manager provides two types of catalog views for every database instance:
The following sections describe some of the catalog views available in DB2 under the SYSCAT schema, which stores the metadata about database objects.
Table 1 provides some of the columns available in this catalog view.
Column Name | Data Type | Description |
---|---|---|
TABSCHEMA | VARCHAR(128) | Stores the schema name on which the database object is defined |
TABNAME | VARCHAR(128) | Stores the name of the database object, such as table, view, nickname, or an alias |
TYPE | CHAR(1) | Identifies the database object as a table, view, alias, or a nickname (The type value 'T' means table; 'V' means view; 'N' means nickname; and 'A' means alias.) |
COLCOUNT | SMALLINT | Number of columns in the table or view |
KEYCOLUMNS | SMALLINT | Number of columns that constitute the primary key |
KEYINDEXID | SMALLINT | Index ID for the primary key |
KEYUNIQUE | SMALLINT | Number of unique constraints in the table or view |
(* This table is only a partial list of columns, which are of interest to this article. For the complete list, refer the IBM DB2 UDB Ver. 8.2 SQL Reference Volume 1.) |
Table 1. Columns Available in SYSCAT.TABLES Catalog View |
Column Name | Data Type | Description |
---|---|---|
VIEWSCHEMA | VARCHAR(128) | Schema name for the view |
VIEWNAME | VARCHAR(128) | Name of the view |
DEFINER | VARCHAR(128) | User who created the view |
VIEWCHECK | CHAR(1) | Type of view checking defined for this view: |
READONLY | CHAR(1) | Defines whether the view is read only or not: |
VALID | CHAR(1) | Determines the validity of the view: |
TEXT | CLOB(64K) | DDL text for view |
(* This table is only a partial list of columns, which are of interest to this article. For the complete list, refer the IBM DB2 UDB Ver. 8.2 SQL Reference Volume 1.) |
Table 2. Columns Available in SYSCAT.VIEWS Catalog View |
Column Name | Data Type | Description |
---|---|---|
TABSCHEMA | VARCHAR(128) | Stores the schema name of the table on which the column is defined |
TABNAME | VARCHAR(128) | Stores the name of the table on which the column is defined |
COLNAME | VARCHAR(128) | Name of the column |
COLNO | SMALLINT | Position of the column in the table |
TYPENAME | VARCHAR(18) | Data type of the column |
LENGTH | INTEGER | Size of the column |
DEFAULT | VARCHAR(254) | Default value for the column, if defined |
INDENTITY | CHAR(1) | 'Y' – indicates the column as an identity column 'N' – indicates the column as not an identity column |
HIDDEN | CHAR(1) | Type of the hidden column: |
(* This table is only a partial list of columns, which are of interest to this article. For the complete list, refer the IBM DB2 UDB Ver. 8.2 SQL Reference Volume 1.) |
Table 3. Columns Available in SYSCAT.COLUMNS Catalog View |
Column Name | Data Type | Description |
---|---|---|
INDSCHEMA | VARCHAR(128) | Name of the schema on which the index is defined |
INDNAME | VARCHAR(18) | Index name |
DEFINER | VARCHAR(128) | User who created the index |
TABSCHEMA | VARCHAR(128) | Stores the schema name of the table on which the index is defined |
TABNAME | VARCHAR(128) | Stores the name of the table for which index is defined |
COLNAMES | VARCHAR(640) | List of columns in the index |
UNIQUERULE | CHAR(1) | Determines whether the index is unique or not: |
INDEXTYPE | CHAR(4) |
(* This table is only a partial list of columns, which are of interest to this article. For the complete list, refer the IBM DB2 UDB Ver. 8.2 SQL Reference Volume 1.) |
Table 4. Columns Available in SYSCAT.INDEXES Catalog View |
Table 5 lists some of the columns available in this catalog view.
Column Name | Data Type | Description |
---|---|---|
INDSCHEMA | VARCHAR(128) | Name of the schema on which the index is defined |
INDNAME | VARCHAR(18) | Index name |
COLNAME | VARCHAR(128) | User who created the index |
COLSEQ | SMALLINT | Stores the schema name of the table on which the index is defined |
COLORDER | CHAR(1) | Order of value in the column: |
(* This table is only a partial list of columns, which are of interest to this article. For the complete list, refer the IBM DB2 UDB Ver. 8.2 SQL Reference Volume 1.) |
Table 5. Columns Available in SYSCAT.COLUSE Catalog View |
Table 6 lists some of the columns available in this catalog view.
Column Name | Data Type | Description |
---|---|---|
TRIGSCHEMA | VARCHAR(128) | Name of the schema on which the trigger is defined |
TRIGNAME | VARCHAR(18) | Trigger name |
DEFINER | VARCHAR(128) | User who created the index |
TABSCHEMA | VARCHAR(128) | Stores the schema name of the table for which the trigger is defined |
TABNAME | VARCHAR(128) | Name of table for which the trigger is defined |
TRIGTIME | CHAR(1) | |
TRIGEVENET | CHAR(1) | Event for which the trigger is defined: |
GRANULARITY | CHAR(1) | Determines whether the trigger is executed per statement or per row: |
TEXT | CLOB(64K) | Full text of the trigger statement |
(* This table is only a partial list of columns, which are of interest to this article. For the complete list, refer the IBM DB2 UDB Ver. 8.2 SQL Reference Volume 1.) |
Table 6. Columns Available in SYSCAT.TRIGGERS Catalog View |
Apart from the above list, other catalog views that carry useful information for database developers include the following:
SELECT TABSCHEMA, TABNAME, TYPE, COLCOUNT, KEYCOLUMNS, KEYINDEXID, KEYUNIQUE FROM SYSCAT.TABLES WHERE TABSCHEMA NOT LIKE 'SYS%' AND TYPE = 'T'
Having the type filter as 'T' will return only information about tables in the given database. You can also filter by other database objects like views, nicknames, and aliases by providing the corresponding type values.
SELECT TABSCHEMA, TABNAME, TYPE, COLCOUNT, KEYCOLUMNS, KEYINDEXID, KEYUNIQUE FROM SYSCAT.TABLES WHERE TABSCHEMA NOT LIKE 'SYS%' AND TYPE = 'T' AND TABNAME = 'TABLE_NAME'
Among the selected columns, COLCOUNT gives the number of columns in the tables; KEYCOLUMNS gives the number of columns that constitute the primary key; KEYINDEXID gives the index ID of the primary index; and KEYUNIQUE gives the number of unique constraints in a table.
SELECT TABSCHEMA, TABNAME, COLNO, COLNAME, TYPENAME, LENGTH, DEFAULT, IDENTITY, HIDDEN FROM SYSCAT.COLUMNS WHERE TABSCHEMA NOT LIKE 'SYS%' AND TABNAME = 'TABLE_NAME' ORDER BY COLNO
SELECT INDNAME, DEFINER, TABSCHEMA, TABNAME, COLNAMES, COLCOUNT, UNIQUERULE, INDEXTYPE FROM SYSCAT.INDEXES WHERE TABSCHEMA NOT LIKE 'SYS%' AND TABNAME = 'TABLE_NAME'
When you write queries to retrieve data from a table, use all the available indexes to improve performance. For that, you need to know all the columns in an index, the order in which they appear, and the sort order for each of the columns. To get all this information, you can use the SYSCAT.INDEXES and SYSCAT.INDEXCOLUSE catalog views as follows:
SELECT A.INDNAME, A.TABSCHEMA, A.TABNAME, B.COLNAME, B.COLSEQ, B.COLORDER FROM SYSCAT.INDEXES AS A, SYSCAT.INDEXCOLUSE AS B WHERE A.INDSCHEMA = B.INDSCHEMA AND A.INDNAME = B.INDNAME AND A.TABSCHEMA NOT LIKE 'SYS%' AND A.TABNAME = 'TABLE_NAME' ORDER BY A.INDNAME, B.COLSEQ
SELECT TRIGNAME, TABNAME, TRIGTIME, TRIGEVENT, GRANULARITY, VALID, CREATE_TIME, TEXT FROM SYSCAT.TRIGGERS WHERE TABSCHEMA NOT LIKE 'SYS%' AND TABNAME = 'TABLE_NAME' ORDER BY TABNAME
SELECT VIEWSCHEMA, VIEWNAME, VIEWCHECK, READONLY, VALID, TEXT FROM SYSCAT.VIEWS WHERE VIEWSCHEMA NOT LIKE 'SYS%' AND VIEWBAME = 'VIEW_NAME'
The preceding examples described how to retrieve and use metadata information about database objects such as tables, indexes, views, columns, and triggers.