我有以下表格:
Piers(PierID, Name, NumOfShips)
Ships(ShipID, Name)
Docking(PierID, ShipID)
我需要实现一个触发器,它将计算每个码头包含的船只数量,并将结果插入NumOfShips
。
答案 0 :(得分:0)
您需要AFTER UPDATE
触发器
CREATE OR REPLACE TRIGGER change_on_Ships
AFTER UPDATE ON Ships
BEGIN
UPDATE Piers
SET NumOfShips = (
SELECT count(1) FROM Docking d
WHERE d.PierID = (
SELECT PierID FROM Docking where ShipId = :NEW.ShipID
)
)
WHERE PierID = (select PierID from Docking where ShipID = :NEW.ShipID );
END;
希望有所帮助