UNIQUEKEY and NONUNIQUEKEY are DEFINE ALTERNATEINDEX parameters that specify whether the alternate key can have duplicate values across base cluster records. You must specify exactly one when defining an alternate index. UNIQUEKEY means every alternate key value must be unique—no two base records can have the same value in the alternate key field. NONUNIQUEKEY means duplicate alternate key values are allowed; many base records can share the same alternate key (e.g. the same department code or status). Your choice affects how BLDINDEX builds the AIX, how many records can be returned when you read by alternate key, and what happens if base data has duplicate alternate key values when you expected unique. This page explains UNIQUEKEY and NONUNIQUEKEY: what they do, when to use each, and how they differ from primary key uniqueness.
UNIQUEKEY and NONUNIQUEKEY are specified in the IDCAMS DEFINE ALTERNATEINDEX (or DEFINE AIX) command. They do not apply to the base cluster or to the primary key of a KSDS. The primary key of a KSDS is always unique; there is no keyword to change that. Only the alternate index has a choice: the alternate key can be unique (UNIQUEKEY) or non-unique (NONUNIQUEKEY). So when you define an AIX with KEYS(length offset) to identify the alternate key field in the base record, you must also say whether that field is unique or can repeat.
In DEFINE ALTERNATEINDEX you code one of:
12UNIQUEKEY /* every alternate key value must be unique */ NONUNIQUEKEY /* duplicate alternate key values allowed */
You cannot specify both. If you omit both, some systems may default to one or the other; to avoid ambiguity, always specify explicitly. The keyword is stored in the catalog and is used when the AIX is built (BLDINDEX) and when you access by alternate key (e.g. READ by key, READNEXT for duplicates).
| Aspect | UNIQUEKEY | NONUNIQUEKEY |
|---|---|---|
| Alternate key values | Every alternate key value must be unique across base records. | Duplicate alternate key values are allowed; many records can share the same alternate key. |
| When to use | When the alternate key field is a unique identifier (e.g. employee ID, order number). | When the alternate key is a category or group (e.g. department, status, region). |
| BLDINDEX / load | Duplicate values in base data cause BLDINDEX (or load) to fail or reject duplicates. | BLDINDEX builds one index entry per distinct key, with multiple pointers per key if needed. |
| Read by key | READ by alternate key returns at most one base record. | READ by alternate key can return multiple base records (e.g. READNEXT to get all with that key). |
When you run BLDINDEX to build the alternate index from the base cluster, the system scans the base records and extracts the alternate key value from each. With UNIQUEKEY, every key value must be different. If two or more base records have the same alternate key value, BLDINDEX may fail with a duplicate-key error or may reject the duplicate (behavior depends on implementation). So before building an AIX with UNIQUEKEY, ensure the base data actually has unique values in the alternate key field. With NONUNIQUEKEY, duplicates are expected. The AIX is built so that one index entry can point to multiple base records (e.g. a list of pointers or a structure that supports multiple RBA/RRN values per key). So BLDINDEX succeeds even when many records share the same alternate key.
When your program reads by alternate key (e.g. opens the path and does a READ or START followed by READ/READNEXT), the uniqueness attribute affects how many records you get. With UNIQUEKEY, a read by alternate key returns at most one record—the one (if any) with that key value. With NONUNIQUEKEY, a read by alternate key can return multiple records. You typically do a START at the key and then READNEXT (or equivalent) to retrieve all base records that have that alternate key value. So UNIQUEKEY gives a one-to-one mapping from alternate key to base record; NONUNIQUEKEY gives a one-to-many mapping.
Choose based on the meaning of the alternate key field in your data:
| Alternate key type | Choice |
|---|---|
| Employee ID, account number, order ID | UNIQUEKEY — one record per value. |
| Department code, region, status code | NONUNIQUEKEY — many records per value. |
| Customer number (if one record per customer) | UNIQUEKEY. |
| Product category, zip code | NONUNIQUEKEY. |
An alternate index on a unique field such as employee ID (when the base is keyed by something else, e.g. name):
12345678910DEFINE ALTERNATEINDEX ( - NAME(MY.APPL.EMPID.AIX) - RELATE(MY.APPL.EMPLOYEE.KSDS) - KEYS(8 0) - UNIQUEKEY - UPGRADE - RECORDSIZE(48 48) - CYLINDERS(1 1)) - DATA (NAME(MY.APPL.EMPID.AIX.DATA)) - INDEX (NAME(MY.APPL.EMPID.AIX.INDEX))
Here the alternate key is 8 bytes at offset 0 (e.g. employee ID). UNIQUEKEY means every employee ID must be unique in the base cluster. BLDINDEX will fail if two records have the same employee ID.
An alternate index on department code (many employees per department):
12345678910DEFINE ALTERNATEINDEX ( - NAME(MY.APPL.DEPT.AIX) - RELATE(MY.APPL.EMPLOYEE.KSDS) - KEYS(4 20) - NONUNIQUEKEY - UPGRADE - RECORDSIZE(44 44) - CYLINDERS(1 1)) - DATA (NAME(MY.APPL.DEPT.AIX.DATA)) - INDEX (NAME(MY.APPL.DEPT.AIX.INDEX))
The alternate key is 4 bytes at offset 20 (department code). NONUNIQUEKEY allows many records to have the same department code. When you read by department, you get the first record with that department and then READNEXT to get the rest.
The base cluster primary key (in a KSDS) is always unique. You do not specify UNIQUEKEY for the base; it is inherent. UNIQUEKEY and NONUNIQUEKEY apply only to alternate indexes. So you can have a KSDS with a unique primary key and one or more alternate indexes: some alternate keys might be unique (UNIQUEKEY) and others might allow duplicates (NONUNIQUEKEY). For example, the base might be keyed by transaction ID (unique); one AIX might be on customer ID (NONUNIQUEKEY—one customer has many transactions) and another AIX on transaction ID again (UNIQUEKEY—same as primary, alternate access path).
Imagine a phone book (base cluster) and an index by first name (alternate index). If you say UNIQUEKEY, you are saying every first name must be different—only one "John" allowed. If you say NONUNIQUEKEY, you are saying many people can have the same first name—many "John"s, and when you look up "John" you get a list of all of them. So UNIQUEKEY = one name per person; NONUNIQUEKEY = many people can share a name.
1. What does NONUNIQUEKEY mean for an alternate index?
2. When defining an alternate index, must you specify UNIQUEKEY or NONUNIQUEKEY?
3. For an AIX on "department code," which would you use?