Thursday, October 18, 2012

MySQL Event Scheduler

MySQL Event Scheduler

When you are running a large MYSQL applications, you have handle lots of unwanted data rows. it will be created problem in database overload. So that I want to explain how to delete such data in regular intervals using MySQL event scheduler.It also can be used to update data base table auto backup etc. 
I have used table customer used in previous lessens in my_database create it using following MYSQL dump

CREATE TABLE IF NOT EXISTS `customer` (
  `customer_id` int(11) NOT NULL AUTO_INCREMENT,
  `customer_name` varchar(100) NOT NULL,
  `product_id` int(11) NOT NULL,
  PRIMARY KEY (`customer_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

--
-- Dumping data for table `customer`
--

INSERT INTO `customer` (`customer_id`, `customer_name`, `product_id`) VALUES
(1, 'Harfer', 2),
(2, 'Taylor', 3),
(3, 'Jones', 1),
(4, 'Lopez', 4),
(5, 'Bayker', 5),
(6, 'wonne', 8),
(7, 'hare', 9),
(8, 'Martin', 3);

Then move the my_database and open SQL tab and write following query to delete the 2nd record in table customer in 2 minutes. 
SET GLOBAL event_scheduler = ON; CREATE EVENT myEvent ON SCHEDULE EVERY 24 HOUR STARTS CURRENT_TIMESTAMP + INTERVAL 2 MINUTE DO DELETE FROM cart WHERE id=2;


No comments:

Post a Comment