Skip navigation links

MySQL Forums :: Triggers :: limit number of rows using a trigger


Advanced Search

limit number of rows using a trigger
Posted by: Darren White ()
Date: March 13, 2012 03:50AM

I want a (log) table to grow either for a certain number of rows or for a certain number of days. Then when I reach that limit, delete the oldest row before inserting a new. As with a roatating buffer.

As a test I made the following table:
delimiter $$

CREATE TABLE `a_test` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`a` int(11) NOT NULL DEFAULT '0',
`b` int(11) NOT NULL DEFAULT '0',
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1 MAX_ROWS=5$$

Then I made an attempt at the trigger


-- Trigger DDL Statements
DELIMITER $$
USE `test`$$
CREATE
DEFINER=`root`@`localhost`
TRIGGER `test`.`log_limit`
BEFORE INSERT ON `pgs`.`a_test`
FOR EACH ROW

BEGIN

SELECT COUNT(*) FROM a_test;

IF COUNT >= 5 THEN
DELETE FROM a_test ORDER BY last_update LIMIT 1;
END IF;

END$$


But I get this error:
SQL Error 1415: Not allowed to return a result set from a trigger



Edited 1 time(s). Last edit at 03/13/2012 03:55AM by Darren White.

Options: ReplyQuote


Subject Views Written By Posted
limit number of rows using a trigger 2861 Darren White 03/13/2012 03:50AM
Re: limit number of rows using a trigger 1064 Darren White 03/13/2012 03:59AM
Re: limit number of rows using a trigger 859 Darren White 03/13/2012 04:09AM
Re: limit number of rows using a trigger 1166 Darren White 03/13/2012 04:11AM
Re: limit number of rows using a trigger 1078 irek kordirko 03/13/2012 11:50AM
Re: limit number of rows using a trigger 747 Darren White 03/14/2012 05:32AM
Re: limit number of rows using a trigger 1266 irek kordirko 03/14/2012 11:48AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.