{"id":86,"date":"2009-02-18T11:33:30","date_gmt":"2009-02-18T16:33:30","guid":{"rendered":"\/\/www.beauchamp.me\/techno\/?p=86"},"modified":"2011-08-12T21:58:27","modified_gmt":"2011-08-13T02:58:27","slug":"multiple-nulls-in-a-column-with-a-unique-constraint","status":"publish","type":"post","link":"https:\/\/www.beauchamp.me\/techno\/blog\/2009\/02\/18\/multiple-nulls-in-a-column-with-a-unique-constraint\/","title":{"rendered":"Multiple Nulls in a column with a UNIQUE constraint"},"content":{"rendered":"<p>I came accross a situation where I needed to temporarily insert multiple rows in a table with a Null value in a column with a UNIQUE constraint on it. In other words, I wanted the values in that column to be unique, except when they are Null. According to ANSI SQL specifications, this should be possible, but SQL Server does not allow this. I found a <a title=\"solution on another blog\" href=\"http:\/\/dimantdatabasesolutions.blogspot.com\/2007\/03\/unique-constraint-vs-nulls.html\" target=\"_blank\">solution on another blog<\/a>, which I like for its simplicity.<\/p>\n<p>I am reposting the code here just in case that ohter blog post ever disappears:<\/p>\n<pre class=\"brush:sql\">CREATE TABLE dupNulls (\r\n    pk int identity(1,1) primary key,\r\n    X  int NULL,\r\n    nullbuster as (case when X is null then pk else 0 end),\r\n    CONSTRAINT dupNulls_uqX UNIQUE (X,nullbuster)\r\n)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I came accross a situation where I needed to temporarily insert multiple rows in a table with a Null value in a column with a UNIQUE constraint on it. In other words, I wanted the values in that column to be unique, except when they are Null. According to ANSI SQL specifications, this should be [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[7,3],"tags":[],"class_list":["post-86","post","type-post","status-publish","format-standard","hentry","category-c","category-sql-server-2005"],"_links":{"self":[{"href":"https:\/\/www.beauchamp.me\/techno\/wp-json\/wp\/v2\/posts\/86","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.beauchamp.me\/techno\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.beauchamp.me\/techno\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.beauchamp.me\/techno\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.beauchamp.me\/techno\/wp-json\/wp\/v2\/comments?post=86"}],"version-history":[{"count":8,"href":"https:\/\/www.beauchamp.me\/techno\/wp-json\/wp\/v2\/posts\/86\/revisions"}],"predecessor-version":[{"id":93,"href":"https:\/\/www.beauchamp.me\/techno\/wp-json\/wp\/v2\/posts\/86\/revisions\/93"}],"wp:attachment":[{"href":"https:\/\/www.beauchamp.me\/techno\/wp-json\/wp\/v2\/media?parent=86"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.beauchamp.me\/techno\/wp-json\/wp\/v2\/categories?post=86"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.beauchamp.me\/techno\/wp-json\/wp\/v2\/tags?post=86"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}