473,562 Members | 3,023 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>.<BLA H> ON <SCHEMA>.<TABLE > (<COL1> ASC)
PCTFREE 10 MINPCTUSED 10;

Just curious.

TIA

aj
Nov 12 '05 #1
5 16695

"aj" <ro****@mcdonal ds.com> wrote in message
news:11******** *****@news.supe rnews.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>.<BLA H> 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****@mcdonal ds.com> wrote in message
news:11******** *****@news.supe rnews.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>.<BLA H> 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>.<BLA H> 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*****@mobile audio.com> wrote in message
news:43******** **@newsfeed.slu rp.net...
Rhino wrote:
"aj" <ro****@mcdonal ds.com> wrote in message
news:11******** *****@news.supe rnews.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>.<BLA H> 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
45385
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.) How can I achieve this? I suppose I would get the most-hated "table/view is changing, trigger/function may not see it" error if I tried to write a...
3
27457
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 actual sql syntax to create this index? Thanks, June.
5
10851
by: Kamil | last post by:
Hello What should I use for better perfomance since unique constraint always use index ? Thanks Kamil
4
2869
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 are: 1. Does a unique constraint interally use unique index? 2. If Yes to #1, I DO NOT need to create an index for search/sort purpose, right?
4
38543
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
9296
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? Regds Mallah.
3
22618
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 both columns allow nulls, I want col3 + col4 to be unique, if one or both the columns have values. If both columns have nulls, it should allow more...
10
26075
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 null references another, FK_LASTLYOID bigint not null references lastly, unique (FK_OTHEROID,FK_ANOTHEROID))
10
14656
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
0
7652
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7867
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8095
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7625
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7934
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6219
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5476
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
901
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.