Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IF NOT EXISTS in trigger

I have tow tables concept_access and concept_access_log. I want to create a trigger that works every time something is deleted from concept_access, check if there is similar record in log table and if not, inserts new one before it is deleted from concept_access.

I modified trigger and now it looks like this:

DROP TRIGGER IF EXISTS before_delete_concept_access;
DELIMITER //
CREATE TRIGGER before_delete_concept_access
    BEFORE DELETE ON `concept_access` FOR EACH ROW
    BEGIN
        IF (SELECT 1 FROM concept_access_log WHERE map=OLD.map 
                          AND accesstype=OLD.accesstype AND startdate=OLD.startdate AND stopdate=OLD.stopdate) IS NULL THEN
            INSERT INTO concept_access_log (map, accesstype, startdate, stopdate)
            VALUES (OLD.map, OLD.accesstype, OLD.startdate, OLD.stopdate);
        END IF;
    END//
DELIMITER ;

Sample data in concept_access before delete:

map accesstype  startdate   stopdate
1   public      NULL        NULL    
1   loggedin    2011-05-11  NULL    
1   friends     NULL        NULL    

Log table already has first 2 rows. And they are exactly the same as in concept_access. When I delete first row from concept_access table, I get this in log table:

map accesstype  startdate   stopdate
1   public      NULL        NULL    
1   loggedin    2011-05-11  NULL    
1   friends     NULL        NULL    
1   public      NULL        NULL    

While it is not supposed to insert anything because (1,public,null,null) already exists there.

This table has no primary key. I was not creating structure, so don't ask me why. Changing it will ruin a lot of already existing functionality. I just need to keep log of what was removed from table concept_access and store it in log without duplicates.

I would really appreciate, if anyone can figure out what is going wrong.

like image 900
Jull Avatar asked May 09 '11 23:05

Jull


2 Answers

DROP TRIGGER IF EXISTS before_delete_concept_access;
DELIMITER //
CREATE TRIGGER before_delete_concept_access
    BEFORE DELETE ON `concept_access` FOR EACH ROW
    BEGIN
        IF (SELECT COUNT(*) FROM concept_access_log WHERE map=OLD.map 
                          AND accesstype=OLD.accesstype AND startdate=OLD.startdate AND stopdate=OLD.stopdate) = 0 THEN
            INSERT INTO concept_access_log (map, accesstype, startdate, stopdate)
            VALUES (OLD.map, OLD.accesstype, OLD.startdate, OLD.stopdate);
        END IF;
    END//
DELIMITER ;

I am not using not exists, just test if the match count greater than 0

your code runs well on my machine, what's your MySQL version?

mysql> select version();
+------------+
| version()  |
+------------+
| 5.1.56-log |
+------------+
1 row in set (0.00 sec)
like image 164
neocanable Avatar answered Oct 16 '22 09:10

neocanable


Instead of this

... WHERE map = OLD.map ...

--  map   OLD.map  bool
--  ---   -------  ----
--  'a'    'a'     TRUE
--  'a'    'b'     FALSE
--  'a'    NULL    FALSE
--  NULL   'b'     FALSE
--  NULL   NULL    FALSE  <-- Undesired! We want this to be TRUE

try <=>, which is MySQL's answer to the SQL standard IS (NOT) DISTINCT FROM predicate:

... WHERE map <=> OLD.map ...
--  TRUE when both have the same non-NULL value or when both are NULL
--  Equivalents:
--    WHERE (map = OLD.map OR (map IS NULL AND OLD.map IS NULL))
--    WHERE (map = OLD.map OR COALESCE(map, OLD.map) IS NULL)
like image 45
pilcrow Avatar answered Oct 16 '22 11:10

pilcrow