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.
Related
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'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();
I have a MySQL table called: "regkeys". This table has two columns: "keyCat" and "keyNum". In my web-app I have a keygen module that adds a key(1134fb) and a category(mds or dts, etc.) into this table.
What I want to accomplish is this: after issuing (adding into the db) a key, I'd like to create a MySQL event to delete the keys that stay/stayed in my table longer than 2 days (or so), this way expiring the keys (for example: - from the time it was created, counting 2 days, each key has to be deleted, having certain keys getting deleted sooner or later than others, depending on when they were created). I looked at the MySQL's API, but I need some help with the logic. I do not know how to tell the event to delete only the keys that were stored in for 2 days (or so). I was hoping somebody could give me a quick example or direct me to a clear tutorial.
Thank you very much in advance.
Edited: I think I found this other question that helps a bit with my problem. I think I was going about it the wrong way (key based). Since every key and key category get inserted into a row, the scheduler should deal with rows instead of keys.
This is the solution:
Enabled the event_scheduler in the db like this: SET GLOBAL event_scheduler = ON;
Added a timestamp column in my table using MySQL default values
I created an event, CREATE EVENT <name>
Called the event to run on a schedule like so: ON SCHEDULE EVERY 20 SECOND
Add the SQL query to the event: DO DELETE FROM <table_name> WHERE <time_stamp_column> < NOW() - INTERVAL 5 MINUTE.
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).
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.