|
| 1 | +--- |
| 2 | +title: UNIQUERULE Values in IBM Db2 |
| 3 | +lang: en |
| 4 | +layout: post |
| 5 | +audio: false |
| 6 | +translated: false |
| 7 | +generated: true |
| 8 | +--- |
| 9 | + |
| 10 | +In the IBM Db2 system catalog view `SYSCAT.INDEXES`, the `UNIQUERULE` column indicates the uniqueness property of an index. The possible values for `UNIQUERULE` are: |
| 11 | + |
| 12 | +- **`D`**: Duplicates are allowed (non-unique index). |
| 13 | +- **`P`**: Primary key index (unique, associated with the table's primary key constraint). |
| 14 | +- **`U`**: Unique index (enforces uniqueness but is not a primary key). |
| 15 | + |
| 16 | +You mentioned "D, P, D" in your question, which seems like a typo or a misinterpretation of the output. I assume you meant to ask about the values `D`, `P`, and possibly `U` appearing in the results of your query. Let me clarify what each value represents: |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +### `UNIQUERULE` Values Explained |
| 21 | +1. **`D` (Duplicates Allowed)**: |
| 22 | + - The index does not enforce uniqueness. |
| 23 | + - Multiple rows can have the same value(s) in the indexed column(s). |
| 24 | + - Example: A regular index created without the `UNIQUE` keyword. |
| 25 | + - Use case: Speeding up queries without restricting data. |
| 26 | + |
| 27 | + **Example**: |
| 28 | + ```sql |
| 29 | + CREATE INDEX idx_dept ON employees (department_id); |
| 30 | + ``` |
| 31 | + If `department_id` can have duplicates, `UNIQUERULE` will be `D`. |
| 32 | + |
| 33 | +2. **`P` (Primary Key)**: |
| 34 | + - The index enforces uniqueness and is tied to the table's primary key constraint. |
| 35 | + - Automatically created when you define a primary key on a table. |
| 36 | + - Only one primary key index can exist per table. |
| 37 | + - Use case: Ensuring a unique identifier for each row. |
| 38 | + |
| 39 | + **Example**: |
| 40 | + ```sql |
| 41 | + CREATE TABLE employees ( |
| 42 | + employee_id INT NOT NULL, |
| 43 | + PRIMARY KEY (employee_id) |
| 44 | + ); |
| 45 | + ``` |
| 46 | + The index backing the primary key will have `UNIQUERULE = 'P'`. |
| 47 | + |
| 48 | +3. **`U` (Unique Index)**: |
| 49 | + - The index enforces uniqueness but is not a primary key. |
| 50 | + - Created explicitly with the `CREATE UNIQUE INDEX` statement. |
| 51 | + - Use case: Enforcing uniqueness on columns other than the primary key (e.g., email addresses). |
| 52 | + |
| 53 | + **Example**: |
| 54 | + ```sql |
| 55 | + CREATE UNIQUE INDEX idx_email ON employees (email); |
| 56 | + ``` |
| 57 | + Here, `UNIQUERULE` will be `U`. |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +### Sample Output Interpretation |
| 62 | +Running your query: |
| 63 | +```sql |
| 64 | +SELECT |
| 65 | + INDNAME, |
| 66 | + TABSCHEMA, |
| 67 | + TABNAME, |
| 68 | + COLNAMES, |
| 69 | + UNIQUERULE, |
| 70 | + INDEXTYPE |
| 71 | +FROM |
| 72 | + SYSCAT.INDEXES |
| 73 | +WHERE |
| 74 | + TABNAME = 'EMPLOYEES' |
| 75 | + AND TABSCHEMA = 'MY_SCHEMA'; |
| 76 | +``` |
| 77 | + |
| 78 | +You might see: |
| 79 | +``` |
| 80 | +INDNAME TABSCHEMA TABNAME COLNAMES UNIQUERULE INDEXTYPE |
| 81 | +------------- ---------- ---------- -------------- ----------- --------- |
| 82 | +PK_EMPLOYEES MY_SCHEMA EMPLOYEES +EMPLOYEE_ID P REG |
| 83 | +IDX_EMAIL MY_SCHEMA EMPLOYEES +EMAIL U REG |
| 84 | +IDX_DEPT MY_SCHEMA EMPLOYEES +DEPARTMENT_ID D REG |
| 85 | +``` |
| 86 | + |
| 87 | +- **`PK_EMPLOYEES`**: `UNIQUERULE = 'P'` → Primary key index on `employee_id`. |
| 88 | +- **`IDX_EMAIL`**: `UNIQUERULE = 'U'` → Unique index on `email`. |
| 89 | +- **`IDX_DEPT`**: `UNIQUERULE = 'D'` → Non-unique index on `department_id`. |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +### Why No Repeated "D, P, D"? |
| 94 | +The values `D`, `P`, and `U` appear individually for each index in the result set, not as a sequence like "D, P, D." Each row represents one index, and `UNIQUERULE` will be one of these values based on how the index was defined. If you saw something like "D, P, D" in your output, it might be a misunderstanding of multiple rows or a formatting issue in how the results were displayed. |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +### Quick Reference |
| 99 | +| UNIQUERULE | Meaning | Created By | |
| 100 | +|------------|---------------------|--------------------------------| |
| 101 | +| D | Duplicates allowed | `CREATE INDEX` (non-unique) | |
| 102 | +| P | Primary key | `PRIMARY KEY` constraint | |
| 103 | +| U | Unique index | `CREATE UNIQUE INDEX` | |
| 104 | + |
| 105 | +Let me know if you have a specific output you’d like me to interpret or if you need further clarification! |
0 commit comments