First, create the trigger:
CREATE TRIGGER UpdateSentiero_AfterUpdatingTappa
AFTER UPDATE ON TAPPA
FOR EACH ROW
BEGIN
//NEW.tempo and OLD.tempo would refers to TAPPA "tempo" and "lunghezza", really? I ask you this because SENTIERO has it own "tempo" and "lunghezza"
IF (NEW.tempo is not null) and NOT (NEW.tempo=OLD.tempo) THEN
UPDATE SENTIERO
SET tempo=0;
END IF;
END$$
Then, run the query:
UPDATE SENTIERO
SET tempo=null;
It seems the TRIGGER doesn't work and i don't know the reason.
NEW.tempo and OLD.tempo could be NULL (i suppose they refers to TAPPA, because it's a TRIGGER ON TAPPA) that's the reason for i've written that IF condition.
EDIT ::
One INSTANCE of SENTIERO has multiple INSTANCES of TAPPA, so one INSTANCE of SENTIERO collect the values of it own "tempo" and "lunghezza" TAPPA INSTANCES
IF (NEW.tempo is not null) and NOT (NEW.tempo=OLD.tempo) THEN
IF (OLD.tempo is NOT NULL) THEN
UPDATE SENTIERO
SET tempo=tempo - OLD.tempo + NEW.tempo
WHERE (tempo is NOT NULL) and IDsentiero IN ( SELECT DISTINCT IDsentiero
FROM SENTIERO__HA__TAPPA AS sht
WHERE NEW.IDtappa=sht.IDtappa);
ELSE
UPDATE SENTIERO
SET tempo=tempo + NEW.tempo
WHERE (tempo is NOT NULL) and IDsentiero IN ( SELECT DISTINCT IDsentiero
FROM SENTIERO__HA__TAPPA AS sht
WHERE NEW.IDtappa=sht.IDtappa);
UPDATE SENTIERO
SET tempo=NEW.tempo
WHERE (tempo is NULL) and IDsentiero IN ( SELECT DISTINCT IDsentiero
FROM SENTIERO__HA__TAPPA AS sht
WHERE NEW.IDtappa=sht.IDtappa);
END IF;
END IF;
But when I try to run
UPDATE TAPPA
SET tempo=9
WHERE IDtappa=0;
//where instance of IDtappa=0 has "tempo"'s value=NULL
it doesn't work
TABLES DATA:
SENTIERO | SENTIERO_HA_TAPPA | TAPPA
IDsentiero time | IDsentiero IDtappa | IDtappa time
0 7.5 | 0 0 | 0 null
| 0 1 | 1 1.45
| 0 2 | 2 2.3
| 0 5 | 5 1.45
| 0 8 | 8 2.3
The first time (update on tappa, set tempo=2 where IDtappa=0) the trigger doesn't work.
NOTE: OLD.tempo is NULL, SENTIERO.tempo is not null
But the second time (update on tappa, set tempo=3 where IDtappa=0) the trigger works.
The derived data calculated on SENTIERO's "tempo", for IDsentiero=0, HAS CHANGED: 8.5
It seems that it has done: 7.5 - 2 +3, so it seems it has been taken the right condition.
NOTE: OLD.tappa is NOT NULL, SENTIERO.tempo is NOT NULL
So the problem is with the NULL value of TAPPA.tempo