Max Frai
Max Frai

Reputation: 64326

MySQL Trigger converting

I have such SqLite trigger:

CREATE TRIGGER update_rating AFTER UPDATE ON gameServers 
FOR EACH ROW BEGIN UPDATE gameServers 
SET rated_order=NEW.rating || '#' || NEW._address 
WHERE rowid=NEW.rowid; END;

Help me, please, with converting it into MySQL.

Upvotes: 0

Views: 283

Answers (1)

Mörre
Mörre

Reputation: 5723

CREATE TRIGGER update_rating BEFORE UPDATE ON gameServers
FOR EACH ROW SET NEW.rated_order=CONCAT(NEW.rating,' # ',NEW.address);

OF COURSE - this does nothing ON INSERT...(!)

Note that I changed it from AFTER to BEFORE (quite deliberately): Apart from the question why I should start another UPDATE after the one triggering the trigger, there is the issue Updating table in trigger after update on the same table Your (full) "UPDATE" statement in your AFTER trigger would cause a circular trigger-calling-trigger-calling-trigger... (which mysql would prevent by refusing the statement)

EDIT: At first I wanted to use the '||' too for string concatenation, but this is MySQL and not Oracle :)

It should work, I actually tested it on one of my own tables just to be sure. This trigger stuff is fickle at times :)

Upvotes: 1

Related Questions