具有空值的SQL Server 2005唯一键

Cur*_*tis 8 sql-server-2005 unique-key

我在SQL Server 2005中有一个带有外键的表,我希望该外键是唯一值或null.我已将其设置为唯一键,但它不允许我在同一个表中有多个空值.有可能做我想要的吗?

Tho*_*mas 9

这是关于SQL Server的唯一约束/索引的长期抱怨.最好的解决方案是使用schemabinding创建一个视图,然后在该列上放置一个唯一索引:

Create View dbo.MyUniqueColView
With SchemaBinding
As
Select MyColToBeUnique
From MyTable
Where MyColToBeUnique Is Not Null

GO

Create Unique Clustered Index IX_MyTable_MyColToBeUnique On MyUniqueColView ( MyColToBeUnique )
Run Code Online (Sandbox Code Playgroud)