Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2005
    Posts
    266

    Unanswered: UNIQUE INDEX if not NULL

    Hello !

    for MS SQL 2000
    how can i set an unique index on Serial column but only if Serial IS NOT NULL

    CREATE UNIQUE INDEX [IX_Product] ON [Product]([Serial]) ON [PRIMARY]

    i can have 100 rows with a NULL Serial


    thank you

  2. #2
    Join Date
    Feb 2003
    Location
    India
    Posts
    216
    unique index will contain one null. if this is not acceptable I suggest u should think in terms of creating a view.

  3. #3
    Join Date
    Dec 2005
    Location
    Tilburg, Netherlands
    Posts
    73
    ... like living like a vegetarian, except when you're eating meat ?!
    'Unique' means unique, and not 'unique excpet for NULL'. As upalsen already stated, a unique index will accept exactly one (hence 'unique') NULL value ... period.

    Gr,
    Yveau

    > SELECT * FROM users WHERE clue > 0;
    Empty set (0.00 sec)


  4. #4
    Join Date
    Dec 2005
    Posts
    266
    I need in any case a column allowing NULL but if it is not NULL it must be different

    i dont live with a vegetarian, i am vegetarian

  5. #5
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    a trigger perhaps?
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  6. #6
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    You can do this in DB2, but not in SQL Server, and yes you can use a trigger with a non unique index
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  7. #7
    Join Date
    Feb 2003
    Location
    India
    Posts
    216
    u can try a workaround like this - create another dummy computed column on the same table. enter the below as formula for that column

    isnull(Col_UniqueOrNull,newid())

    or if u feel that newid() is a bit overkill, replace it with an identity/PK of that table. finally create a unique constraint index on that dummy column ......
    Last edited by upalsen; 12-19-06 at 05:29.

  8. #8
    Join Date
    Dec 2005
    Posts
    266
    ok ! it is mor simple inthat case to do it from outside (.NET)

    but good to know that you can do it with DB2

    thank you

  9. #9
    Join Date
    Mar 2005
    Location
    Netherlands
    Posts
    280
    Just a thought...
    If NULL <> NULL because NULL is undefined (and could be anything) why does a unique index only allow one NULL value?

    But then again, how would you want a GROUP BY to react on a NULL value. In the current SQL Server implementation it conveniently groups all NULL values together instead of listing each NULL separatly (which would be logical if a unique index could contain multiple NULL values). How does DB2 do this?

  10. #10
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Yeah, I thought I did this already

    http://weblogs.sqlteam.com/brettk/ar...4/20/4592.aspx
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •