Breaking circular reference will required another table e.g :
CREATE TABLE `trigger_semaphore` (
`id` INTEGER(11) NOT NULL,
`semaphore` INTEGER(11) DEFAULT NULL,
`actions` VARCHAR(20) COLLATE utf8_general_ci DEFAULT NULL,
`random` INTEGER(11) DEFAULT NULL,
PRIMARY KEY (`id`)
)ENGINE=MyISAM
CHARACTER SET 'latin1' COLLATE 'latin1_swedish_ci';
CREATE DEFINER = 'root'@'localhost' TRIGGER `trigger_semaphore_before_upd_tr`
BEFORE UPDATE ON `trigger_semaphore`
FOR EACH ROW
BEGIN
DECLARE semaphore int;
set New.random=RAND()*1000;
if New.actions='++' then
set New.semaphore=COALESCE(New.semaphore,0)+1;
elseif New.actions='--' then
set semaphore= COALESCE(New.semaphore,0);
if semaphore<=0 then
set New.semaphore=semaphore;
else
set New.semaphore=semaphore-1;
end if;
end if;
END;
In triggers for the tables which update each other use trigger_semaphore table
in this way:
update trigger_semaphore set actions='++' where id=1;
select semaphore into _semaphore from trigger_semaphore where id=1;
if _semaphore=1 then
# ...... Here goes the code without circular reference..........
end if;
Release the semaphore now at the end of trigger
update trigger_semaphore set actions='--' where id=1;
In short, the table which sets semaphore first (1) will only able to execute certain
statements. You can use a simple method once you get the idea ;)