3

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?

4

2 回答 2

1

You need to add extra OR condition for NULL values (ticket_rank_id IS NULL OR ticket_rank_id = NEW.ticket_rank_id) because NULL compared with anything return FALSE. Try this query:

SELECT COUNT(*)
INTO num_rows
FROM performance_tickets
WHERE performance_id = NEW.performance_id
      AND (ticket_rank_id IS NULL OR ticket_rank_id = NEW.ticket_rank_id)
      AND (ticket_type_id IS NULL OR ticket_type_id = NEW.ticket_type_id);
于 2012-09-13T09:24:27.813 回答
0

I think I figured it out:

SELECT COUNT(*) INTO num_rows FROM performance_tickets
WHERE performance_id = NEW.performance_id 
AND IF(NEW.ticket_rank_id IS NULL, ticket_rank_id IS NULL, ticket_rank_id = NEW.ticket_rank_id)
AND IF(NEW.ticket_type_id IS NULL, ticket_type_id IS NULL, ticket_type_id = NEW.ticket_type_id)

Seems to work, now I have to figure out how to abort the insert if num_rows == 1. But that's a different problem.

Thanks anyways ;)

于 2012-09-13T10:08:10.377 回答