cron job/mysql event task - mysql

I'm completely clueless as to do a cron job/mysql event task for some things in my database to be updated automatically every 24 hours.
Basically here's the 2 queries I need to be ran every 24 hours as soon as users_stats DailyRespectPoints hits '0'.
UPDATE user_stats SET DailyPetRespectPoints = '3' and
UPDATE user_stats SET DailyRespectPoints = '3'
Those are what needs to be ran so that when someone uses all 3 of their DailyRespectPoints, it refills back to 3 in 24 hours.
Thanks!

you could create trigger , which will be executed automatically when DailyRespectPoints hits '0'.
you can read more about the triggers here http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
or you can write more details about your database/query-s and we can help you write the trigger

First write a script that will perform the work you need done. Based on your question, you will check some table value, then execute your two UPDATE steps.
After your script is tested and performs correctly, you can schedule it to run every day at some point in time using cron (probably crontab).

Related

Perform delete a perticular records after some interval of time in mysql

DROP EVENT `deleteTestEntries`;
CREATE DEFINER=`root`#`localhost` EVENT `deleteTestEntries`
ON SCHEDULE EVERY 1 MINUTE
STARTS '2018-05-25 18:17:01'
ON COMPLETION NOT PRESERVE ENABLE DO
DELETE FROM lead_master WHERE lead_master.lname LIKE '%Test%'
What is wrong in above event.
It has been created with no errors but not performing any action.
I simply want to delete the records from my lead_master table where lname is 'Test'
Go into my.ini file and add this line,most probably this is the issue.
event_scheduler = on
Restart mysql.Apparently you can even set it on the fly
I finally did it with cron jobs.
I created a controller which calls a model function contains query to delete record.
and set the cron job which calls the controller after specific interval of time.
I am guessing that you don't need an event every minute. Just define a view to filter out the records you don't want:
create view v_lead_master as
select lm.*
from lead_master lm
where lm.lname not like '%Test%';
Then schedule a job or event every day at a slow period to delete the rows you want. Your application should use the view. Your testing should use the base table.

Trigger that delete row if currentdatetime > datetime+5

I'm creating a database where i want to create a trigger.
This trigger will delete a row if the current time is 5 min over the requesttime in the table.
The requesttime is a datetime attribute in the table.
How can i make a trigger which do this?
You don't want to do this. First, triggers only run when something is happening inside the database (such as an insert or update). They are not the same as scheduled jobs. Second, you generally don't want to do this in a scheduled job. Instead, just create a view:
create view v_table as
select t.*
from table t
where requesttime >= now() - interval 5 minute;
This will return valid requests.
Then, at your leisure, delete the old rows -- if you really need to. There is a good chance you might want to keep them around to know what happened in the past. But you can delete them on Sunday morning at 3:00 a.m. or some other slow time, if you want.
You may want to consider Event Scheduler added in MySQL 5.1.6. Events can be seen as an alternative to Cronjob.
Their main advantages are:
Platform independent; directly written in MySsql
Ability to list all events SELECT * FROM INFORMATION_SCHEMA.EVENTS;
Built in error logging option
Syntax is something similar to:
CREATE EVENT my_event
ON SCHEDULE
EVERY 1 MINUTE
STARTS '2015-01-01 00:15:00'
COMMENT 'Removes forgotten password tokens older thank 1 week.'
DO
DELETE FROM my_table WHERE some_timestamp > NOW();

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

Triggers in MySQL

I have database of Real Estate. In that I want to delete a record after particular time event say 22 days. I want to write trigger that will delete record from table automatically after 22 days.
Something like this:
CREATE EVENT CheckDeletes
ON SCHEDULE
EVERY 1 DAY
DO
DELETE FROM YourTable WHERE DATEDIFF(NOW(), YourField) > 22
http://dev.mysql.com/doc/refman/5.1/en/create-event.html
Good luck.
Triggers fire immediately based on some particular action in the database. What you will probably need to to do is to create a script that you can run at scheduled intervals (via cron or similar) in order to look for records older than 22 days or whatever and delete them.

Is there any way to implement a time based trigger in Mysql 5.1?

Is there any way to implement a time based trigger in Mysql 5.1
i.e. it must run at 12H05 every day
Edit
A corn job calling an SQL script is currently been used -
but I am looking for a less complicated solution.
To do this with Mysql 5.1.x use the following code:
CREATE EVENT myevent
ON SCHEDULE
EVERY 6 HOUR
COMMENT 'A sample comment.'
DO
UPDATE myschema.mytable SET mycol = mycol + 1;
Here link to the users manual
Unless triggers in MySQL are different than in most other RDBMS, a time based trigger makes no sense. Triggers are fired in reaction to rows being added/inserted/deleted in a table. If you want a time based operation to occur, you should be considering a Job (in SQL Server) or Windows Service.