title: mysql check constraint where either one of two columns should be null
body: <p>(Sorry if this is a duplicate post, but I can't seem to find an example for this problem)</p>
  
  <p>I have a mysql table with 4 columns like this:</p>
  
  <pre><code>SomeTable
  =========
  ID (int)
  name (varchar(255))
  column1 (varchar(255))
  column2 (double)
  </code></pre>
  
  <p>What I now want to do is to add a constraint so that there is always one column (either column1 or column2) that has a null value. I have tried the following:</p>
  
  <pre><code>ALTER TABLE mytable 
      ADD CHECK (
          (column1 IS NOT NULL &amp;&amp; column2 IS NULL) || 
          (column2 IS NOT NULL &amp;&amp; column1 IS NULL)
      )
  </code></pre>
  
  <p>But it doesnt seem to be working since I still can have cases like this:</p>
  
  <pre><code>CASE1:
  ------
  name: bla
  column1: null
  column2: null
  
  CASE2:
  ------
  name: bla
  column1: somevalue
  column2: 123
  </code></pre>
  
  <p>How can I get this working so that I get an error when I try case1 and case2?</p>
  
  <p><em>(Additionally: if my memory serves me well: the constraint I used can be shortened, but I can't remember how it was done. So I would be happy if someone helped me with that as well!)</em></p>