sql - MySQL BEFORE INSERT trigger to turn duplicate primary keys inserts into updates -
i'm trying execute query in database through phpmyadmin
create trigger avoid_duplicated_sharing before insert on sharingevents each row begin if ( select count(*) sharingevents shared_note_id = new.shared_note_id , shared_to = new.shared_to > 0 ) delete sharingevents shared_note_id = new.shared_note , shared_to = new.shared_to end if; end
but phpmyadmin gives me following error:
mysql said: #1064 - have error in sql syntax; check manual corresponds mariadb server version right syntax use near 'end if' @ line 7
two questions:
- what's wrong script?
- after
before insert
trigger,insert
operation performed? in case doesn't have removeinsert sharingevents (select * new);
i solve following code:
delimiter $$ create trigger avoid_duplicated_sharing before insert on sharingevents each row begin if ( select count(*) sharingevents shared_note_id = new.shared_note_id , shared_to = new.shared_to > 0 ) delete sharingevents shared_note_id = new.shared_note_id , shared_to = new.shared_to; end if; end$$
the problem delimiter.
even so, trigger doesn't work. when application inserts duplicated primary keys mysql throws following error:
#1442 - can't update table 'sharingevents' in stored function/trigger because used statement invoked stored function/trigger.
Comments
Post a Comment