I am aware that it is possible to create UNIQUE
constraints with a WHERE
clause for a field in an existing table, but is it possible to create them in the CREATE TABLE
statement? ie something like (I know this is invalid syntax):
CREATE TABLE [ExampleTable]
(
[ID] INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Name] NVARCHAR(20) DEFAULT '' NOT NULL UNIQUE NONCLUSTERED WHERE [Name] <> ''
)
With the aim here being that the [Name]
field must be unique, unless it's a blank string.