473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Difference between unique constraint and unique index?

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
5 16658

"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
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

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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.)...
3
by: June Moore | last post by:
Hi, I would like to add a unique index that consists of two fields in a table. e.g. tbl_A (field1,field2) -- field1 & field2 Indexed and combination must be Unique. Can anyone tell me the...
5
by: Kamil | last post by:
Hello What should I use for better perfomance since unique constraint always use index ? Thanks Kamil
4
by: Q. John Chen | last post by:
All, What's the difference between a unique contraint and unique? sementically, if you want a column contain unique values, it is a contraint. And an index is for searching/sort. The questions...
4
by: Dave | last post by:
Can you create a unique constraint on multiple columns, or does it have to be implemented as a unique index? If possible can someone please post some sample code? Thanks,
1
by: Rajesh Kumar Mallah | last post by:
Hi , Looks like ADD UNIQUE( some_fuc( some_feild) ) is not supported with add constraint. the only way is to add the constriant is using UNIQUE INDEX . Is it a bug or intended behaviour? ...
3
by: Prince Kumar | last post by:
Is there any way I can define an Unique constraint or unique index which allows more than one null values for the same column combination in DB2? ie, If my index is defined on (col3, col4) where...
10
by: BuddhaBuddy | last post by:
Platform is DB2/NT 7.2.9 The table was created like this: CREATE TABLE MYTEST ( MYTESTOID bigint not null primary key, FK_OTHEROID bigint not null references other, FK_ANOTHEROID bigint not...
10
by: Laurence | last post by:
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
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.