MySql event date limitation? - mysql

am trying to generate an event that delete a table in a DB
my idea is to make that to run every (saying hypotetically) 10 min for ever ( no end date)
but I get an error as following:

The starts and ends parameters of the create event command are timestamps. Currently the upper limit of timestamp in mysql is 2038-01-19 03:14:07.999999'. You can't specify any ends after this point.
If you want the event to run "forever", then do not specify any ends (although I would be surprised if your application would run unchanged beyond 2038...)

Related

At what time will MySQL execute events that are scheduled every DAY?

If I create an event in mysql with the interval every day like this:
CREATE EVENT `event`
ON SCHEDULE EVERY '1' DAY ON COMPLETION PRESERVE
ENABLE COMMENT '' DO
UPDATE table SET something = something + 1;
On what time of the day will it execute? I would assume 0AM but the specification does not mention it.
Yes, I am aware that it is possible to specify specific times, however I would like to keep it simple.
Not specifying STARTS is the same as using STARTS
CURRENT_TIMESTAMP—that is, the action specified for the event begins
repeating immediately upon creation of the event.
from the MySQL Reference

Does mysql can give each row a specified lifetime to be deleted automatically?

I'd like to know if mysql(or mariadb) offers a function for the expiration that a row can be removed automatically in the DB without using any extra scheduler program nor using any SQL like DELETE.
This should happen or define when you create a table so that once INSERT occurs it starts to manage it.
There are many related questions here:
MySQL how to make value expire?
Remove Mysql row after specified time
MySQL give rows a lifetime
However, I couldn't find the answer. I am not curious about using WHERE nor DELETE.
Is it even possible?
Yes for same you can create an event by this way
CREATE EVENT lifetime ON SCHEDULE
EVERY 1 DAY STARTS '14:05:44' ENDS '14:05:46'
ON COMPLETION NOT PRESERVE
ENABLE
DO BEGIN
// put your delete query here with where clause by calculate your exp date
END

How to reset a column value in MySQL after it's been set to a certain value after 1 hour?

Hi I am quite new to MySQL events and triggers, I have a table called userstatus(userid, timestamp,count, flag) which is used to store data about the user if he's blocked or not. When count reaches 4, the flag becomes 0(blocked). Can you please help me on what I can do to automatically reset the flag to 1 after it remains blocked for an hour.
I have tried the following code:
create event testevent
on schedule
every 1 minute
starts CURRENT_TIMESTAMP
do
update demo.userstatus set flag=1
where timestampdiff(hour,timestamp,CURRENT_TIMESTAMP)>1
This seems to work but is not efficient. Any help would be appreciated. Thanks!
It would be a lot more efficient if you added a datetime field to the usersratus table that contains the date and time until a user is blocked, or use the existing timestamp field that stores when the user was blocked rather than having just a flag.
During login or any activity you deem to fit, you just check if the current time is on or earlier than the block time (or on or before timestamp + 1 hour). If yes, then the user is blocked and you can prevent the activity. If not, then the user is not blocked and can allow the activity. You can even restore the counter at the first successful activity to 1.
This way you avoid the need to periodically check the database and clear the status flag.

MySQL - Subtract one from value every minute

I would like to set up MySQL so that it can subtract one from all values in a row every minute. Once that value equals 0, I would like MySQL to delete that row, and execute a query which adds a row to another table. Before I ask how this can be done, I first would like to know if such a thing is even possible. I have looked around on Google using the search term "automatic run sql query", and unless I am incorrectly understanding what I have read, it is possible, but how can I do something like what I want? What I want would need an if statement to work, but does MySQL even have such a thing as if statements?
You can use the recurrent option in the MySQL event scheduler. You an set it to run every min and then have your logic in it.
http://dev.mysql.com/doc/refman/5.1/en/events-overview.html
Using recurrent event will help you to accomplish this. Create an event to execute every minute.
CREATE EVENT subtractRow
ON SCHEDULE
EVERY 1 MINUTE
STARTS NOW
ON COMPLETION PRESERVE
COMMENT 'Row Subtraction'
DO
-- WRITE QUERY TO SUBTRACT ROW VALUE HERE

MYSQL, Event not running

I am trying to run the MYSQL event at a particular time, and to be repeated after 24 hours:
ALTER EVENT myeventToronto1
ON SCHEDULE EVERY 24 HOUR
STARTS '2013-08-04 02:02:00'
COMMENT 'A sample comment.'
DO
INSERT into `smallworksdb`.`eventtesttable`(ID,NAME) Values(1,'someValue');
When I save the above event, the table is not getting populated at a particular time specified in the event, so I am assuming that the event does not get run.
What am I missing here.
Thanx
Why do I have this string feeling that id is declared as a primary key (or at least unique)? And you are trying to insert the same value every day. The problem would be an error generated by SQL, not that the job is not running.
Typically a field like id would be an auto_increment value that would be set automatically on insert, and not given a particular value.
This is just speculation. There are other possibilities like the scheduler is turned off or the computer reboots at exactly that time every day?