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

Difference between unique constraint and unique index?

P: n/a
aj
DB2 WSE 8.1 FP5
Red Hat AS 2.1

What is the difference between adding a unique constraint like:

ALTER TABLE <SCHEMA>.<TABLE> ADD CONSTRAINT CC1131378283225 UNIQUE (
<COL1>) ;

and adding a unique index like:

CREATE UNIQUE INDEX <SCHEMA>.<BLAH> ON <SCHEMA>.<TABLE> (<COL1> ASC)
PCTFREE 10 MINPCTUSED 10;

Just curious.

TIA

aj
Nov 12 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"aj" <ro****@mcdonalds.com> wrote in message
news:11*************@news.supernews.com...
DB2 WSE 8.1 FP5
Red Hat AS 2.1

What is the difference between adding a unique constraint like:

ALTER TABLE <SCHEMA>.<TABLE> ADD CONSTRAINT CC1131378283225 UNIQUE (
<COL1>) ;

and adding a unique index like:

CREATE UNIQUE INDEX <SCHEMA>.<BLAH> ON <SCHEMA>.<TABLE> (<COL1> ASC)
PCTFREE 10 MINPCTUSED 10;

Just curious.


They are two sides of the same coin, really. A unique constraint is a rule
in the database that this column needs to be kept unique (i.e. no duplicate
values in the column) while a unique index is the way that uniqueness is
enforced. Whenever you define a column as unique (or as a primary key), you
will be forced to create a unique index before you can use the table. DB2
then enforces the uniqueness in the column via the index.

Rhino
Nov 12 '05 #2

P: n/a
Ian
Rhino wrote:
"aj" <ro****@mcdonalds.com> wrote in message
news:11*************@news.supernews.com...
DB2 WSE 8.1 FP5
Red Hat AS 2.1

What is the difference between adding a unique constraint like:

ALTER TABLE <SCHEMA>.<TABLE> ADD CONSTRAINT CC1131378283225 UNIQUE (
<COL1>) ;

and adding a unique index like:

CREATE UNIQUE INDEX <SCHEMA>.<BLAH> ON <SCHEMA>.<TABLE> (<COL1> ASC)
PCTFREE 10 MINPCTUSED 10;

Just curious.


They are two sides of the same coin, really. A unique constraint is a rule
in the database that this column needs to be kept unique (i.e. no duplicate
values in the column) while a unique index is the way that uniqueness is
enforced. Whenever you define a column as unique (or as a primary key), you
will be forced to create a unique index before you can use the table. DB2
then enforces the uniqueness in the column via the index.


Actually you are not forced into doing anything. If you add a unique
constraint to a table, DB2 will automatically create a unique index if
one does not already exist. (The same way it will automatically create
an index if you alter a table to add a primary key).
Technically, there is no logical difference between the two options
given by the OP. However, there are physical differences:

#1 causes DB2 to automatically create an index (named
<SCHEMA>.<SQL....>) to enforce the constraint

#2 will not show that the table has a unique constraint, even though
the index exists and enforces uniqueness.


Nov 12 '05 #3

P: n/a

aj wrote:
DB2 WSE 8.1 FP5
Red Hat AS 2.1

What is the difference between adding a unique constraint like:

ALTER TABLE <SCHEMA>.<TABLE> ADD CONSTRAINT CC1131378283225 UNIQUE (
<COL1>) ;

and adding a unique index like:

CREATE UNIQUE INDEX <SCHEMA>.<BLAH> ON <SCHEMA>.<TABLE> (<COL1> ASC)
PCTFREE 10 MINPCTUSED 10;

Just curious.

TIA

aj


In addition to other replies, there's another difference between the
two.
DB2 allows a "unique index" to contain a single "null" value. So DB2
lets you create a "unique index" on a nullable column.

However, in contrast, if you have a column that has a "unique
constraint" then DB2 forces that column to be "not-null". Db2 requires
any column in either a primary-key or a unique-key to be mandatory (not
null).

Nov 12 '05 #4

P: n/a
aj wrote:
DB2 WSE 8.1 FP5
Red Hat AS 2.1

What is the difference between adding a unique constraint like:


A unique index is a physical thing whereas a unique constraint is a data
modeling construct. As was already stated, unique constraint are
implemented by adding a unique index (and additionally requiring the NOT
NULL condition).

But you should also be aware that referential integrity (foreign key
constraints) can only reference unique constraints (or primary keys as a
special case for unique constraints). A foreign key _cannot_ reference a
unique index because it is just that: an index and not a constraint.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 12 '05 #5

P: n/a

"Ian" <ia*****@mobileaudio.com> wrote in message
news:43**********@newsfeed.slurp.net...
Rhino wrote:
"aj" <ro****@mcdonalds.com> wrote in message
news:11*************@news.supernews.com...
DB2 WSE 8.1 FP5
Red Hat AS 2.1

What is the difference between adding a unique constraint like:

ALTER TABLE <SCHEMA>.<TABLE> ADD CONSTRAINT CC1131378283225 UNIQUE (
<COL1>) ;

and adding a unique index like:

CREATE UNIQUE INDEX <SCHEMA>.<BLAH> ON <SCHEMA>.<TABLE> (<COL1> ASC)
PCTFREE 10 MINPCTUSED 10;

Just curious.

They are two sides of the same coin, really. A unique constraint is a rule in the database that this column needs to be kept unique (i.e. no duplicate values in the column) while a unique index is the way that uniqueness is
enforced. Whenever you define a column as unique (or as a primary key), you will be forced to create a unique index before you can use the table. DB2 then enforces the uniqueness in the column via the index.


Actually you are not forced into doing anything. If you add a unique
constraint to a table, DB2 will automatically create a unique index if
one does not already exist. (The same way it will automatically create
an index if you alter a table to add a primary key).

You're absolutely right for DB2 on Windows/Linux/Unix, which is what the
original poster is using.

On some platforms, like z/OS, DB2 doesn't automatically create indexes for
you so DB2 (on those platforms) refuses to let you use the data until you
manually create the indexes that support the unique constraint. That's why I
answered the way I did. But I shouldn't have muddied the waters by adding
that factor into the answer; the original poster asked specifically about
his platform and you are right: the indexes get created automatically on DB2
running on Linux platforms.
Technically, there is no logical difference between the two options
given by the OP. However, there are physical differences:

#1 causes DB2 to automatically create an index (named
<SCHEMA>.<SQL....>) to enforce the constraint

#2 will not show that the table has a unique constraint, even though
the index exists and enforces uniqueness.

Rhino
Nov 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.