Reputation:
If I want to limit number of entries per user in this table:
CREATE TABLE `connections` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) unsigned NOT NULL,
`word_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `No duplicates` (`user_id`,`word_id`),
KEY `word_id` (`word_id`),
CONSTRAINT `connections_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `connections_ibfk_2` FOREIGN KEY (`word_id`) REFERENCES `words_en` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And this limit can change and I store it in another table called settings.
Should I do first SELECT and than do php compare and than INSERT. or is there a mechanism to do this kind of operations directly in INSERT query?
Upvotes: 1
Views: 67
Reputation: 26804
DELIMITER $$
create
trigger `some trigger` AFTER INSERT
on table
for each row begin
if (select count(id) from someothertable)>=100 then
delete from table WHERE id = NEW.id -99;
end$$
DELIMITER ;
I`m afraid I`m out of my depth,I think I could do it given enough time for research,but you`d better ask somebody more informed.Here is my half-assed trigger,not tested.
There is also Create Event in mysql you could use.http://www.sitepoint.com/how-to-create-mysql-events/
Upvotes: 1