The proper way to implement unique constraint that allows multiple NULL values in SQL Server

前端 未结 3 1903
臣服心动
臣服心动 2020-12-17 19:52


I need 1 column in the table to hold unique non-null values or NULL. TSQL UNIQUE constraint treats 2 NULLs as equal, so I cannot

相关标签:
3条回答
  • 2020-12-17 20:33

    Normalise it. Move the column to a new table together with your current table's primary key. Make the column unique and not null in the new table. Nullable unique constraints make no logical sense and are of little or no practical use.

    0 讨论(0)
  • 2020-12-17 20:36

    SQL 2008 allows you to define a filtered index - essentially an index with a WHERE clause - see Phil Haselden's asnwer to this question for MSDN links and an example.

    0 讨论(0)
  • 2020-12-17 20:55

    4 ways:

    • Filtered index (SQL Server 2008) <- recommended based on your tags
    • Trigger (mentioned)
    • Indexed view (in your question)
    • Unique constraint/index with computed column (in your question)
    0 讨论(0)
提交回复
热议问题