samedi 27 décembre 2014

Trigger: Get inserted row id


Merry Christmas to everyone reading :)


I just started using Triggers for MySQL today, and i came over an issue. I could find a way to get the new id of the row that has been inserted. My current code ("Which works") is this:


DROP TRIGGER IF EXISTS exampleTrigger;


DELIMITER //


CREATE TRIGGER exampleTrigger AFTER INSERT ON exampleTable


FOR EACH ROW BEGIN


UPDATE otherExampleTable


SET columnInt = columnInt+1


WHERE Name = (SELECT Name FROM exampleTable ORDER BY id DESC LIMIT 1);


END //


DELIMITER ;


Although this works fine, i was thinking that if for example 5, 10, or even 100 requests were made exactly on the same time the system could get the wrong values.


So i was looking for anything that could get the INSERTED id that triggered the INSERT TRIGGER. I found on some other threads that you could use the PSEUDOCODE "INSERTED" so the query would be.


SELECT Name FROM INSERTED


But that seems not to work.


I would apprieciate if anyone could help me figure this problem out :)


Thank you for reading, Vasilis Dimitriadis





Aucun commentaire:

Enregistrer un commentaire