Unix Technical Forum

unique constraint and unique index

This is a discussion on unique constraint and unique index within the DB2 forums, part of the Database Server Software category; --> Hi there, How to differentiate between unique constraint and unique index? These are very similar but I cannot differentiate ...


Go Back   Unix Technical Forum > Database Server Software > DB2

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008, 07:31 AM
Laurence
 
Posts: n/a
Default 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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 07:31 AM
Mark A
 
Posts: n/a
Default Re: unique constraint and unique index

"Laurence" <worldwide607@hotmail.com> wrote in message
news:1151650230.297672.49290@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).



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-27-2008, 07:31 AM
Mark A
 
Posts: n/a
Default Re: unique constraint and unique index

"Mark A" <nobody@nowhere.com> wrote in message
news:esednUU_j4gUUTnZnZ2dnUVZ_vidnZ2d@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.




Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-27-2008, 07:31 AM
Laurence
 
Posts: n/a
Default Re: unique constraint and unique index

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-27-2008, 07:31 AM
Mark A
 
Posts: n/a
Default Re: unique constraint and unique index

"Laurence" <worldwide607@hotmail.com> wrote in message
news:1151654236.770266.44450@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.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-27-2008, 07:31 AM
Ian
 
Posts: n/a
Default Re: unique constraint and unique index

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.



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-27-2008, 07:31 AM
Mark A
 
Posts: n/a
Default Re: unique constraint and unique index

"Ian" <ianbjor@mobileaudio.com> wrote in message
news:44a57d9d$1_4@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.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-27-2008, 07:32 AM
Ian
 
Posts: n/a
Default Re: unique constraint and unique index

Mark A wrote:
> "Ian" <ianbjor@mobileaudio.com> wrote in message
> news:44a57d9d$1_4@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.







Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-27-2008, 07:32 AM
Mark A
 
Posts: n/a
Default Re: unique constraint and unique index

"Ian" <ianbjor@mobileaudio.com> wrote in message
news:44a963b1_3@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.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 02-27-2008, 07:32 AM
Ian
 
Posts: n/a
Default Re: unique constraint and unique index

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 :-).


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:40 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com