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 ;
?
Related
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 an event that deletes record on a table that are a month old. But it does not seem to execute.
DELIMITER $
CREATE EVENT delete_wykofile_back
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP + INTERVAL 1 DAY
ENDS CURRENT_TIMESTAMP + INTERVAL 5 YEAR
ON COMPLETION PRESERVE
DO
BEGIN
DELETE FROM factory.wyko_file_backup WHERE date_inserted < DATE_SUB(NOW(), INTERVAL 1 MONTH);
END$
DELIMITER ;
What I've done so far:
I have followed and set global event_scheduler to ON based from this question
I've also check this question, but to no avail.
There's also no answer from this question.
The account im using is a superuser based from this.
SHOW EVENTS;
SHOW PROCESSLIST;
I am using MySQL Workbench 6.2. I am running the event with 1 minute interval for testing.
STARTS CURRENT_TIMESTAMP + INTERVAL 1 DAY
needs to be the current date or a past date. Right now its set up to start on 12/4/2015.
I am trying to create a trigger using the date add function in mysql using phpadmin and getting a syntax error; my goal is to automatically add a date in the estimated finish date column ie 'fin_dt' which is 14 days from the date the order is placed. Ie current date plus 14 days. I have come up with the following mysql state but get an error:
DELIMITER $$
CREATE TRIGGER before_insert_orderin BEFORE INSERT ON order_in
BEGIN
SET NEW.fin_dt = SELECT DATE_ADD(CURDATE(), INTERVAL 14 DAY) ;
END $$
Your trigger syntax is wrong. You are missing for each row and also you do not need select to set the value. It should be as
delimiter //
create trigger before_insert_orderin before insert on order_in
for each row
begin
set new.fin_dt = date_add(curdate(),interval 14 day);
end ; //
delimiter ;
Your Trigger syntax near " SET NEW.fin_dt = SELECT DATE_ADD(CURDATE(), INTERVAL 14 DAY)"
SELECT DATE_ADD is Wrong. It should be as
CREATE TRIGGER `before_insert_orderin`
BEFORE INSERT ON `order_in`
FOR EACH ROW
SET NEW.fin_dt=DATE_ADD(NOW(), INTERVAL 14 DAY)
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)
I must have done something really stupid, but the following is correct:
CREATE EVENT delete_old
ON SCHEDULE
EVERY 1 DAY
COMMENT 'Clears old cache data from the DB.'
DO
DELETE FROM summoners
WHERE `date` < (NOW() - INTERVAL 7 DAY);
Where the next bit seems to throw a syntax error on the last 2 lines:
CREATE EVENT delete_old
ON SCHEDULE
EVERY 1 DAY
COMMENT 'Clears old cache data from the DB.'
DO BEGIN
DELETE FROM summoners
WHERE `date` < (NOW() - INTERVAL 7 DAY);
END;
The syntax to my knowledge is correct, however MySQL Workbench does not agree. I intend to do multiple tables inside the BEGIN - END section, that is why i need it.
I hope someone can figure out what goes wrong here, i am at a loss.
Thanks in advance,
Smiley
You have to change the DELIMITER to something that doesn't appear in your event body.
I just tried it (MySQL Workbench 6.0.6, MySQL 5.6) and it works fine. Here's a screenshot:
It's because ; in body breaks your command in the middle. Use different delimiter.
DELIMITER |
CREATE EVENT delete_old
ON SCHEDULE
EVERY 1 DAY
COMMENT 'Clears old cache data from the DB.'
DO BEGIN
DELETE FROM summoners
WHERE `date` < (NOW() - INTERVAL 7 DAY);
END;
| DELIMITER ;