最近需要删除MySQL数据库中固定时间之前的数据。
需要实现的功能:每天自动删除三天之前的数据行
现有环境:
MYSQL8.0
数据库:changsan
数据表:py_url
具体实现代码为:
CREATE EVENT AutoDeleteOldNotifications
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
ON COMPLETION PRESERVE
DO
DELETE FROM changsan.py_url WHERE c_data < DATE_SUB(NOW(), INTERVAL 3 DAY);
参考资料:
http://www.mysqltutorial.org/mysql-triggers/working-mysql-scheduled-event/
http://www.w3school.com.cn/sql/func_datediff.asp
https://stackoverflow.com/questions/32507258/stored-procedure-that-automatically-delete-rows-older-than-7-days-in-mysql
https://stackoverflow.com/questions/9472167/what-is-the-best-way-to-delete-old-rows-from-mysql-on-a-rolling-basis