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();
Related
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
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.
I am trying to find the best way to process a record with a timestamp field whereby it would automatically move from one table to another table after 30 minutes.
If you are using MySQL 5.1.6 and later, check out MySQL events.
Events work the same with other routines, you can CREATE, ALTER, and even DROP them.
Martin Psinas has basic examples of this. (Just found online, we're not friends, lol)
First, you'll have to enable events scheduler:
SET GLOBAL event_scheduler = ON;
Then, you can create your event like (base from your question):
CREATE EVENT ProcessRecord
ON SCHEDULE EVERY 30 MINUTE
--STARTS CURRENT_TIMESTAMP + INTERVAL 1 DAY
--ENDS CURRENT_TIMESTAMP + INTERVAL 1 YEAR
DO
BEGIN
INSERT INTO ...
SELECT ...
END
Otherwise, you can go with windows scheduled tasks, if you're using windows platform.
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.
I have added some rows into my ad table in the database, their properties includes created date and expiry date.
I need php script to automatically delete the row that its expiry date has reached.
Pls assist my project.
Thanks u
You could use MySQL's event scheduler to automatically delete such records when they expire:
CREATE EVENT delete_expired_101
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 3 WEEK DO
DELETE FROM my_table WHERE id = 101;
Or to run an automatic purge of all expired records on a regular basis:
CREATE EVENT delete_all_expired
ON SCHEDULE EVERY HOUR DO
DELETE FROM my_table WHERE expiry < NOW();
<?php
$today = getdate();
if($today <= $expirayDate)
{
//your deletion logic
}
?>
The other way would be to filter your select query i-e hide all those fields which aren't expire;
$query = "SELECT * FROM `yourTable` WHERE `expiration` >= NOW();";
What you are looking for is crontab.
Using it, you will be able to schedule scripts to run in determinate times, for example:
Run deleteExpiredRows.php every 5 minutes
Run updateOnlineUsers.php every 2 hours
Cron is very easy to use. Check this quick reference guide
Setting up crontabs at your web hosting:
/path/to/hosting/php /home/user/public_html/script.php
Path to php may vary, so contact your hosting to get this information. More information about crontab can be found here at stackoverflow.
Note: you might want to check if your web hosting service supports crontab.
After this is done, you just need to create your PHP script to delete every row older than current time.