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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| |||
| "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). |
| |||
| "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. |
| |||
| > 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? |
| |||
| "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. |
| |||
| 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. |
| |||
| "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. |
| |||
| 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. |
| |||
| "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. |
| ||||
| 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 :-). |
| Thread Tools | |
| Display Modes | |
|
|