方法一:
CREATE TABLE [Table] ( [id] [int] IDENTITY (1, 1) NOT NULL ,
[Col] [int] NULL ,
[Col_x] AS ( CASE WHEN Col IS NULL THEN id ELSE 0 END )
CONSTRAINT UNQ_NULLS UNIQUE ( Col,Col_x )
)
GOINSERT INTO [Table] VALUES( '1' )
INSERT INTO [Table] VALUES('2' )
INSERT INTO [Table] VALUES('3' )
INSERT INTO [Table] VALUES( NULL )
INSERT INTO [Table] VALUES( NULL )
INSERT INTO [Table] VALUES( '1' )
select * from [table]drop table [table]
方法二:
CREATE TABLE [Table] ( [id] [int] IDENTITY (1, 1) NOT NULL ,
[Col] varchar(10) NULL
)
GOCREATE VIEW dbo.V_Table
WITH SCHEMABINDING
ASSELECT id, Col
FROM dbo.[Table]WHERE (Col IS NOT NULL)
GOCREATE UNIQUE CLUSTERED INDEX IDX1 ON V_Table( Col )
GO