June 26, 2008 at 10:52 am
I always remember that unique constraints allow nulls, but forget that they only allow one null (assuming the constraint is on just one column).
I have a table in which a column value must be unique, if it exists. If the value does not exist, then it can be null (unlimited number of nulls).
I started by assigning a unique constraint to a nullable column, but, of course, that doesn't work when I add the data with numerous null values.
What is a good alternative? I can think of a few:
Insert/update triggers
Additional table
I'm leaning toward triggers. The additional table would have only two columns (the id relating it back to the parent table and the non-null unique code), and many queries would involve outer joins to that table.
Is there a standard way to implement this type of constraint?
June 26, 2008 at 8:28 pm
Stephanie Giovannini (6/26/2008)
I always remember that unique constraints allow nulls, but forget that they only allow one null (assuming the constraint is on just one column).I have a table in which a column value must be unique, if it exists. If the value does not exist, then it can be null (unlimited number of nulls).
I started by assigning a unique constraint to a nullable column, but, of course, that doesn't work when I add the data with numerous null values.
What is a good alternative? I can think of a few:
Insert/update triggers
Additional table
I'm leaning toward triggers. The additional table would have only two columns (the id relating it back to the parent table and the non-null unique code), and many queries would involve outer joins to that table.
Is there a standard way to implement this type of constraint?
Second idea is probably the best with a little help... foreign key to a definition table of allowable values...
--Jeff Moden
Change is inevitable... Change for the better is not.
June 27, 2008 at 8:19 am
Hmmm... I suppose the second table is the best way to do it, if I'm allowed to do so. At least I now have ammunition.
June 27, 2008 at 8:26 am
You can use indexed views for this
http://www.sqlservercentral.com/Forums/Topic471378-338-1.aspx#bm471408
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537June 27, 2008 at 8:30 am
Stephanie Giovannini (6/27/2008)
Hmmm... I suppose the second table is the best way to do it, if I'm allowed to do so. At least I now have ammunition.
Sorry, Stephanie... I have to remember... "Optional" and "Unique". 2nd Table with FK would be way too high maintenance. Bad suggestion on my part... sorry.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 27, 2008 at 9:11 am
Jeff Moden (6/27/2008)
Stephanie Giovannini (6/27/2008)
Hmmm... I suppose the second table is the best way to do it, if I'm allowed to do so. At least I now have ammunition.Sorry, Stephanie... I have to remember... "Optional" and "Unique". 2nd Table with FK would be way too high maintenance. Bad suggestion on my part... sorry.
OK, then. My plan now is:
- One table
- Optional, unique column is nullable and indexed
- Uniqueness enforced by insert/update trigger
Actually, the two tables make queries sort of awkward. I already made the changes to move the optional code into a separate table. I'll just change it back.
June 28, 2008 at 12:16 pm
So, how did you solve the problem of both being optional AND unique?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply