如何在 NULL 列上创建唯一索引? [英] How to create a unique index on a NULL column?

查看:34
本文介绍了如何在 NULL 列上创建唯一索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 SQL Server 2005.我想将列中的值限制为唯一,同时允许 NULL.

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)

有更好的想法吗?

推荐答案

很确定你不能这样做,因为它违反了唯一性的目的.

Pretty sure you can't do that, as it violates the purpose of uniques.

然而,这个人似乎有一个体面的工作:http://sqlservercodebook.blogspot.com/2008/04/multiple-null-values-in-unique-index-in.html

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆