0

I have three tables:

HouseMode:
mode_id (INT, PK)
switch (CHAR 1BYTE)

ModeDevices:
modedevice_id (INT, PK)
house_mode (INT, FK)
houseroomdevice (INT, FK)

HouseRoomDevices:
houseroomdevice_id (INT, PK)
switch (CHAR 1BYTE)

I would like to have a trigger which updates switches from HouseRoomDevices table after updating switch in HouseMode table.

My trigger:

CREATE OR REPLACE TRIGGER switch
BEFORE UPDATE
ON HouseMode
FOR EACH ROW
BEGIN
  UPDATE houseroomdevices
  SET switch = :NEW.switch
  WHERE EXISTS(SELECT houseroomdevice_id FROM houseroomdevices INNER JOIN modedevices ON houseroomdevice = houseroomdevice_id WHERE house_mode = :NEW.mode_id);
END;

But when I try to update record:

UPDATE HouseMode
SET switch = 1
WHERE mode_id = 1;

It updates all records from HouseRoomDevices table.

1
  • Believe it should be AFTER UPDATE ON HouseMode as per your statement Commented Jun 20, 2015 at 19:08

1 Answer 1

1

Your update statement is wrong, because your WHERE condition is always true. Use this one:

UPDATE houseroomdevices
SET switch = :NEW.switch
WHERE houseroomdevice_id IN  (SELECT houseroomdevice FROM modedevices WHERE house_mode = :NEW.mode_id);
Sign up to request clarification or add additional context in comments.

2 Comments

Pavel, that's a wrong syntax. You will have to use a subquery to achieve that.
Alright, that UPDATE .. JOIN syntax is not supported in Oracle. Moreover, I actually don't see any issue in OP's posted update query except that his ON clause is bit ODD and he supposed to use a AFTER trigger whereas he is using a BEFORE trigger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.