看起来sale 表缺少关键信息:已售商品的数量(数量)。所以:
SQL> CREATE TABLE product (
2 product_id number,
3 price number,
4 quantity number
5 );
Table created.
SQL> CREATE TABLE sale (
2 sale_id number,
3 client_id number,
4 product_id number,
5 quantity number --> missing
6 );
Table created.
触发器:
SQL> create or replace trigger trg_ai_sale
2 after insert on sale
3 for each row
4 begin
5 update product p set
6 p.quantity = p.quantity - :new.quantity
7 where p.product_id = :new.product_id;
8 end;
9 /
Trigger created.
测试:product = 1 的起始数量为 50:
SQL> insert into product (product_id, price, quantity) values (1, 100, 50);
1 row created.
让我们卖 1 件商品:
SQL> insert into sale (sale_id, client_id, product_id, quantity) values (1, 1, 1, 1);
1 row created.
数量现在是 49:
SQL> select * from product;
PRODUCT_ID PRICE QUANTITY
---------- ---------- ----------
1 100 49
SQL>