Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

A unique index can contain NULL values?

Posted on 2005-05-02
5
563 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

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 video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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