UNIQUE Constraints

UNIQUE Constraints

Post by Paul Bowma » Thu, 12 Apr 2001 20:19:53



Hi All

This is on SQLServer 7.0

Does anybody have any ideas why this is occuring...?

I have put a UNIQUE constraint on a column in a table.
The column does contain <NULL> values and should do by design.
However when I try to apply the constraint it errors saying that the column
contains duplicate values.
Other than the <NULL> there are no duplicates.
If I remove those rows with a <NULL> value in the column I am trying to constrain
the constraint is applied OK!!

Now the interesting point is that the MS SQL Server 7.0 docs say that a UNIQUE
constraint doesnt consider <NULL> values.

Evidence from SQL Server Documentation
UNIQUE Constraints
You can use UNIQUE constraints to ensure that no duplicate values are entered in
specific columns that do not participate in a primary key. While both a UNIQUE
constraint and a primary key enforce uniqueness, use a UNIQUE constraint instead
of a PRIMARY KEY constraint when you want to enforce the uniqueness of:

A column that allows null values.
UNIQUE constraints can be defined on columns that allow null values, whereas
PRIMARY KEY constraints can be defined only on columns that do not allow null
values.

Regards Paul

 
 
 

UNIQUE Constraints

Post by Kalen Delane » Thu, 12 Apr 2001 20:38:44


For the purpose of UNIQUE indexes, all NULLs are considered equal. You can
only have one NULL with a unique constraint.

--
-------------------------------------
Kalen Delaney
MCSE, SQL Server MCT, MVP
www.InsideSQLServer.com

Feed Someone for Free Today!
www.thehungersite.com


Hi All

This is on SQLServer 7.0

Does anybody have any ideas why this is occuring...?

I have put a UNIQUE constraint on a column in a table.
The column does contain <NULL> values and should do by design.
However when I try to apply the constraint it errors saying that the column
contains duplicate values.
Other than the <NULL> there are no duplicates.
If I remove those rows with a <NULL> value in the column I am trying to
constrain
the constraint is applied OK!!

Now the interesting point is that the MS SQL Server 7.0 docs say that a
UNIQUE
constraint doesnt consider <NULL> values.

Evidence from SQL Server Documentation
UNIQUE Constraints
You can use UNIQUE constraints to ensure that no duplicate values are
entered in
specific columns that do not participate in a primary key. While both a
UNIQUE
constraint and a primary key enforce uniqueness, use a UNIQUE constraint
instead
of a PRIMARY KEY constraint when you want to enforce the uniqueness of:

A column that allows null values.
UNIQUE constraints can be defined on columns that allow null values, whereas
PRIMARY KEY constraints can be defined only on columns that do not allow
null
values.

Regards Paul