title: MySQL Trigger checking for existing rows with possible NULL values
body: <p>I have an InnoDB table with a unique index on <code>performance_id</code>, <code>ticket_rank_id</code> and <code>ticket_type_id</code>. </p>
  
  <p>All of these id's have relations to other tables, but their values can also be <code>NULL</code>. 
  MySQL allows duplicate rows if one of the column values is NULL so I decided to build a <code>trigger</code> for this problem.</p>
  
  <pre><code>CREATE TRIGGER `before_insert_performance_tickets` 
  BEFORE INSERT ON `performance_tickets` FOR EACH ROW BEGIN
  
  DECLARE num_rows INTEGER;
  
  SELECT COUNT(*) INTO num_rows FROM performance_tickets
  WHERE performance_id = NEW.performance_id 
  AND ticket_rank_id = NEW.ticket_rank_id
  AND ticket_type_id = NEW.ticket_type_id;
  
  IF num_rows &gt; 0 THEN
     // Cancel insert action
  ELSE
     // Perform insert action
  END IF;
  
  END
  </code></pre>
  
  <p>The problem is <code>AND ticket_rank_id = NEW.ticket_rank_id</code> where I have to check if <code>ticket_rank_id</code> is NULL or has a value. </p>
  
  <p><code>AND ticket_rank_id = NULL</code> does not work, it only works if i do <code>AND ticket_rank_id IS NULL</code>.</p>
  
  <p>Is there any slick solution for this or do I have to write separate queries depending on <code>NEW.ticket_rank_id</code> and <code>NEW.ticket_type_id</code> being NULL or not?</p>