Trying to create a trigger that will delete any record that is 90 days old. I used a trigger statement from stackoverflow that I have found and changed the statement a bit, but in my MySQL Workbench, I am getting a syntax error. I cannot seem to figure what is wrong.
Below is my query:
create trigger user_connections_dump after insert on user_connections
for each row
begin
delete from connection_time where Date('2014-06-09') > Date('now','-90 days')
end;
Your need looks more like an Event than a Trigger.
CREATE EVENT IF NOT EXISTS `Clean_Older_Than_90_days_logs`
ON SCHEDULE
EVERY 1 DAY_HOUR
COMMENT 'Clean up log connections at 1 AM.'
DO
DELETE FROM log
WHERE log_date < DATE_SUB(NOW(), INTERVAL 90 DAY)
References:
MySQL Event Scheduler on a specific time everyday
CREATE TRIGGER user_connections_dump
AFTER INSERT ON user_connections
FOR EACH ROW
DELETE FROM log
WHERE log_date < DATE_SUB(NOW(), INTERVAL 90 DAY)
You should be comparing the date column in the log table, not a literal date. Then you use DATE_SUB to subtract dates, not the Date function.
CREATE EVENT IF NOT EXISTS `Delete_Older_Than_90_Days`
ON SCHEDULE EVERY 1 DAY
STARTS STR_TO_DATE(DATE_FORMAT(NOW(),'%Y%m%d 0100'),'%Y%m%d %H%i') + INTERVAL 1 DAY
DO
DELETE LOW_PRIORITY FROM log WHERE log_date < DATE_SUB(NOW(),INTERVAL 90 DAY)
Related
I want to delete rows from the database that are older than 7 days. I went through some questions here on Stack but can't find one that helped at all.
I already found this way to do it:
DELETE FROM users WHERE updateDt < (NOW() - INTERVAL 7 DAY)
And this, to create a event in the database:
DELIMITER $$
CREATE EVENT `delete7DayOldMessages`
ON SCHEDULE EVERY 1 DAY STARTS '2015-09-01 00:00:00'
ON COMPLETION PRESERVE
DO BEGIN
delete from users
where datediff(now(),updateDt)>6;
END;$$
DELIMITER ;
Is it right to transform these two examples above into this:
DELIMITER $$
CREATE EVENT `delete7DayOldMessages`
ON SCHEDULE EVERY 1 DAY STARTS '2015-09-01 00:00:00'
ON COMPLETION PRESERVE
DELETE FROM users WHERE updateDt < (NOW() - INTERVAL 7 DAY)
DELIMITER ;
?
How to create event Auto-Delete some row 1 day after inserting that row?
I have 3 fields:
id varchar
name varchar
timestamp current_timestamp()
Please take a look at Event: https://dev.mysql.com/doc/refman/5.7/en/events-syntax.html
in your case:
you can try this one:
-- create a timer to update this table automatically
DROP EVENT IF EXISTS `et_update_your_trigger_name`;
CREATE EVENT `et_update_your_trigger_name` ON SCHEDULE EVERY 1 MINUTE
STARTS '2010-01-01 00:00:00'
DO
DELETE FROM `DB_NAME`.`table_name` where DATEDIFF(now(),`timestamp`) > 1;
ALTER EVENT `et_update_your_trigger_name` ON COMPLETION PRESERVE ENABLE;
this sql code create a trigger, and execute every minutes.
Here's your scripts.
Insert into table1 (id, name, timestamp) values (1, 'test', now())
after insert
Delete from table1 WHERE timestamp < now() - interval 1 day;
Please try like this:
DELETE FROM table1
WHERE date < DATE_SUB(NOW(), INTERVAL 1 DAY)
I have the following MySql event that works perfect but I would like to validate if the record was inserted before the delete just to make sure that I won't loose the record.
This is the MySql event:
DELIMITER |
CREATE
EVENT IF NOT EXISTS respaldoRegRawInformeAperturaLocal
ON SCHEDULE EVERY 1 DAY STARTS '2017-08-03 22:00:00'
DO BEGIN
-- INSERT INTO BACKUP TABLE
INSERT INTO regRawInformeAperturaLocalBACKUP (regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId)
-- GET DATA
SELECT regRawInformeAperturaLocalId, rawInformeAperturaLocal, done, date, deviceId
FROM regRawInformeAperturaLocal
WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
-- DELETE DATA FROM ORIGINAL TABLE
DELETE FROM regRawInformeAperturaLocal WHERE date < DATE_SUB(NOW(), INTERVAL 5 MONTH);
END|
DELIMITER ;
Is there any way to validate if the records were inserted before the delete?
I have a MySQL table named registered where one of my rows named time is of type timestamp and I wanted to add an EVENT where all the entries in this table are deleted if they are older than one day. I have this so far...
CREATE EVENT delete_registration_data
ON SCHEDULE EVERY 1 DAY
DO
DELETE FROM `registered` where time < DATE_SUB(CurDate(), INTERVAL 1 DAY);
What I noticed however is that the timestamp datatype is made up of the CURDATE() and CURTIME() e.g. 2008-11-11 12:45:34. Would this cause a problem for the EVENT handler to delete from the table?
CREATE EVENT delete_registration_data
ON SCHEDULE EVERY 1 DAY
DO
DELETE FROM `registered` where date(time) < DATE_SUB(CurDate(), INTERVAL 1 DAY);
I want to delete some messages from my MySQL database after 7 days.
My message table rows have this format:
id | message | date
The date is a timestamp in the normal format; 2012-12-29 17:14:53
I was thinking that an MySQL event would be the way to go instead of a cron job.
I have what I guess is a simple question to an experienced SQL person, how do I code the delete messages portion in brackets below?
An example would be appreciated, Thanks.
DELIMITER $$
CREATE EVENT delete_event
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
ON COMPLETION PRESERVE
DO
BEGIN
DELETE messages WHERE date >= (the current date - 7 days);
END;
$$;
You can try using this condition:
WHERE date < DATE_SUB(NOW(), INTERVAL 7 DAY)
So that the whole SQL script looks like this:
CREATE EVENT delete_event
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
ON COMPLETION PRESERVE
DO BEGIN
DELETE messages WHERE date < DATE_SUB(NOW(), INTERVAL 7 DAY);
END;
However, on your place I would solve the given problem with a simple cron script. The reasons to do this is simple: it's easier to maintain the code, no ugly SQL workarounds, integrates smoothly with your system.
This should do the trick.
DELETE FROM messages WHERE date < (CURDATE() - INTERVAL 7 DAY);
For those out there who are on a shared hosting, like 1and1's, and can't create events, an alternative is to use webcron
You just need to tell webcron the url of the php script you'd like to be run, and they'll trigger it for you at the intervals you want