我有三张桌子:
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)
我希望在更新HouseRoomDevices
表中的开关后有一个更新HouseMode
表开关的触发器。
我的触发器:
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;
但是当我尝试更新记录时:
UPDATE HouseMode
SET switch = 1
WHERE mode_id = 1;
它会更新HouseRoomDevices
表中的所有记录。
答案 0 :(得分:1)
您的更新语句错误,因为您的WHERE条件始终为true。使用这个:
UPDATE houseroomdevices
SET switch = :NEW.switch
WHERE houseroomdevice_id IN (SELECT houseroomdevice FROM modedevices WHERE house_mode = :NEW.mode_id);