4

I've created a trigger (never done it before).
My goal is:
WHEN one or more rows are INSERTED into the table 'userlite'
IF there are rows with 'IdLite' as in the new row inserted in the table 'litedetails'
THEN add a row to the table 'informations' for each row counted.

The new rows data fields will be:

IdUser -> from new row inserted into the table 'userlite'

IdLite -> it's the same on new row inserted into the table 'userlite' and into rows selected from the table 'litedetails'

IdEvent -> from rows selected

I used the code below to create the trigger

DELIMITER $$
CREATE TRIGGER after_newuserlite
AFTER INSERT ON userlite
FOR EACH ROW
BEGIN
IF (
    (
     SELECT COUNT(*) 
     FROM litedetails
     WHERE IdLite = NEW.IdLite
    ) > 0
   ) THEN
   INSERT INTO informations (IdUser, IdLite, IdEvent)
   SELECT NEW.IdUser AS IdUser, IdLite, IdEvent
   FROM litedetails
   WHERE IdLite = NEW.IdLite;
END IF;
END;
$$

I've tested it and all seems to work but I'm worried for my inexperience, so my questions are:
1) Is there anything that can cause my trigger to fail?
2) what happens if the trigger fails?
3) If the trigger fails the query who started the trigger will mantain its effects?

genespos
  • 3,211
  • 6
  • 38
  • 70
  • You should investigate transactions, as these ensure all actions happen or no actions happen. – Toby Allen Apr 16 '16 at 09:06
  • @TobyAllen Thanks for your comment but, as I said, I never used a trigge before, so I don't know how to correctly test it. – genespos Apr 16 '16 at 09:23
  • Did you investigate transactions? – Toby Allen Apr 16 '16 at 09:33
  • @TobyAllen Sorry for my limit, what it means? – genespos Apr 16 '16 at 09:36
  • Post the full error message. Just a guess: Replace the last two rows with `END $$ DELIMITER ;` – Paul Spiegel Apr 16 '16 at 09:39
  • @PaulSpiegel I don't get errors! All seems to work. But I'm not a pro and this is my first trigger so I'm to sure if all is correct and I want to know what happens if something fails. (Or how can I test or force it to fail to see what happens) – genespos Apr 16 '16 at 09:44
  • 2
    If you use transactional tables (storage engine is ACID compliant), trigger failure causes entire statement to fail, causing a rollback. If you don't use transactional tables (MyISAM) then any changes made by the statements will be in effect, even if the trigger fails. Basically, if you use InnoDB or TokuDB and your trigger fails, that will cause your query to fail also, thus no writes will be made and an error will be raised and sent back. As for what makes trigger to fail - there can be many things involved in that, but it's important to know that if it fails, you will be notified :) – N.B. Apr 16 '16 at 09:47
  • @genespos, sorry i somehow mixed up two questions and read "Syntax error" in the title. I will delete my comments in few minutes. – Paul Spiegel Apr 16 '16 at 10:12
  • @PaulSpiegel Not your fault but mine! I posted the question with a wrong title. Sorry for generated cofusion. – genespos Apr 16 '16 at 10:19

1 Answers1

4

As per comment: when using tables that support transactions, the triggers are then part of the statement. If a trigger fails, it causes the query that triggered it to fail as well, which causes a rollback. This applies to InnoDB and TokuDB storage engines.

For MyISAM, which isn't transactional engine, the trigger might error out but it won't cause a rollback (because it isn't supported by that storage engine).

Triggers can fail due to many reasons, just like regular queries can, but if they fail - you will receive an error message / notification that will let you act upon it (notify the user about failure, log the message, try again etc.).

N.B.
  • 13,688
  • 3
  • 45
  • 55