Create Trigger to delete rows that are >90 days old

谁都会走 提交于 2019-12-21 05:51:35

问题


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;

回答1:


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




回答2:


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.




回答3:


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)


来源:https://stackoverflow.com/questions/24167613/create-trigger-to-delete-rows-that-are-90-days-old

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!