472,328 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

unique constraint and unique index

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

Similar topics

3
by: KULJEET | last post by:
foreign key also refer to unique constraint. (GREAT...) 1.then table that containt unique constraint act as master table????? 2.IS unique...
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...
2
by: Mansoor Azam | last post by:
When I add a unique key constraint to column in SQL 6.5 why does it also create an index. e.g. In the table subaccounts I added a unique key...
5
by: Kamil | last post by:
Hello What should I use for better perfomance since unique constraint always use index ? Thanks Kamil
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...
3
by: Thomas LeBlanc | last post by:
Does a Unique Constraint build a unique index? What is the difference? Thanks, Thomas LeBlanc ...
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? ...
5
by: aj | last post by:
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...
1
by: D.Stone | last post by:
Hi, I'm getting hung up on a trivial schema change - I need to make a column accept only null or unique values and Books Online avers 'what you...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

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.