I have an InnoDB table with a unique index on performance_id
, ticket_rank_id
and ticket_type_id
.
All of these id's have relations to other tables, but their values can also be NULL
.
MySQL allows duplicate rows if one of the column values is NULL so I decided to build a trigger
for this problem.
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 > 0 THEN
// Cancel insert action
ELSE
// Perform insert action
END IF;
END
The problem is AND ticket_rank_id = NEW.ticket_rank_id
where I have to check if ticket_rank_id
is NULL or has a value.
AND ticket_rank_id = NULL
does not work, it only works if i do AND ticket_rank_id IS NULL
.
Is there any slick solution for this or do I have to write separate queries depending on NEW.ticket_rank_id
and NEW.ticket_type_id
being NULL or not?