What is the difference between the Mysql Trigger and Mysql Event exception?

Asked

Viewed 462 times

6

I would like to create a e-mail trigger scheduler, without the use of cron, and would like to know if it is possible to do this through Mysql.

Searching the Internet I saw some examples of event creation no use of cron:

CREATE EVENT PurgeLogTable
ON SCHEDULE EVERY 1 WEEK
DO
BEGIN
DELETE FROM `logs` WHERE `LogTime` <= DATE_SUB(CURRENT_TIMESTAMP,INTERVAL 1 WEEK);
INSERT INTO `audit` (`AuditDate`, `Message`) VALUES(NOW(), "Log table purged succesfully!");
END 

And an example of email firing using Trigger, I just don’t know if it works because I haven’t tried it:

CREATE TRIGGER send_emailverifier AFTER INSERT, UPDATE ON tbl_users 
FOR EACH ROW BEGIN 
SELECT * FROM email_bodies WHERE EmailID = 1; 
SELECT * FROM tbl_users WHERE ClientID = @ClientID 
INSERT INTO tbl_emailverify VALUES (UUID, tbl_users.ClientID, OLD.CltEmail, NEW.CltEmail) 
SELECT concat("To: ",NEW.CltEmail & "," & OLD.CltEmail), 
"From: [email protected]", 
concat("Subject: ",NEW.subject), 
"", 
email_bodies.EmailContent 
INTO OUTFILE "/inetpub/mailroot/pickup/mail.eml" 
FIELDS TERMINATED by '\r\n'; 
END 

But my question is pertinent as to how I could warn an email trigger, so that it triggers the sending URL, example:

/enviar-emails/go

1 answer

4


They have very different purposes, as the syntax presented in the question already gives a hint.

The TRIGGER is an old database resource in general to fire - as the name says - an execution of something when something occurs in a table. A change in the data (INSERT, UPDATE, DELETE) causes secondary action.

The EVENT is a relatively new resource that is determined by time, is a scheduler. It is a way to ensure that an action is executed from time to time, regardless of what happens in the database. It is a kind of cron of the database.

So it depends on the goal to fire the email. Every time an email is inserted or updated in the table, do you need to fire it? Use the TRIGGER, as shown in the code above. If sending - even if selectively - should be done from time to time, regardless of what happens in the database, use the EVENT.

The action you both perform can be basically the same. Of course there are some limitations on EVENT because it is not manipulating data. For example, it does not have an old or new version of data that can be used in TRIGGER, but it is a limitation that does not hinder because nor would it make sense to have it in something that is not updating the data.

  • Thanks for the clarification.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.