Skip to content

Commit e74c9ef

Browse files
committed
docs(notes): add ibm-db2-indexes-en notes
1 parent 1e1b25b commit e74c9ef

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

Diff for: notes/2025-04-09-ibm-db2-indexes-en.md

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
title: SQL Query to List Indexes in IBM Db2
3+
lang: en
4+
layout: post
5+
audio: false
6+
translated: false
7+
generated: true
8+
---
9+
10+
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).
11+
12+
Here’s how you can check the existing indexes for a specific table:
13+
14+
### SQL Query to List Indexes
15+
Use the following query to retrieve index information for a specific table:
16+
17+
```sql
18+
SELECT
19+
INDNAME,
20+
TABSCHEMA,
21+
TABNAME,
22+
COLNAMES,
23+
UNIQUERULE,
24+
INDEXTYPE
25+
FROM
26+
SYSCAT.INDEXES
27+
WHERE
28+
TABNAME = 'YOUR_TABLE_NAME'
29+
AND TABSCHEMA = 'YOUR_SCHEMA_NAME';
30+
```
31+
32+
#### Explanation of Columns
33+
- **`INDNAME`**: The name of the index.
34+
- **`TABSCHEMA`**: The schema of the table.
35+
- **`TABNAME`**: The name of the table.
36+
- **`COLNAMES`**: The columns included in the index (prefixed with `+` for ascending or `-` for descending).
37+
- **`UNIQUERULE`**: Indicates if the index is unique (`U` for unique, `D` for duplicate-allowed, `P` for primary key).
38+
- **`INDEXTYPE`**: The type of index (e.g., `REG` for regular, `CLUS` for clustering).
39+
40+
#### Example
41+
To find the indexes for a table named `EMPLOYEES` in the schema `MY_SCHEMA`:
42+
```sql
43+
SELECT
44+
INDNAME,
45+
TABSCHEMA,
46+
TABNAME,
47+
COLNAMES,
48+
UNIQUERULE,
49+
INDEXTYPE
50+
FROM
51+
SYSCAT.INDEXES
52+
WHERE
53+
TABNAME = 'EMPLOYEES'
54+
AND TABSCHEMA = 'MY_SCHEMA';
55+
```
56+
57+
### Notes
58+
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.
59+
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:
60+
```sql
61+
SELECT CURRENT SCHEMA FROM SYSIBM.SYSDUMMY1;
62+
```
63+
3. **Permissions**: You need appropriate privileges to query `SYSCAT.INDEXES`. Typically, `SELECT` access on the catalog views is granted to most users.
64+
65+
### Alternative: Using `DESCRIBE`
66+
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:
67+
```sql
68+
DESCRIBE INDEXES FOR TABLE schema_name.table_name SHOW DETAIL;
69+
```
70+
71+
#### Example
72+
```sql
73+
DESCRIBE INDEXES FOR TABLE MY_SCHEMA.EMPLOYEES SHOW DETAIL;
74+
```
75+
76+
This command lists indexes but might not be available or as comprehensive in all Db2 editions (e.g., Db2 on z/OS).
77+
78+
### Sample Output
79+
For the query on `SYSCAT.INDEXES`, you might see something like:
80+
```
81+
INDNAME TABSCHEMA TABNAME COLNAMES UNIQUERULE INDEXTYPE
82+
------------- ---------- ---------- -------------- ----------- ---------
83+
EMP_ID_IDX MY_SCHEMA EMPLOYEES +EMPLOYEE_ID U REG
84+
EMP_NAME_IDX MY_SCHEMA EMPLOYEES +LAST_NAME D REG
85+
```
86+
- `EMP_ID_IDX` is a unique index on `EMPLOYEE_ID`.
87+
- `EMP_NAME_IDX` is a non-unique index on `LAST_NAME`.
88+
89+
### If You Want All Indexes in the Database
90+
Omit the `WHERE` clause:
91+
```sql
92+
SELECT
93+
INDNAME,
94+
TABSCHEMA,
95+
TABNAME,
96+
COLNAMES,
97+
UNIQUERULE,
98+
INDEXTYPE
99+
FROM
100+
SYSCAT.INDEXES
101+
ORDER BY
102+
TABSCHEMA, TABNAME, INDNAME;
103+
```
104+
105+
Let me know if you need help interpreting the results or refining the query!

0 commit comments

Comments
 (0)