Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autorunning Query in mysql

Is it possible to make a stored procedure that run every night 11 pm , check in table if any record is modified for last six month, If some record is modified for last six month I have to delete it from table. This has to run automatically without use of any external language.

like image 285
John Christy Avatar asked Dec 14 '12 04:12

John Christy


People also ask

What is autocommit in MySQL?

If autocommit mode is enabled, each SQL statement forms a single transaction on its own. By default, MySQL starts the session for each new connection with autocommit enabled, so MySQL does a commit after each SQL statement if that statement did not return an error.

What is auto rehash MySQL?

--auto-rehash. Enable automatic rehashing. This option is on by default, which enables database, table, and column name completion. Use --disable-auto-rehash to disable rehashing. That causes mysql to start faster, but you must issue the rehash command or its \# shortcut if you want to use name completion.


2 Answers

CREATE EVENT IF NOT EXISTS `my_old_data_cleaner_event`
ON SCHEDULE EVERY 23 DAY_HOUR
COMMENT 'Clean up Service Start at 11:00PM daily!'
DO DELETE FROM my_table WHERE created_date < (NOW() - INTERVAL 1 MONTH);

OR for Stored Procedure.

CREATE EVENT IF NOT EXISTS `my_old_data_cleaner_event`
ON SCHEDULE EVERY 23 DAY_HOUR
DO CALL my_sp_cleanup_old_data();
like image 122
Talha Ahmed Khan Avatar answered Oct 24 '22 00:10

Talha Ahmed Khan


you can achieve with mysql event scheduler--

http://dev.mysql.com/doc/refman/5.1/en/events.html

detail blog: http://goo.gl/6Hzjvg

like image 24
Suresh Kamrushi Avatar answered Oct 24 '22 00:10

Suresh Kamrushi