Question : Require a unique compound key/index that excludes nulls or alternative solution

I allready have a database that contains a unique compound index for two columns in a Microsoft SQL Server 2005 Standard edition database.

These 2 columns are called WholesaleID and SupplierID. Combined they make up a unique key.

The same Supplier will never sell a product that has the same WholesaleID. I want to make sure I do not enter the same product twice if I can help it so I use the WholesaleID when it exists with the supplierID. However while there will be no Null SupplierIDs some of the WholesaleID values are unknown for some suppliers and will be Null.

This violates the Constraint as 2 Null values are considered the same value.

I require a unique compound constraint that prevents me from inserting values that are NOT Unique with the exception of Null values for the WholesaleID.

What is the most effective and efficient way of achieving this or should I be looking at a completely different method then having a Unique compount index that excludes nulls. Ive read that such an index is dodgy.
 
Microsoft SQL Server 2008 enables you to use an efficient solution ---  define a unique filtered index based on a predicate that excludes NULLs.

Im not sure if SQL Server 2005 edition supports this option or if there is a work around that is just as efficient.

A detailed solution would be much appreciated.

Answer : Require a unique compound key/index that excludes nulls or alternative solution

That is an intentional error indicating that the two lines cannot be executed:

insert into TheStuff (a,b) values('test','DEF')
GO
insert into TheStuff (a,b) values('test','DEF')

while these two can:

insert into TheStuff (a,b) values('test',null)
GO
insert into TheStuff (a,b) values('test',null)
Random Solutions  
 
programming4us programming4us