SQL Query to List Indexes in IBM Db2 | Generated by AI

Home PDF

In IBM Db2, you can query the system catalog views to find information about a table’s existing indexes. The primary catalog view for indexes is SYSCAT.INDEXES. This view contains details about all indexes in the database, including the table they belong to, the columns they index, and their properties (e.g., unique or non-unique).

Here’s how you can check the existing indexes for a specific table:

SQL Query to List Indexes

Use the following query to retrieve index information for a specific table:

SELECT 
    INDNAME, 
    TABSCHEMA, 
    TABNAME, 
    COLNAMES, 
    UNIQUERULE, 
    INDEXTYPE
FROM 
    SYSCAT.INDEXES
WHERE 
    TABNAME = 'YOUR_TABLE_NAME'
    AND TABSCHEMA = 'YOUR_SCHEMA_NAME';

Explanation of Columns

Example

To find the indexes for a table named EMPLOYEES in the schema MY_SCHEMA:

SELECT 
    INDNAME, 
    TABSCHEMA, 
    TABNAME, 
    COLNAMES, 
    UNIQUERULE, 
    INDEXTYPE
FROM 
    SYSCAT.INDEXES
WHERE 
    TABNAME = 'EMPLOYEES'
    AND TABSCHEMA = 'MY_SCHEMA';

Notes

  1. Case Sensitivity: Table and schema names in Db2 are typically uppercase in the catalog unless explicitly created with quotes (e.g., "employees"). So, use 'EMPLOYEES' instead of 'employees' unless you’re sure of the casing.
  2. Schema: If you don’t specify TABSCHEMA, you might get results for tables with the same name in different schemas. To find your schema, you can check your current schema with:
    SELECT CURRENT SCHEMA FROM SYSIBM.SYSDUMMY1;
    
  3. Permissions: You need appropriate privileges to query SYSCAT.INDEXES. Typically, SELECT access on the catalog views is granted to most users.

Alternative: Using DESCRIBE

For a quick check in some Db2 environments (e.g., Db2 LUW), you can use the DESCRIBE command to see indexes, though it’s less detailed:

DESCRIBE INDEXES FOR TABLE schema_name.table_name SHOW DETAIL;

Example

DESCRIBE INDEXES FOR TABLE MY_SCHEMA.EMPLOYEES SHOW DETAIL;

This command lists indexes but might not be available or as comprehensive in all Db2 editions (e.g., Db2 on z/OS).

Sample Output

For the query on SYSCAT.INDEXES, you might see something like:

INDNAME        TABSCHEMA  TABNAME    COLNAMES       UNIQUERULE  INDEXTYPE
-------------  ---------- ---------- -------------- ----------- ---------
EMP_ID_IDX     MY_SCHEMA  EMPLOYEES  +EMPLOYEE_ID   U           REG
EMP_NAME_IDX   MY_SCHEMA  EMPLOYEES  +LAST_NAME     D           REG

If You Want All Indexes in the Database

Omit the WHERE clause:

SELECT 
    INDNAME, 
    TABSCHEMA, 
    TABNAME, 
    COLNAMES, 
    UNIQUERULE, 
    INDEXTYPE
FROM 
    SYSCAT.INDEXES
ORDER BY 
    TABSCHEMA, TABNAME, INDNAME;

Let me know if you need help interpreting the results or refining the query!


In IBM Db2, you can create an index on a table using the CREATE INDEX statement. Indexes improve query performance by allowing faster data retrieval, especially for columns frequently used in WHERE, JOIN, or ORDER BY clauses. Below is the syntax and explanation for creating an index, along with examples.


Basic Syntax

CREATE [UNIQUE] INDEX index_name
ON table_name (column_name [ASC | DESC], ...)
[options];

Key Components


Examples

1. Create a Simple Index

To create an index on the employee_id column of the employees table:

CREATE INDEX idx_employee_id
ON employees (employee_id);

2. Create a Unique Index

To create a unique index on the email column (no duplicate emails allowed):

CREATE UNIQUE INDEX idx_email
ON employees (email);

3. Create a Composite Index

To create an index on multiple columns, such as last_name and first_name:

CREATE INDEX idx_name
ON employees (last_name, first_name);

4. Create an Index with Descending Order

To create an index on hire_date in descending order:

CREATE INDEX idx_hire_date
ON employees (hire_date DESC);

5. Create a Clustered Index (Optional)

A clustered index determines the physical order of data in the table (only one per table):

CREATE INDEX idx_employee_id_clust
ON employees (employee_id)
CLUSTER;

Additional Options


Key Notes

  1. Permissions: You need the INDEX privilege on the table or appropriate authority (e.g., DBADM).
  2. Existing Data: If creating a UNIQUE index, the table must not already contain duplicate values in the indexed column(s), or the command will fail.
  3. Naming: Index names must be unique within the schema.
  4. Performance: Indexes speed up reads but can slow down writes (INSERT, UPDATE, DELETE) due to maintenance overhead.
  5. Verification: After creating, check the index with:
    SELECT INDNAME, COLNAMES 
    FROM SYSCAT.INDEXES 
    WHERE TABNAME = 'YOUR_TABLE_NAME' 
    AND TABSCHEMA = 'YOUR_SCHEMA_NAME';
    

Example Workflow

Suppose you have an employees table and want to index department_id for faster queries:

CREATE INDEX idx_department_id
ON employees (department_id);

Then verify:

SELECT INDNAME, COLNAMES 
FROM SYSCAT.INDEXES 
WHERE TABNAME = 'EMPLOYEES' 
AND TABSCHEMA = 'MY_SCHEMA';

Output

INDNAME           COLNAMES
----------------  -----------
IDX_DEPARTMENT_ID +DEPARTMENT_ID

Let me know if you need help with a specific index or additional options!


Back 2025.04.09 Donate