Oracle sql 在 insert/update 上触发以计算 2 个值并更新第三个值

Oracle sql trigger on insert/update to calculate 2 values and update a third value

我想制作一个触发器,通过计算 2 个值来更新行并更新第三个值:

CREATE TABLE Customers (
    Cust_ID INTEGER PRIMARY KEY,
    Dates TIMESTAMP WITH LOCAL TIME ZONE,
    Quantity dec(7,2) NOT NULL,
    Price_per_item dec(7,2) NOT NULL,
    Total_price dec(7,2)
);

我已经这样做了:

CREATE OR REPLACE TRIGGER cust_after_insert AFTER INSERT 
ON Customers
FOR EACH ROW
BEGIN
    UPDATE Customers
    SET 
    DATES = CURRENT_TIMESTAMP,
    Total_price = Quantity * Price_per_item;
END;

我尝试同时查看和编辑 table 时遇到一些错误。

我也试过这个:

CREATE SEQUENCE cust_Seq START WITH 1 INCREMENT BY 1;
CREATE OR REPLACE TRIGGER cust_trig BEFORE INSERT ON Customers
FOR EACH ROW
BEGIN
  SELECT cust_Seq.nextval INTO :new.Fuel_ID FROM dual;
  SELECT CURRENT_TIMESTAMP INTO :new.DATES FROM dual;
  SELECT Quantity * Price_per_item INTO :new.Total_price FROM dual;
END;
/

我更喜欢第二个选项,如果更新也会发生,我将需要一些东西。

第二种方法是正确的,但它需要一些调整来处理 Total_price 计算并处理 update

CREATE SEQUENCE cust_Seq START WITH 1 INCREMENT BY 1;
CREATE OR REPLACE TRIGGER cust_trig BEFORE INSERT OR UPDATE ON Customers
FOR EACH ROW
BEGIN
  IF INSERTING THEN
    SELECT cust_Seq.nextval INTO :new.Fuel_ID FROM dual;
    SELECT CURRENT_TIMESTAMP INTO :new.DATES FROM dual;
  END IF;
  :new.Total_price :=  :new.Quantity * :new.Price_per_item;
END;
/