在oracle sql中创建视图和触发器

时间:2016-04-26 19:13:11

标签: sql oracle

我需要帮助创建视图,然后触发以下操作:在输入每个发票行项目时更新销售员佣金(销售额的10%),库存数量和客户余额。

这就是我现在对视图的看法:

CREATE OR REPLACE VIEW NEW_INVOICE_LINE_ITEM 
AS
SELECT COMMISSION, INV_QUANTITY, CUSTOMER_BALANCE
FROM SALESPERSON, INVENTORY, CUSTOMER;

这就是我对触发器的看法:

CREATE OR REPLACE TRIGGER UPDATE_COMMISSION
AFTER INSERT ON INVOICE_LINE_ITEM FOR EACH ROW
BEGIN
UPDATE SALESPERSON
SET COMMISSION = (SALE_PRICE * QUANTITY_SOLD) *.10
WHERE :NEW.COMMISSION = SALESPERSON.COMMISSION;
END;

我一直在收到错误:错误的绑定变量' new.commission'

这是我的数据库架构:

DROP TABLE PO_LINE_ITEM;
DROP TABLE PURCHASE_ORDER;
DROP TABLE VENDOR;
DROP TABLE INVOICE_LINE_ITEM;
DROP TABLE INVENTORY;
DROP TABLE INVOICE;
DROP TABLE SALESPERSON;
DROP TABLE CUSTOMER;
CREATE TABLE CUSTOMER
(CUSTOMER_ID DECIMAL(2,0) PRIMARY KEY,    
CUSTOMER_NAME CHAR(25),
CUSTOMER_ADDRESS CHAR(15),
CUSTOMER_ZIPCODE DECIMAL(5,0),
CUSTOMER_CITY CHAR(15),
CUSTOMER_STATE CHAR(2),
CUSTOMER_BALANCE DECIMAL(4,2)
);
CREATE TABLE SALESPERSON
(SALESPERSON_ID DECIMAL(3,0) PRIMARY KEY,
SALESPERSON_NAME CHAR(25),
COMMISSION DECIMAL(5,2)
);
CREATE TABLE INVOICE
(INVOICE_ID DECIMAL(3,0),
CUSTOMER_ID DECIMAL(2,0),
SALESPERSON_ID DECIMAL(3,0),
ITEM_NUM DECIMAL(4,0),
INVOICE_DATE DATE,
PRIMARY KEY (CUSTOMER_ID, SALESPERSON_ID),
FOREIGN KEY (CUSTOMER_ID) REFERENCES CUSTOMER,
FOREIGN KEY (SALESPERSON_ID) REFERENCES SALESPERSON
);
CREATE TABLE INVENTORY 
(INV_NUM DECIMAL(4,0) PRIMARY KEY,
DESCRIPTION CHAR(10),
INV_QUANTITY DECIMAL(4,0),
INV_PRICE DECIMAL(7,2),
INV_COST DECIMAL(7,2),
INVOICE_ID DECIMAL(3,0),
ITEM_NUM DECIMAL(4,0),
FOREIGN KEY (INVOICE_ID, ITEM_NUM) REFERENCES INVOICE
);
CREATE TABLE INVOICE_LINE_ITEM
(QUANTITY_SOLD DECIMAL(4,0),
SALE_PRICE DECIMAL(7,2),
INVOICE_ID DECIMAL(3,0),
INV_NUM DECIMAL(4,0),
ITEM_NUM DECIMAL(4,0),
PRIMARY KEY (INVOICE_ID, INV_NUM, ITEM_NUM),
FOREIGN KEY (INVOICE_ID, ITEM_NUM) REFERENCES INVOICE,
FOREIGN KEY (INV_NUM) REFERENCES INVENTORY
);
CREATE TABLE VENDOR
(VENDOR_ID DECIMAL(2,0) PRIMARY KEY,
VENDOR_NAME CHAR(25),
CITY CHAR(15),
STATE CHAR(2),
VENDOR_BALANCE DECIMAL(4,2)
);
CREATE TABLE PURCHASE_ORDER
(PURCHASE_ORDER_ID DECIMAL(2,0) PRIMARY KEY,
BALANCE DECIMAL(4,2),
SHIPMENT CHAR(10),
PURCHASE_ORDER_DATE DATE,
VENDER_ID DECIMAL (2,0),
FOREIGN KEY (VENDER_ID) REFERENCES VENDOR
);
CREATE TABLE PO_LINE_ITEM
(PO_DATE DATE,
PO_BALANCE DECIMAL(4,0),
ITEM_NUM DECIMAL(4,0),
INV_QUANTITY DECIMAL(4,0),
INV_NUM DECIMAL(4,0),
PURCHASE_ORDER_ID DECIMAL(2,0),
PRIMARY KEY (INV_NUM, PURCHASE_ORDER_ID),
FOREIGN KEY (INV_NUM) REFERENCES INVENTORY,
FOREIGN KEY (PURCHASE_ORDER_ID) REFERENCES PURCHASE_ORDER);

提前致谢!

1 个答案:

答案 0 :(得分:0)

invoice_line_item不包含列commission,这是错误绑定变量错误的原因。你应该像这里一样重写你的触发器代码:

create or replace trigger update_commission
  after insert on invoice_line_item for each row
begin
  update salesperson sp
     set sp.commission = sp.commission + (:new.sale_price * :new.quantity_sold * .1)
   where sp.salesperson_id =
         (select salesperson_id
            from invoice i
           where i.invoice_id = :new.invoice_id);
end;

view您没有附加条件如何连接表格,以下是如何执行此操作的示例:

create or replace view vw_invoices as 
  select ili.invoice_id, ili.quantity_sold, ili.sale_price, 
         i.salesperson_id, sp.salesperson_name
    from invoice_line_item ili
    join invoice  i on i.invoice_id = ili.invoice_id
    join salesperson sp on sp.salesperson_id = i.salesperson_id;

根据您要显示的信息 - 连接正确的表并使用正确的连接。