Solved

A unique index can contain NULL values?

Posted on 2005-05-02
5
562 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
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
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
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.
0
 

Author Comment

by:minjiezen
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.
0
 
LVL 13

Expert Comment

by:ghp7000
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
0
 

Author Comment

by:minjiezen
ID: 13917673
Many thanks!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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 (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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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