0

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

4

1 回答 1

0

Your query doesn't fire the trigger. Here's why:

CREATE TRIGGER UpdateSentiero_AfterUpdatingTappa
AFTER UPDATE ON TAPPA

The trigger only fires after updates are made to table TAPPA.

UPDATE SENTIERO
SET tempo=null;

Your test query is updating table SENTIERO, and setting tempo to null, which is filtered out by the trigger's IF conditional. Try updating TAPPA instead, like this:

UPDATE TAPPA
SET tempo=5;

This query should cause SENTIERO's tempo values to get set to zero, assuming there's a tempo value != 5 in that table.

Note that it appears your trigger will affect every row in SENTIERO. Perhaps you should include some kind of ID matching between the two tables? For example:

UPDATE SENTIERO 
SET tempo=0
WHERE SENTIERO.ID = NEW.ID;

In response to new question:

Your IF statement:

IF (NEW.tempo is not null) and NOT (NEW.tempo=OLD.tempo) THEN

Your test query:

UPDATE TAPPA
SET tempo=9
WHERE IDtappa=0;
//where instance of IDtappa=0 has "tempo"'s value=NULL

Given the comment, it's clear the IF block isn't being entered. You're comparing a non-null value against null. This yields null. You need to use the null safe equality operator, <=>. Your IF statement should look like this instead:

IF (NEW.tempo is not null) and NOT (NEW.tempo<=>OLD.tempo) THEN
于 2013-06-05T16:06:40.093 回答