A unique index can contain NULL values?

Hello,
I was reading IBM DB2 UDB documentation "Administration Guide: Planning" (Version 8) about unique keys and unique indexes, but I am confused now by the sentence "A unique index can contain NULL values" (see the last senetence in the following paragraphs). In the 2nd sentence of the 1st paragraph it states that "The columns of a unique key cannot contain NULL values", and then in the 1st sentence of the 2nd paragraph it states "The mechanism used to enforce the uniqueness of the key is called a unique index", so how come "A unique index can contain NULL values"?  (the paragraphs are found on page 54 of the doc, or go to page 68 from Acrobat Reader).
-----------------
A unique key is a key that is constrained so that no two of its values are equal.
The columns of a unique key cannot contain NULL values. For example, an
employee number column can be defined as a unique key, because each value
in the column identifies only one employee. No two employees can have the
same employee number.
The mechanism used to enforce the uniqueness of the key is called a unique
index. The unique index of a table is a column, or an ordered collection of
columns, for which each value identifies (functionally determines) a unique
row. A unique index can contain NULL values.
------------------
Thanks for any clarification.
minjiezenAsked:
Who is Participating?
 
ghp7000Connect With a Mentor Commented:
always test out concepts you dont understand, it is the best way to learn and you can save your points for more important issues

create table test (col1 char(4), col2 char(4))
insert into test values (null, '1')
insert into test values ('1', null)
insert into test values (null, '2')
insert into test values ('2', null)
now try:
insert into test values (null, '1')
now you will get duplicate key violation
why?
the columns have NOT been defined as NOT NULL, therefore they can accept null values
the unique index is looking for duplicate rows containing the same index keys , null, '1' is a violation of this constraint second time around, you could insert null, '3', null '4' without problem

if you had a primary key defined, that is different, the pk column can only be defined as NOT NULL, therefore you could never enter a NULL value.
0
 
minjiezenAuthor Commented:
Hi ghp7000, thanks for your fast response. I took your advice and did a test for the scenario you have created, and found that if I create a unique key for both col1 and col2, then the first 4 inserts succeeded. If I create a unique key either on col1 or col2, the 3rd or the 4th insert will error out. I was only thinking about the latter situation before (i.e., I did not think about a unique key on two or more columns). Am I understanding it correctly? Thanks.
0
 
minjiezenAuthor Commented:
Sorry, my mistake. Even if I create a unique index on either col1 or col2, I can still add NULL. Please ignore my previous message.
Thanks again.
0
 
ghp7000Commented:
sorry, i left out an important statement in my first post
create index test_uni on test (col1,col2)
if you try the 5th insert, it will fail.
If you define the index only as:
create index test_uni on test (col1)
only 1 row can have value=NULL for col1
you cannot do null,1, null 2   null 3, null 4 etc
0
 
minjiezenAuthor Commented:
Many thanks!
0
All Courses

From novice to tech pro — start learning today.