问题描述
我试图通过允许 UNIQUE NULLABLES 来消除该错误,但它一直出现
我希望字段具有唯一值,除非它们为空
(我知道有很多答案,但它们与创建表查询无关)
我做错了什么?
CREATE TABLE [dbo].[users] (
[Id] INT IDENTITY (1,1) NOT NULL PRIMARY KEY,[Email] VARCHAR (100) UNIQUE NONCLUSTERED NULL DEFAULT NULL,[Login] VARCHAR (100) UNIQUE NONCLUSTERED NULL DEFAULT NULL,[Identifier] VARCHAR(40) NOT NULL UNIQUE DEFAULT newid(),[ResetPasswordToken] VARCHAR(40) UNIQUE NONCLUSTERED NULL DEFAULT NULL,[ResetPasswordDate]DATETIME UNIQUE NONCLUSTERED NULL DEFAULT NULL,...(other fields)
);
感谢您对此的帮助
解决方法
答案是关于使用“GO”这个词
GO
CREATE UNIQUE NONCLUSTERED INDEX [IX_User_Email] ON [dbo].[Users]([Email] ASC) WHERE ([Email] IS NOT NULL);
GO
CREATE UNIQUE NONCLUSTERED INDEX [IX_User_Login] ON [dbo].[Users]([Login] ASC) WHERE ([Login] IS NOT NULL);
GO
CREATE UNIQUE NONCLUSTERED INDEX [IX_User_ResetPasswordToken] ON [dbo].[Users]([ResetPasswordToken] ASC) WHERE ([ResetPasswordToken] IS NOT NULL);
GO
CREATE UNIQUE NONCLUSTERED INDEX [IX_User_ResetPasswordDate] ON [dbo].[Users]([ResetPasswordDate] ASC) WHERE ([ResetPasswordDate] IS NOT NULL);