title: MySQL unique constraint of two columns only if second column is a specific value
body: <p>Hi I have a MysQL table in which I save the clients</p>
  
  <p>In one column I save the IP Address,</p>
  
  <p>In other column I set if the client is Active or Not
  Also I save the name, mail, etc...</p>
  
  <p>If one client left, we just set the Active column to 0, (to keep Payments history and so), when new client arrives we can reassign the same IP</p>
  
  <p>Is there a way to create a UNIQUE constraint to allow just one IP that is ACTIVE ?</p>
  
  <p>example</p>
  
  <pre><code>IP    |   ACTIVE  |   DATE
  ---------------------------------
  102   |      1    |    2017-10-12
  103   |      1    |    2017-10-12
  103   |      0    |    2017-10-13
  103   |      0    |    2017-10-09
  </code></pre>
  
  <p>So It can be multiples rows with the IP and 0 but only one with the IP and 1</p>
  
  <p>I'm thinking about a trigger before INSERT, UPDATE that do a SELECT and verify if there is no other row with that IP and 1 in ACTIVE</p>
  
  <p>Suggestions?</p>