I have a trigger:
CREATE TRIGGER BEFORE_DELETE_ON_SENTIERO__HA__TAPPA
BEFORE DELETE ON SENTIERO__HA__TAPPA
FOR EACH ROW
BEGIN
DECLARE temp_prima_tappa INTEGER;
DECLARE temp_ultima_tappa INTEGER;
-- NOW THIS QUERY WILL FAIL; it checks if the record that has to be deleted is in the table
IF NOT EXISTS ( SELECT *
FROM SENTIERO__HA__TAPPA as sht
WHERE OLD.IDsentiero=sht.IDsentiero and OLD.IDtappa=sht.IDtappa) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'DELETE: La tappa indicata non è nel sentiero; impossibile elimnarla';
END IF;
IDsentiero and IDtappa are PK of of their relative tables: IDsentiero for SENTIERO, IDtappa for TAPPA.
The problem is that the DELETE QUERY doesn’t reply me with that error message; simply it let runs DELETE QUERY, although it cannot delete anything, because that record doesn’t exist in the table.
The problem is that query.. i’m sure but i cannot find the bug.
Something like this should work: