title: Can CHECK constraints act like if else?
body: <p>I have a table with 4 columns:</p>
  <pre><code>(ID (PK, int, NOT NULL), col1 (NULL), col2 (NULL), col3 (NULL))
  </code></pre>
  <p>I'd like to add a <code>CHECK</code> constraint (table-level I think?) so that:</p>
  <pre><code>if col1 OR col2 are NOT NULL then col3 must be NULL
  </code></pre>
  <p>and</p>
  <pre><code>if col3 is NOT NULL then col1 AND col2 must be NULL
  </code></pre>
  <p>i.e. <code>col3</code> should be <code>null</code> if <code>col1</code> and <code>col2</code> are not null or vice-versa</p>
  <p>I am very new to SQL and SQL server though and am not sure how to actually implement this or even if it can/should be implemented?</p>
  <p>I think maybe:</p>
  <pre><code>CHECK ( (col1 NOT NULL OR col2 NOT NULL AND col3 NULL) OR 
          (col3 NOT NULL AND col1 NULL AND col2 NULL) )
  </code></pre>
  <p>But I am not sure if the brackets can be used to group the logic like this? If not, how can this best be implemented?</p>