Ano, musíte vytvořit after insert trigger
za to
delimiter //
create trigger total_votes_count after insert on votes
for each row
begin
if (new.value == 1) then
update posts set total_votes = total_votes+1
where id = new.id_post;
elseif (new.value == -1) then
update posts set total_votes = total_votes-1
where id = new.id_post;
end if;
end;//
delimiter //
Pro manipulaci s aktualizací zůstává vše stejné, jen potřebujete další spouštěč jako
delimiter //
create trigger total_votes_count_upd after update on votes
for each row
begin
if (new.value == 1) then
update posts set total_votes = total_votes+1
where id = new.id_post;
elseif (new.value == -1) then
update posts set total_votes = total_votes-1
where id = new.id_post;
end if;
end;//
delimiter //
Protože máte 2 tabulky příspěvků, budete je muset použít v podmínce if
delimiter //
create trigger total_votes_count after insert on votes
for each row
begin
if (new.value == 1) then
if (new.table_name == 'post_A') then
update posts_A set total_votes = total_votes+1
where id = new.id_post;
else
update posts_B set total_votes = total_votes+1
where id = new.id_post;
end if;
elseif (new.value == -1) then
if (new.table_name == 'post_A') then
update posts_A set total_votes = total_votes-1
where id = new.id_post;
else
update posts_B set total_votes = total_votes-1
where id = new.id_post;
end if ;
end if;
end;//
delimiter //
Udělejte totéž pro spouštěč aktualizace.