I have this trigger
CREATE TRIGGER checkcollision AFTER UPDATE ON players BEGIN
SELECT RAISE(ABORT, 'collision') FROM walls WHERE NEW.x=x AND NEW.y=y;
END;
mysql 5.1.72-0ubuntu0.10.04.1 (Ubuntu)
But I am getting a syntax error, and I don't see where…
EDIT:
DELIMITER //
CREATE TRIGGER checkcollision AFTER UPDATE ON players BEGIN SELECT RAISE(ABORT, 'collision') FROM walls WHERE NEW.x=x AND NEW.y=y; END//
DELIMITER ;
this still got a syntax error
EDIT2:
I think I need the old syntax from here:
https://dev.mysql.com/doc/refman/5.0/en/commit.html
I don't know how to complete the code though…
START TRANSACTION;
SELECT p.* FROM players p WHERE EXISTS (SELECT w.* FROM walls w WHERE w.x=p.x AND w.y=p.y);
COMMIT;
ROLLBACK;
Best Answer
For 5.5 and later it is possible to use signals:
For 5.1 and earlier version there is no support for signal. You could try to mimic it with an forced exception, like division by zero or by referencing something that does not exist. You don't get a nice error message though:
A slightly more elegant way is to use an exit handler which is supported in 5.1, still no error message though:
If no wall is found an empty exit handler is invoked, otherwise the trigger continues and an deliberate error is made.
By adding a dummy table like:
We can force a primary key violation by inserting the same value twice from the trigger:
We will get an error message like (tested in 10.0.20-MariaDB):
You might want to encapsulate this into a stored procedure:
which can be called from the trigger: