如何在NULL列上创建唯一索引? [英] How to create a unique index on a NULL column?
本文介绍了如何在NULL列上创建唯一索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用SQL Server 2005.我想限制列中的值是唯一的,同时允许NULLS。
我当前的解决方案涉及一个唯一索引
CREATE VIEW vw_unq WITH SCHEMABINDING AS
SELECT Column1
From MyTable
WHERE Column1 IS NOT NULL
CREATE UNIQUE CLUSTERED INDEX unq_idx ON vw_unq(Column1)
有什么更好的想法吗?
解决方案
很肯定,你不能这样做,因为它违反了不重复的目的。
然而,这个人似乎有一个体面的工作:
http://sqlservercodebook.blogspot.com/2008/04/multiple-null-values-in-unique-index-in.html p>
I am using SQL Server 2005. I want to constrain the values in a column to be unique, while allowing NULLS.
My current solution involves a unique index on a view like so:
CREATE VIEW vw_unq WITH SCHEMABINDING AS
SELECT Column1
FROM MyTable
WHERE Column1 IS NOT NULL
CREATE UNIQUE CLUSTERED INDEX unq_idx ON vw_unq (Column1)
Any better ideas?
解决方案
Pretty sure you can't do that, as it violates the purpose of uniques.
However, this person seems to have a decent work around: http://sqlservercodebook.blogspot.com/2008/04/multiple-null-values-in-unique-index-in.html
这篇关于如何在NULL列上创建唯一索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文