Saturday, February 6, 2010

UNIQUE WHERE NOT NULL - Explained

I always had confusion over the meaning of indexes created with UNIQUE WHERE NOT NULL clause on DB2 for z/OS. The manuals don't seem to do a good job explaining it. I understand it now and here is how it works.

It means its ok to have multiple rows with null values in columns included in the index, but its not ok to have rows where the combination of the columns included in the index is not unique and all the columns are not null. I tested this on a three column index. I was able to insert rows rows shown below just fine, the only duplicate exception I got was where column a, b, c were the same value for two different rows and none of the 3 columns contained a null value.



COL1 COL2 COL3
---- ---- ----
A B C
A B -
A B -
A - -
A - -
- - -
- - -

CREATE UNIQUE WHERE NOT NULL INDEX PRE.IDX1
ON PRE.TEST
(COL1 ASC
,COL2 ASC
,COL3 ASC)
USING STOGROUP ASTERISK
PRIQTY 12
SECQTY 10800
ERASE NO
FREEPAGE 0
PCTFREE 0
BUFFERPOOL BP1
CLOSE YES
PIECESIZE 2097152 K;

CREATE TABLE PRE.TEST
(COL1 CHARACTER(1) FOR SBCS DATA WITH DEFAULT NULL
,COL2 CHARACTER(1) FOR SBCS DATA WITH DEFAULT NULL
,COL3 CHARACTER(1) FOR SBCS DATA WITH DEFAULT NULL
)
IN DB.TS;

1 comment:

Unknown said...

Thank you for sharing ur findings.
Garry