甲骨文:更新触发器后
Oracle: After update Trigger
我有 3 个表:
Category(CategoryId, Name)
Product(ProductId, Name, Description, CategoryId)
OrderItem(OrderId, OrdinalNumber, ProductId, CategoryId)
我想创建一个 AFTER UPDATE
触发器来更改 CategoryId
(基于更新 OrderItem
中的 ProductId
后 OrderItem
中的新 ProductId)
=].
有人可以帮忙解决这个触发器吗?
在订单行中复制类别 ID 不是您通常想要做的事情,但如果您打算这样做,则需要 'before' 触发器,而不是 'after' 一个 - 因为您需要更改正在更新的行中的值:
create or replace trigger orderitem_cat_trig
before insert or update on orderitem
for each row
begin
select categoryid
into :new.categoryid
from product
where productid = :new.productid;
end;
/
我假设您也想为新订单项设置值,所以我已将其插入和更新。
除非您喜欢数据库死锁、一般性能问题、数据损坏和不可预测的结果,否则不建议使用此类更新。如果您的性能有问题,请检查索引和查询。不要在表中复制您的列,尤其是当它们是外键的一部分时。我不是教条主义者,但在这种情况下我不会让步;-)
我有 3 个表:
Category(CategoryId, Name)
Product(ProductId, Name, Description, CategoryId)
OrderItem(OrderId, OrdinalNumber, ProductId, CategoryId)
我想创建一个 AFTER UPDATE
触发器来更改 CategoryId
(基于更新 OrderItem
中的 ProductId
后 OrderItem
中的新 ProductId)
=].
有人可以帮忙解决这个触发器吗?
在订单行中复制类别 ID 不是您通常想要做的事情,但如果您打算这样做,则需要 'before' 触发器,而不是 'after' 一个 - 因为您需要更改正在更新的行中的值:
create or replace trigger orderitem_cat_trig
before insert or update on orderitem
for each row
begin
select categoryid
into :new.categoryid
from product
where productid = :new.productid;
end;
/
我假设您也想为新订单项设置值,所以我已将其插入和更新。
除非您喜欢数据库死锁、一般性能问题、数据损坏和不可预测的结果,否则不建议使用此类更新。如果您的性能有问题,请检查索引和查询。不要在表中复制您的列,尤其是当它们是外键的一部分时。我不是教条主义者,但在这种情况下我不会让步;-)