Skip to content

Instantly share code, notes, and snippets.

@chilversc
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chilversc/9464280 to your computer and use it in GitHub Desktop.
Save chilversc/9464280 to your computer and use it in GitHub Desktop.
MSSQL 2008 allowing nulls in unique constraints.
-- See http://technet.microsoft.com/en-us/library/ms188783.aspx
-- "For UNIQUE indexes, only the selected rows must have unique index values."
CREATE TABLE foo (pk int PRIMARY KEY IDENTITY, x int NULL);
GO
CREATE UNIQUE INDEX U_Foo_x ON foo (x) WHERE x is not null;
GO
INSERT INTO foo (x) VALUES (1);
INSERT INTO foo (x) VALUES (2);
INSERT INTO foo (x) VALUES (NULL);
INSERT INTO foo (x) VALUES (NULL);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment