{"id":69423,"date":"2016-12-27T10:41:41","date_gmt":"2016-12-27T10:41:41","guid":{"rendered":"https:\/\/www.simple-talk.com\/?p=69423"},"modified":"2016-12-28T17:55:27","modified_gmt":"2016-12-28T17:55:27","slug":"allow-nulls-unique-fields","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/allow-nulls-unique-fields\/","title":{"rendered":"How to allow nulls in unique fields"},"content":{"rendered":"<h2>An interesting workaround using unique fields<\/h2>\n<p>Unique fields in SQL Server are created using unique constraints or unique indexes, furthermore, each unique constraint uses a unique index.<\/p>\n<p>Regardless of\u00a0using unique constraint or unique index, the field can accept null values, however\u00a0the uniqueness will result in only accepting a single row with null value.<\/p>\n<p>The solution to allow nulls in unique fields is create a unique filtered index excluding the nulls of the index, due to that the uniqueness of the nulls will not be validated and multiple rows with nulls will be accepted.<\/p>\n<p>First, let&#8217;s see the regular behavior of unique fields:<\/p>\n<div><span style=\"font-family: Courier New;font-size: 10pt\"> <span style=\"color: blue\">CREATE<\/span>\u00a0<span style=\"color: blue\">TABLE<\/span>\u00a0<span style=\"color: maroon\">testunique<\/span> <br \/> \u00a0\u00a0<span style=\"color: maroon\">(<\/span> <br \/> \u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">id<\/span>\u00a0\u00a0\u00a0\u00a0<span style=\"color: black;font-style: italic\">INT<\/span>\u00a0<span style=\"color: blue\">IDENTITY<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">1<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: black\">1<\/span><span style=\"color: maroon\">)<\/span><span style=\"color: silver\">,<\/span> <br \/> \u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">value<\/span>\u00a0<span style=\"color: black;font-style: italic\">VARCHAR<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">30<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: blue\">NULL<\/span>\u00a0<span style=\"color: blue\">UNIQUE<\/span> <br \/> \u00a0\u00a0<span style=\"color: maroon\">)<\/span> <\/p>\n<p> <span style=\"color: maroon\">go<\/span> <\/p>\n<p> <span style=\"color: green;font-style: italic\">&#8212;\u00a0The\u00a0first\u00a0two\u00a0inserts\u00a0will\u00a0work<\/span> <br \/> <span style=\"color: blue\">INSERT<\/span>\u00a0<span style=\"color: blue\">INTO<\/span>\u00a0<span style=\"color: maroon\">testunique<\/span> <br \/> <span style=\"color: blue\">VALUES<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: red\">&#8216;value1&#8217;<\/span><span style=\"color: maroon\">)<\/span> <\/p>\n<p> <span style=\"color: blue\">INSERT<\/span>\u00a0<span style=\"color: blue\">INTO<\/span>\u00a0<span style=\"color: maroon\">testunique<\/span> <br \/> <span style=\"color: blue\">VALUES<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: blue\">NULL<\/span><span style=\"color: maroon\">)<\/span> <\/p>\n<p> <span style=\"color: maroon\">go<\/span> <\/p>\n<p> <span style=\"color: green;font-style: italic\">&#8211;Next\u00a0two\u00a0inserts\u00a0will\u00a0fail,\u00a0they\u00a0aren&#8217;t\u00a0unique\u00a0\u00a0<\/span> <br \/> <span style=\"color: blue\">INSERT<\/span>\u00a0<span style=\"color: blue\">INTO<\/span>\u00a0<span style=\"color: maroon\">testunique<\/span> <br \/> <span style=\"color: blue\">VALUES<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: red\">&#8216;value1&#8217;<\/span><span style=\"color: maroon\">)<\/span> <\/p>\n<p> <span style=\"color: maroon\">go<\/span> <\/p>\n<p> <span style=\"color: blue\">INSERT<\/span>\u00a0<span style=\"color: blue\">INTO<\/span>\u00a0<span style=\"color: maroon\">testunique<\/span> <br \/> <span style=\"color: blue\">VALUES<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: blue\">NULL<\/span><span style=\"color: maroon\">)<\/span> <\/p>\n<p> <span style=\"color: maroon\">go<\/span> <\/p>\n<p> <span style=\"color: blue\">DROP<\/span>\u00a0<span style=\"color: blue\">TABLE<\/span>\u00a0<span style=\"color: maroon\">testunique<\/span>\u00a0 <\/span><\/div>\n<p>Now let reapeat the same example again, using a filtered index:<\/p>\n<div><span style=\"font-family: Courier New;font-size: 10pt\"> <span style=\"color: blue\">CREATE<\/span>\u00a0<span style=\"color: blue\">TABLE<\/span>\u00a0<span style=\"color: maroon\">testunique<\/span> <br \/> \u00a0\u00a0<span style=\"color: maroon\">(<\/span> <br \/> \u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">id<\/span>\u00a0\u00a0\u00a0\u00a0<span style=\"color: black;font-style: italic\">INT<\/span>\u00a0<span style=\"color: blue\">IDENTITY<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">1<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: black\">1<\/span><span style=\"color: maroon\">)<\/span><span style=\"color: silver\">,<\/span> <br \/> \u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">value<\/span>\u00a0<span style=\"color: black;font-style: italic\">VARCHAR<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">30<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: blue\">NULL<\/span>\u00a0<br \/> \u00a0\u00a0<span style=\"color: maroon\">)<\/span> <\/p>\n<p> <span style=\"color: maroon\">go<\/span> <\/p>\n<p> <span style=\"color: green;font-style: italic\">&#8212;\u00a0Creating\u00a0the\u00a0filtered\u00a0index<\/span> <br \/> <span style=\"color: blue\">CREATE<\/span>\u00a0<span style=\"color: blue\">UNIQUE<\/span>\u00a0<span style=\"color: blue\">INDEX<\/span>\u00a0<span style=\"color: maroon\">indunique<\/span> <br \/> \u00a0\u00a0<span style=\"color: blue\">ON<\/span>\u00a0<span style=\"color: maroon\">testunique<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">value<\/span><span style=\"color: maroon\">)<\/span> <br \/> \u00a0\u00a0<span style=\"color: blue\">WHERE<\/span>\u00a0<span style=\"color: maroon\">value<\/span>\u00a0<span style=\"color: blue\">IS<\/span>\u00a0<span style=\"color: blue\">NOT<\/span>\u00a0<span style=\"color: blue\">NULL<\/span> <\/p>\n<p> <span style=\"color: maroon\">go<\/span> <\/p>\n<p> <span style=\"color: green;font-style: italic\">&#8212;\u00a0The\u00a0first\u00a0two\u00a0inserts\u00a0will\u00a0work<\/span> <br \/> <span style=\"color: blue\">INSERT<\/span>\u00a0<span style=\"color: blue\">INTO<\/span>\u00a0<span style=\"color: maroon\">testunique<\/span> <br \/> <span style=\"color: blue\">VALUES<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: red\">&#8216;value1&#8217;<\/span><span style=\"color: maroon\">)<\/span> <\/p>\n<p> <span style=\"color: blue\">INSERT<\/span>\u00a0<span style=\"color: blue\">INTO<\/span>\u00a0<span style=\"color: maroon\">testunique<\/span> <br \/> <span style=\"color: blue\">VALUES<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: blue\">NULL<\/span><span style=\"color: maroon\">)<\/span> <\/p>\n<p> <span style=\"color: maroon\">go<\/span> <\/p>\n<p> <span style=\"color: green;font-style: italic\">&#8211;This\u00a0insert\u00a0will\u00a0fail,\u00a0the\u00a0first\u00a0value\u00a0is\u00a0already\u00a0present\u00a0\u00a0<\/span> <br \/> <span style=\"color: blue\">INSERT<\/span>\u00a0<span style=\"color: blue\">INTO<\/span>\u00a0<span style=\"color: maroon\">testunique<\/span> <br \/> <span style=\"color: blue\">VALUES<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: red\">&#8216;value1&#8217;<\/span><span style=\"color: maroon\">)<\/span> <\/p>\n<p> <span style=\"color: maroon\">go<\/span> <\/p>\n<p> <span style=\"color: green;font-style: italic\">&#8212;\u00a0this\u00a0second\u00a0insert\u00a0will\u00a0work,\u00a0null\u00a0values\u00a0are\u00a0not\u00a0part<\/span> <br \/> <span style=\"color: green;font-style: italic\">&#8212;\u00a0of\u00a0the\u00a0index,\u00a0therefore\u00a0duplicate\u00a0values\u00a0are\u00a0accepted<\/span> <br \/> <span style=\"color: blue\">INSERT<\/span>\u00a0<span style=\"color: blue\">INTO<\/span>\u00a0<span style=\"color: maroon\">testunique<\/span> <br \/> <span style=\"color: blue\">VALUES<\/span>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: blue\">NULL<\/span><span style=\"color: maroon\">)<\/span> <\/p>\n<p> <span style=\"color: maroon\">go<\/span> <\/p>\n<p> <span style=\"color: blue\">DROP<\/span>\u00a0<span style=\"color: blue\">TABLE<\/span>\u00a0<span style=\"color: maroon\">testunique<\/span>\u00a0 <\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>An interesting workaround using unique fields Unique fields in SQL Server are created using unique constraints or unique indexes, furthermore, each unique constraint uses a unique index. Regardless of\u00a0using unique constraint or unique index, the field can accept null values, however\u00a0the uniqueness will result in only accepting a single row with null value. The solution&#8230;&hellip;<\/p>\n","protected":false},"author":50808,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-69423","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/69423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/50808"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=69423"}],"version-history":[{"count":3,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/69423\/revisions"}],"predecessor-version":[{"id":69427,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/69423\/revisions\/69427"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=69423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=69423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=69423"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=69423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}