How to create a unique index on a NULL column?(如何在 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 列上创建唯一索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 NULL 列上创建唯一索引?
基础教程推荐
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01