问题
编写SQL命令以在表Order中创建触发器。触发器应在每次产品出现在订单表中的新订单行之后,从产品表的quantityRemaining列中减去订单中产品的数量。
表格
CREATE TABLE Product(
productNo int PRIMARY KEY,
productName VARCHAR(30),
quantityRemaining INT,
cost DECIMAL (10,2) DEFAULT 0.00);
CREATE TABLE Orders(
orderNo INT PRIMARY KEY,
custNo INT CONSTRAINT cusNo_fk REFERENCES CustomerDetails(custNo) ON DELETE CASCADE,
productNo INT CONSTRAINT prodNo_fk REFERENCES Product(productNo) ON DELETE CASCADE,
quantity INT);
尝试
CREATE OR REPLACE TRIGGER subtractQuantity
AFTER INSERT ON Orders
FOR EACH ROW
BEGIN
UPDATE Product
SET quantityRemaining = quantityRemaining - quantity
WHERE productNo = :NEW.ProductNo;
END;
/
错误
它无法识别'数量'变量。
答案 0 :(得分:3)
我认为您缺少:New.
作为参考:
CREATE OR REPLACE TRIGGER subtractQuantity
AFTER INSERT ON Orders
FOR EACH ROW
BEGIN
UPDATE Product
SET quantityRemaining = quantityRemaining - :NEW.quantity
WHERE productNo = :NEW.ProductNo;
END;
quantity
位于Orders
表中,而不是Product
表。