title: MySQL unique constraint with NULL treated as equal to all other values
body: <p>I'm using MySQL 5.7.6 and I need to create an unusual composite unique index on my table.</p>
  
  <p>I need a unique constraint on 2 columns. However, if the 1st column is NULL, I want no other rows to be allowed with the same 2nd column.</p>
  
  <h2>Accepted:</h2>
  
  <p>a)</p>
  
  <pre><code>context | color
  ===============
  1       | red
  2       | red
  </code></pre>
  
  <p>b)</p>
  
  <pre><code>context | color
  ===============
  1       | red
  1       | blue
  </code></pre>
  
  <p>c)</p>
  
  <pre><code>context | color
  ===============
  NULL    | red
  NULL    | blue
  </code></pre>
  
  <h2>Rejected:</h2>
  
  <p>d)</p>
  
  <pre><code>context | color
  ===============
  1       | red
  1       | red
  </code></pre>
  
  <p>e)</p>
  
  <pre><code>context | color
  ===============
  1       | red
  NULL    | red
  </code></pre>
  
  <p>f)</p>
  
  <pre><code>context | color
  ===============
  NULL    | red
  NULL    | red
  </code></pre>
  
  <p>If needed, I can replace NULL with any other special value, like 0.</p>