PL / SQL - 触发计数

时间:2018-01-05 11:57:55

标签: plsql triggers

我有以下表格:

Piers(PierID, Name, NumOfShips)
Ships(ShipID, Name)
Docking(PierID, ShipID)

我需要实现一个触发器,它将计算每个码头包含的船只数量,并将结果插入NumOfShips

1 个答案:

答案 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;

希望有所帮助