添加非null唯一约束 - 而不是索引

时间:2017-10-30 11:19:35

标签: sql-server tsql

基本上我想转此:

ALTER TABLE [dbo].[Computer] ADD  CONSTRAINT [AK_ResourceId] UNIQUE NONCLUSTERED 
(
    [ResourceId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

不影响NULL值。其他帖子仅给出了创建唯一索引的答案,但是当INSERT上出现重复值时,索引不会创建违规。我想要一个约束,但我似乎无法对此做出WHERE语句..

使用SQL Server 2014

更新

毕竟它确实适用于索引:)

SQL:

CREATE UNIQUE NONCLUSTERED INDEX IX_ResourceId
ON dbo.Computer(ResourceId)
WHERE ResourceId IS NOT NULL;

这将产生以下错误:

Cannot insert duplicate key row in object 'dbo.Computer' with unique index 'IX_ResourceId'. The duplicate key value is (1234)

0 个答案:

没有答案