By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,929 Members | 634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,929 IT Pros & Developers. It's quick & easy.

unique constraint and unique index

P: n/a
Hi there,

How to differentiate between unique constraint and unique index? These
are very similar but I cannot differentiate them?

Could someone give me a hand?
Thanks in advance

Jun 30 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"Laurence" <wo**********@hotmail.com> wrote in message
news:11*********************@75g2000cwc.googlegrou ps.com...
Hi there,

How to differentiate between unique constraint and unique index? These
are very similar but I cannot differentiate them?

Could someone give me a hand?
Thanks in advance


A unique constraint will automatically created a unique index, unless a
unique index already exists on the same columns.

A unique constraint must only contain columns that are defined as not null
(like a PK). A unique index can have a nullable column (but null values are
not ignored and they are counted in determining uniqueness).

A foreign key on a child table can reference a unique constraint on a parent
table (and obviously it can also reference a PK on a parent table).

Jun 30 '06 #2

P: n/a
"Mark A" <no****@nowhere.com> wrote in message
news:es******************************@comcast.com. ..
A unique constraint will automatically created a unique index, unless a
unique index already exists on the same columns.

A unique constraint must only contain columns that are defined as not null
(like a PK). A unique index can have a nullable column (but null values
are not ignored and they are counted in determining uniqueness).

A foreign key on a child table can reference a unique constraint on a
parent table (and obviously it can also reference a PK on a parent table).


Correction, first sentence should read:

A unique constraint will automatically create a unique index, unless a
unique index already exists on the same columns.


Jun 30 '06 #3

P: n/a
> A unique index can have a nullable column (but null values are
not ignored and they are counted in determining uniqueness).

So, does it mean the rows in the same column only can have one NULL
value? or otherwise the violation rule will be fired?

Jun 30 '06 #4

P: n/a
"Laurence" <wo**********@hotmail.com> wrote in message
news:11*********************@75g2000cwc.googlegrou ps.com...
A unique index can have a nullable column (but null values are

not ignored and they are counted in determining uniqueness).

So, does it mean the rows in the same column only can have one NULL
value? or otherwise the violation rule will be fired?


Yes. On DB2 for z/OS their is an option when creating the unique index so
that it will ignore nulls, but this feature is not available on DB2 for
Linux, UNIX, Windows.
Jun 30 '06 #5

P: n/a
Ian
Laurence wrote:
Hi there,

How to differentiate between unique constraint and unique index? These
are very similar but I cannot differentiate them?

Could someone give me a hand?


A unique constraint is a logical element in a data model.

DB2 uses a unique index as the physical implementation of that.

Jun 30 '06 #6

P: n/a
"Ian" <ia*****@mobileaudio.com> wrote in message
news:44**********@newsfeed.slurp.net...

A unique constraint is a logical element in a data model.

DB2 uses a unique index as the physical implementation of that.


A Unique Constraint is a physical element of DB2 DDL. It has some special
characteristics that differ from a unique index that have been noted earlier
in this thread.
Jun 30 '06 #7

P: n/a
Ian
Mark A wrote:
"Ian" <ia*****@mobileaudio.comwrote in message
news:44**********@newsfeed.slurp.net...
>A unique constraint is a logical element in a data model.

DB2 uses a unique index as the physical implementation of that.

A Unique Constraint is a physical element of DB2 DDL. It has some special
characteristics that differ from a unique index that have been noted earlier
in this thread.
A unique constraint <primary key. The OP asked what the difference
between a unique constraint and a unique index was, but nothing about
a primary key.

You can create a unique index on a table, but you there is no
requirement have to add a unique constraint to the table as well.
DB2 will continue to work fine, and will enforce the uniqueness just
the same.



Jul 3 '06 #8

P: n/a
"Ian" <ia*****@mobileaudio.comwrote in message
news:44********@newsfeed.slurp.net...
>
A unique constraint <primary key. The OP asked what the difference
between a unique constraint and a unique index was, but nothing about
a primary key.

You can create a unique index on a table, but you there is no
requirement have to add a unique constraint to the table as well.
DB2 will continue to work fine, and will enforce the uniqueness just
the same.
I never said that a unique constraint was the same as a primary key. But one
of the differences between a unique constraint and unique index is that a
foreign key on another table can refer to a unique constraint, even if it is
not the primary key.
Jul 3 '06 #9

P: n/a
Ian
Mark A wrote:
I never said that a unique constraint was the same as a primary key. But one
of the differences between a unique constraint and unique index is that a
foreign key on another table can refer to a unique constraint, even if it is
not the primary key.

Mark,

That is a true statement - you can't define a foreign key without a
constraint (unique or primary key).

Regardless, I still hold that constraints are purely logical entities.
There are no physical manifestations of them (other than some entries in
the system catalog).

Unique indexes are the _physical_ implementation that DB2 uses to
enforce uniqueness (because it's a lot more efficient than trying to
scan an entire table to determine if a value already exists :-).
Jul 3 '06 #10

P: n/a
DB2UDB allows to define a unique constraint when an index is not
defined as unique.

It means that there is then an internal attribute saying the index is
unique when a unique constraint is defined on it.

The result is: as long as the constraint exists, no duplicates will be
allowed.

When the constrained is dropped, duplicates are allowed again.

Example:

CREATE TABLE T1 ( C1 char(10) not null);

CREATE INDEX MYINDEX1 ON T1(C1);

ALTER TABLE T1 ADD CONSTRAINT CONSTRAINT1 PRIMARY KEY (C1);

When creating the constraint db2 will return SQL0598W:

"SQL0598W Existing index "<name>" is used as the index for the
primary key or a unique key.

Explanation:

An index was required for an ALTER TABLE operation that defined a
primary key or a unique key, and the indicated index matches the
required index.

When creating a primary key or unique key index, an index
description matches if it identifies the same set of columns (in
any order) as the primary or unique key without regard to
ascending or descending specifications, and is specified as
unique.

The statement is processed successfully.

"

It is not correct that an index description matches, specified as
unique.

DB2 UDB allows this anyway, and will enforce correctly the constraint
(at creation time and at insert time).

Looks to be a feature.
Bernard Dhooghe

Ian wrote:
Mark A wrote:
I never said that a unique constraint was the same as a primary key. But one
of the differences between a unique constraint and unique index is that a
foreign key on another table can refer to a unique constraint, even if it is
not the primary key.


Mark,

That is a true statement - you can't define a foreign key without a
constraint (unique or primary key).

Regardless, I still hold that constraints are purely logical entities.
There are no physical manifestations of them (other than some entries in
the system catalog).

Unique indexes are the _physical_ implementation that DB2 uses to
enforce uniqueness (because it's a lot more efficient than trying to
scan an entire table to determine if a value already exists :-).
Jul 6 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.