Sams Teach Yourself SQL in 24 Hours

Sams Teach Yourself SQL in 24 Hours

By Ron Plew and Ryan Stephens

Querying the System Catalog

The system catalog tables or views are queried as any other table or view in the database using SQL. A user can usually query the user-related tables, but may be denied access to various system tables that can be accessed only by privileged database user accounts, such as the database administrator.

You create a SQL query to retrieve data from the system catalog just as you create a query to access any other table in the database.

For example, the following query returns all rows of data from the Sybase table SYSTABLES:

SELECT * FROM SYSTABLES 
GO

The following section displays a few examples of querying system catalog tables and some of the information that you may stumble across.

Examples of System Catalog Queries

The following examples use Oracle's system catalog. Oracle is chosen for no particular reason other than that it is the implementation with which this book's authors are most familiar.

The following query lists all user accounts in the database:

   mysql_icon.gif
   input_icon.gif

   SELECT USERNAME

   FROM ALL_USERS;

   output_icon.gif
USERNAME
----------------
SYS
SYSTEM
RYAN
SCOTT
DEMO
RON
USER1
USER2
8 rows selected.

The following query lists all tables owned by a user:

   mysql_icon.gif
   input_icon.gif

   SELECT TABLE_NAME

   FROM USER_TABLES;

   output_icon.gif
TABLE_NAME
----------------
CANDY_TBL
CUSTOMER_TBL
EMPLOYEE_PAY_TBL
EMPLOYEE_TBL
PRODUCTS_TBL
ORDERS_TBL
6 rows selected.

The next query returns all the system privileges that have been granted to the database user BRANDON:

   mysql_icon.gif
   input_icon.gif

   SELECT GRANTEE, PRIVILEGE

   FROM SYS.DBA_SYS_PRIVS

   WHERE GRANTEE = 'BRANDON';

   output_icon.gif
GRANTEE                       PRIVILEGE
---------------------- --------------------
BRANDON                ALTER ANY TABLE
BRANDON                ALTER USER
BRANDON                CREATE USER
BRANDON                DROP ANY TABLE
BRANDON                SELECT ANY TABLE
BRANDON                UNLIMITED TABLESPACE
6 rows selected.

The following is an example from MS Access:

   mysql_icon.gif
   input_icon.gif

   SELECT NAME

   FROM MSYSOBJECTS

   WHERE NAME = 'MSYSOBJECTS'

   output_icon.gif
NAME
-----------
MSYSOBJECTS

Share ThisShare This

Informit Network