我有两个表Product(Productname VARCHAR(10),Rating REAL)和Review(RatingID INT,Productname VARCHAR(10),Rating INT)。 产品的评级是该特定产品评论中给出的所有评级的平均值 我希望有一个触发器,每当插入或更新新的评论时都会更新产品,但我无法理解如何做到这一点。 是否有一些db2等同于SQL Server中的inserted()?
我对这个微弱的问题道歉。
答案 0 :(得分:4)
DB2 After Insert触发器:
CREATE TRIGGER NEW_REVIEW
AFTER INSERT ON REVIEW
REFERENCING NEW AS N_ROW
FOR EACH ROW
UPDATE PRODUCT SET Rating = (
Select AVG(Rating)
from Review
where ProductName = N_ROW.ProductName
)
更新后:
CREATE TRIGGER NEW_REVIEW
AFTER UPDATE ON REVIEW
REFERENCING NEW AS N_ROW
FOR EACH ROW
UPDATE PRODUCT SET Rating = (
Select AVG(Rating)
from Review
where ProductName = N_ROW.ProductName
)
删除后:
CREATE TRIGGER NEW_REVIEW
AFTER DELETE ON REVIEW
REFERENCING OLD AS O_ROW
FOR EACH ROW
UPDATE PRODUCT SET Rating = (
Select AVG(Rating)
from Review
where ProductName = O_ROW.ProductName
)
查看CREATE TRIGGER statement了解更多信息。