Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Almost everything that is known about a database can be found in a metadata repository called the system catalog. Metadata is information about the data in a database. It is maintained separately from the data itself. The system catalog describes the logical and physical structure of the data. The DB2 UDB system catalog (or simply "catalog") consists of a number of tables and views that are maintained by the database manager. A set of catalog tables and views is created whenever a database is created. The catalog describes the database objects, such as tables, columns, and indexes, and contains information about the types of access that users have to these objects. The catalog tables grow as database objects and privileges are added to the database. When an object is created, altered, or dropped, the database manager inserts, updates, or deletes rows in the catalog tables that describe the object and its relationships to other objects. Of course, you cannot explicitly create or drop these system catalog tables, but you can query and view their content. There is a wealth of useful information tucked away in these views, information that can help you to understand how your database operates. In this article, I will show you, by way of working examples, how to retrieve useful information from the DB2 UDB system catalog. Back to top
In this case, all five table columns appear in the view, but some of their names are different. This is done for consistency and clarity only.
An example of a more complex view that is based on more than one table is SYSCAT.STATEMENTS. This view, which contains information about each SQL statement in each package in the database, is based on the SYSIBM.SYSPLAN and SYSIBM.SYSSTMT catalog tables. Here is the view definition:
Back to top
To construct meaningful queries against the catalog views, you will need to know about the columns in the available views, as well as the names and purpose of the views themselves. The DB2 UDB system catalog views are described in the IBM DB2 Universal Database SQL Reference, Volume 1 or, if you prefer the information online, in the DB2 Information Center (follow Reference SQL Catalog views DB2 Universal Database). You can also examine the catalog views in the DB2 Control Center (see Figure 1).
Figure 1. The views that are associated with a particular database appear in the contents pane of the DB2 Control Center with Views selected in the object tree. The list has been filtered on schema SYSCAT.
The Open View window lets you examine the view contents in detail, and is a convenient way to browse the view columns (Figure 2).
Figure 2. The Open View window provides a convenient way to see a view's data.
If you're interested, you can even access the view definition itself, by selecting Alter from the pop-up menu (Figure 3).
Figure 3. The Alter View window lets you see the view definition.
union select distinct substr(grantee,1,16) as grantee, granteetype, 'Routine' from syscat.routineauth union select distinct substr(grantee,1,16) as grantee, granteetype, 'Server' from syscat.passthruauth order by grantee, granteetype GRANTEE GRANTEETYPE 3 ---------------- ----------- ----------MELNYK U Database MELNYK U Index MELNYK U Package MELNYK U Table MELNYK U Table space PUBLIC G Database PUBLIC G Package PUBLIC G Routine PUBLIC G Schema PUBLIC G Table PUBLIC G Table space 11 record(s) selected.
There are many other queries that you can construct to retrieve authorization data. Here are two more examples:
and b.btype = 'T' and b.viewname = 'COLUMNS' and b.viewschema = 'SYSCAT' connect reset TABSCHEMA TABNAME TYPE TBSPACE
---------------- ---------------- ---- ---------------SYSIBM SYSCHECKS T SYSCATSPACE SYSIBM SYSCOLCHECKS T SYSCATSPACE SYSIBM SYSCOLPROPERTIES T SYSCATSPACE SYSIBM SYSCOLUMNS T SYSCATSPACE 4 record(s) selected.
This query is essentially the same as the underlying query generated by the DB2 Control Center when you select the Show Related action against the SYSCAT.COLUMNS view (Figures 4 and 5).
Figure 4. The Show Related notebook lets you see the first-level dependency relationships between the view and other objects. In this case, we see a list of the catalog tables on which the SYSCAT.COLUMNS view depends.
Figure 5. The Show SQL window gives you the underlying query for the list of dependencies shown in Figure 4.
where n.dbpgname = d.dbpgname connect reset DBPGNAME DEFINER PMAP_ID CREATE_TIME DBPARTITIONNUM IN_USE ------------------ -------... ------- -------------------------- -------------- -----IBMCATGROUP SYSIBM 0 2004-10-18-08.27.54.045000 0Y IBMDEFAULTGROUP SYSIBM 1 2004-10-18-08.27.54.125000 0Y 2 record(s) selected.
Back to top
Figure 6. The View Privileges notebook lets you see or change the privileges that are held on the view.
Consider a view that includes the name of every table on which a user's authorization ID has been explicitly granted the SELECT privilege. The code in Listing 11 creates such a view, named MYSELECTS. This view is based on another view, the SYSCAT.TABAUTH catalog view. We are specifying a grantee type of U (for user; the alternative is G for group) and a grantee value of USER (referring to the special register that specifies the run-time authorization ID). We are also filtering on rows in which a SELECT privilege flag has been set to YES. Once the MYSELECTS view has been created, we can construct a query that retrieves data from both this view and the SYSCAT.TABLES view. This kind of query, which retrieves data from two or more tables simultaneously, is known as a join. Our join retrieves table schema and name values from the MYSELECTS view, and corresponding table types (such as V for view) from the SYSCAT.TABLES view. In this case, the query returns one row, corresponding to the view we have just created; this is the only table or view on which the authorization ID MELNYK has been explicitly granted the SELECT privilege. Finally, we can make the MYSELECTS view available to every authorization ID, and revoke the SELECT privilege on the base view (SYSCAT.TABAUTH) from PUBLIC. A simple query against the base view verifies that both user MELNYK and PUBLIC hold the SELECT privilege on the MYSELECTS view; MELNYK, because that ID is the view creator, and PUBLIC, because the SELECT privilege was granted to PUBLIC explicitly.
Listing 11. Creating and using a view that is based on the SYSCAT.TABAUTH system catalog view
create view myselects as select tabschema, tabname from syscat.tabauth where granteetype = 'U' and grantee = user and selectauth = 'Y' select m.tabschema, m.tabname, t.type from myselects m, syscat.tables t where m.tabschema = t.tabschema and m.tabname = t.tabname TABSCHEMA TABNAME TYPE
grant select on table myselects to public revoke select on table syscat.tabauth from public select tabschema, tabname, selectauth, grantee from syscat.tabauth where tabname = 'MYSELECTS' TABSCHEMA TABNAME SELECTAUTH GRANTEE ------------... ----------... ---------- -------... MELNYK MYSELECTS Y MELNYK MELNYK MYSELECTS Y PUBLIC 2 record(s) selected.
Back to top
Summary
The DB2 UDB system catalog is a collection of tables that describe database objects. DB2 UDB provides a large number of views that you can query for information that will help you to understand the workings of your database. You can build individual queries or scripts to do this, or you can easily access the views through the DB2 Control Center. This article gives you a variety of examples of useful queries, but we have only scratched the surface. I encourage you to explore the catalog to further your understanding of what is available and how to retrieve the information that you need.
Resources
DB2 Technical Support is the ideal place to locate resources such as the Version 8 Information Center and PDF product manuals.