A unique index can contain NULL values?

Posted on 2005-05-02
Last Modified: 2006-11-18
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.
Question by:minjiezen
  • 3
  • 2
LVL 13

Accepted Solution

ghp7000 earned 50 total points
ID: 13912131
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
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.

Author Comment

ID: 13912552
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.

Author Comment

ID: 13912749
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.
LVL 13

Expert Comment

ID: 13913424
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

Author Comment

ID: 13917673
Many thanks!

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how can I create a record layout for this file? 14 448
AS400 DB2 to MS SQL Server 2012 4 1,375
Check file/object size on AS400 3 166
Importing Android SQlite database data to db2 4 76
Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now