Solved

A unique index can contain NULL values?

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

Accepted Solution

by:
ghp7000 earned 50 total points
Comment Utility
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
 

Author Comment

by:minjiezen
Comment Utility
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
 

Author Comment

by:minjiezen
Comment Utility
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
 
LVL 13

Expert Comment

by:ghp7000
Comment Utility
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
 

Author Comment

by:minjiezen
Comment Utility
Many thanks!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

12 Experts available now in Live!

Get 1:1 Help Now