create table product
(productid varchar(10) primary key,
productname varhchar(10),
totalquantity number(10),
reorderlevel number(10),
remainingquatity number(10) );
1. produtid productname totalquantity reorderlevel remainingquantity
0001 abc 100 0 0
0002 xyz 80 0 0
create table orders
(orderno varchar(10),
productid(10) references product(productid),
quantityordered number(10));
现在我的问题是,无论何时发出订单,remainingquantity
reorderlevel
和product table
进行自动更新
我尝试了触发器,但是它无法正常工作请帮助我,如果还有其他概念而不是触发器可以用来执行此过程请提及
答案 0 :(得分:1)
此触发器应该有效
create or replace trigger product_update
BEFORE insert on orders
for each row
begin
UPDATE PRODUCT
SET remainingquantity = totalquantity - :new.quantityordered,
reorderlevel = remainingquantity - :new.quantityordered
WHERE PRODUCT_ID = :new.PRODUCT_ID;
end;
答案 1 :(得分:0)
或者您可以规范化表格(为Oracle 11g重新编译):
b
并在查询中使用join
CREATE TABLE product(
id NUMBER(15,0) NOT NULL CONSTRAINT pk_product PRIMARY KEY,
name VARCHAR2(100),
totalquantity NUMBER(15,0)
)/
CREATE TABLE orders(
id NUMBER(15,0) NOT NULL CONSTRAINT pk_orders PRIMARY KEY,
orderno VARCHAR2(100),
quantityordered NUMBER(15,0),
productid NUMBER(15,0) CONSTRAINT fk_orders_product_id REFERENCES product(id)
)/
测试数据:
select Q.id, Q.name,
Q.totalquantity - Q.orderdquantity as remainingquantity,
Q.totalquantity - 2*Q.orderdquantity as reorderlevel
from
(select p.id, p.name, p.totalquantity,
coalesce(sum(o.quantityordered) ,0) as orderdquantity
from product p
left join orders o on o.productid = p.id
group by p.id, p.name, p.totalquantity) Q
结果:
insert into product(id, name, totalquantity) values(1, 'apple', 100)/
insert into product(id, name, totalquantity) values(2, 'tomato', 100)/
insert into product(id, name, totalquantity) values(3, 'carrot', 100)/
insert into orders(id, orderno, quantityordered, productid) values(1, '№1', 10, 1)/
insert into orders(id, orderno, quantityordered, productid) values(2, '№2', 3, 1)/
insert into orders(id, orderno, quantityordered, productid) values(3, '№3', 23, 2)/