trigger fire when server date change - mysql

How to create trigger that fires automatically when server date will change.

What do you mean by 'date change'. Do you mean an event that should be triggered every midinight? Use event scheduler for that.
If you mean 'whenever someone manually changes server data' - there is no reliable way to catch that in MySQL. You could have an event in event scheduler monitoring the date for changes, but I think it would be wasteful.

Set cron job.
Example for midnight every 1st and 15th of the month.
0 0 1,15 * * /scripts/phpscript.php

Related

How to make mysql run Delet query automatically

I Have an OTP table and I want to delete data that is older than 5 minutes automatically
Then how could I make trigger or procedure for that?
You must use according Event Scheduler procedure.
CREATE EVENT remove_old_rows
ON SCHEDULE
EVERY 10 SECOND
COMMENT 'Delete the rows that are older than 5 minutes from OTP table.'
DO
DELETE
FROM OTP_database.OTP_table
WHERE created_at < CURRENT_TIMESTAMP - INTERVAL 5 MINUTE;
Do not forget to enable Event Scheduler.
Don't do it! Just create view to get the most recent data:
create view v_otp as
select otp.*
from otp
where otp.created_at >= now() - interval 5 minute;
Anyone who uses the view only sees the most recent data.
Then you can leisurely delete old data during a period when the database is not busy.
An added benefit is that this is always accurate. If an event or job gets delayed, then your users might see old data. Further, this does not involve complicated locking and transaction semantics when the server is busy.

Why does MySQL 5.7 Even Scheduler START datetime not match with the CREATE/ALTER EVENT START time?

After I create the event...
DELIMITER $$
CREATE DEFINER=`abc`#`localhost` EVENT `testEventYearly` ON SCHEDULE EVERY 1 YEAR STARTS '2018-01-01 00:00:00' ON COMPLETION PRESERVE ENABLE DO BEGIN
CALL `spTestEventYearly` ();
END$$
...I look in the mysql.event table and see a starts value of 2018-01-01 06:00:00. I've checked the date/time on the server and all is well. Why is there a difference in time? And will it execute at 06:00 or 00:00?
https://dev.mysql.com/doc/refman/5.7/en/create-event.html says:
Times in the ON SCHEDULE clause are interpreted using the current session time_zone value. This becomes the event time zone; that is, the time zone that is used for event scheduling and is in effect within the event as it executes. These times are converted to UTC and stored along with the event time zone in the mysql.event table. This enables event execution to proceed as defined regardless of any subsequent changes to the server time zone or daylight saving time effects.
(emphasis mine)
I infer you are in the US Central time zone. You created the event when your local time zone was UTC -0600, and then the value stored in mysql.event was advanced six hours to UTC.

MySql - Missed event schedule

I am trying to use mysql event schedule in my application, I have not use it before so i have some confusions.
I want to know if my computer is off on the schedule date, then schedule will continue on next day, after starting my computer?
Like:
my schduled is for beginning at every month (no predefined time set)
if in the above date my computer/Server is off,
will mysql continue scheduled event in next day after turning on my computer/server?
If no, then please suggest a solution.
Hmmmm, have you looked at something like this?
MySQL: Using the Event Scheduler
... or:
How to create MySQL Events
... or even: [MySQL :: MySQL 5.1 Reference Manual: 19.4.1. Event Scheduler Overview](19.4.1. Event Scheduler Overview)?
Also please keep in mind that SQL DBMS servers are written with the rather strong presumption that they will be kept up and operating 24 hours per day with only brief periods of downtime for maintenance or repairs. There is generally very little consideration for operation on machines which are shutdown at night and while not in use.
If you simply store a table of dates and events then your can simply query that table for events which have passed or are upcoming within any range you like ... and you can run the program(s) containing those queries (and performing any appropriate activities based on the results) whenever you start you computer and periodically while it's up and running.
These links refer to a feature of MySQL which is designed to have the server internally execute certain commands (MySQL internal commands, such as re-indexing, creating/updating views, cleaning tables of data which "expires" and so on. I don't know if a MySQL server would attempt to execute all events which have passed during downtime, though it should only be a little bit of work to follow the tutorial, schedule some event for some time (say 15 minutes after the time you expect to hit [Enter]) ... then shutdown your computer (or even just the MySQL server) and go off to lunch. Then come back, start it up and see what happens.
The scheduled event could be something absurdly simple, like inserting the "current" time into some table you set up.

MySQL Triggers, deleting a row after inactivity?

I've done some googling but can't really get much relevant information. I'm trying to set a date/time for certain rows to be deleted depending on activity. If active, the time would be bumped to a later time unless activated once again.. Otherwise it will be deleted. I've managed to sort the rows when activated (inserted/updated) in activity.
Thanks in advance.
Firstly do not put this update/delete in a trigger if you have millions of rows that needs to be deleted you are going to see a huge performance hit on inserts/updates. It is not the best place for it. You can create either a cron job as Filype suggested. Or if you want to keep it all in MySQL use the MySQL Event scheduler.
Go to this page to read more about scheduling events in MySQL:
http://dev.mysql.com/doc/refman/5.1/en/events.html
MySQL Event allows you to schedule things on MySQL on a regular basis.
The code would look something like
CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
DELETE FROM MyTable Where Expired< NOW();
Here is a suggestion, I haven't tried yet, you might think to update the row with deleted=1 instead of actually deleting the record.
CREATE TRIGGER deleteInactiveRecords AFTER UPDATE,INSERT ON myTable
FOR EACH ROW BEGIN
DELETE FROM myTable WHERE updated < (updated-((60)*60*24))
END;

How to force an event to execute in MySQL

I am trying to write an automated test script for testing a MySQL Event that I created, and I am wondering if there is a way to execute some kind of command to force the Event to execute immediately. The Event is set to run daily at midnight, but I don't want the automated test script to have to wait for the Event to trigger.
One way I can think of is to ALTER the Event to have it execute one-time 5 seconds into the future, but I'd like to know if there is a more elegant way to do get it to execute.
Move all the code within the event into a stored procedure
Make the event only call the stored procedure
Test the stored procedure with the CALL syntax.
if it is based on the system time, change that, and see if your event fires, then change the system time back.
Another 'proxy' solution is schedule it ver soon, for example:
ON SCHEDULE EVERY 10 SECOND
(or 1 sec, depends how long in the run time of the event).
you could later drop the event.