创建触发器以调整产品库存 - APEX ORACLE

时间:2015-05-11 19:56:19

标签: sql oracle triggers oracle-apex

我试图在APEX ORACLE上创建一个触发器,以便当购买通过订单行表时,客户购买产品时设置的数量将从库存中取出在产品表

CREATE or Replace TRIGGER updatestock
AFTER DELETE OR UPDATE OF QUANTITY.ORDERLINE ON ORDERLINE
FOR EACH ROW
BEGIN
   SET PRODUCT_STOCK.PRODUCTS = PRODUCT_STOCK.PRODUCTS - QUANTITY.ORDERLINE
   WHERE PRODUCT_ID.ORDERLINE = PRODUCT_ID.PRODUCTS
END;

基本上我希望触发器能够注意到订单行中的数量,并从产品库存中减去它。但是,我收到以下错误:

ORA-01748:此处只允许使用简单的列名

有人能指出我正确的方向吗?

SQL:

DROP TABLE ADMIN CASCADE CONSTRAINTS ;
DROP TABLE USERS CASCADE CONSTRAINTS ;
DROP TABLE STALLS CASCADE CONSTRAINTS ;
DROP TABLE PRODUCTS CASCADE CONSTRAINTS ;
DROP TABLE STALLHOLDER CASCADE CONSTRAINTS ;
DROP TABLE CUSTOMERORDER CASCADE CONSTRAINTS;
DROP TABLE ORDERLINE CASCADE CONSTRAINTS;
DROP TABLE COLLECTION CASCADE CONSTRAINTS;
drop sequence ADMIN_ID_SEQ;
drop sequence USER_ID_SEQ;
drop sequence STALL_ID_SEQ;
drop sequence PRODUCT_ID_SEQ;
drop sequence STALLHOLDER_ID_SEQ;
drop sequence CUSTOMERORDER_ID_SEQ;
drop sequence ORDERLINE_ID_SEQ;
drop sequence COLLECTION_ID_SEQ;
create sequence ADMIN_ID_SEQ start with 1;
create sequence USER_ID_SEQ start with 1;
create sequence STALL_ID_SEQ start with 1;
create sequence PRODUCT_ID_SEQ start with 1;
create sequence STALLHOLDER_ID_SEQ start with 1;
create sequence CUSTOMERORDER_ID_SEQ start with 1;
create sequence COLLECTION_ID_SEQ start with 1;
create sequence ORDERLINE_ID_SEQ start with 1;

CREATE table COLLECTION (
    COLLECTION_ID NUMBER(20) NOT NULL PRIMARY KEY,
    STATUS        VARCHAR(10) NOT NULL,
    TIME          NUMBER(4) NOT NULL);

CREATE table CUSTOMERORDER (
    ORDER_ID      NUMBER(15) NOT NULL PRIMARY KEY,
    ORDER_DATE    NUMBER(8) NOT NULL,
    STATUS        VARCHAR2(10) NOT NULL,
    COLLECTION_ID NUMBER(20) NOT NULL);

CREATE TABLE STALLS (
    STALL_ID      NUMBER(15) NOT NULL PRIMARY KEY,
    STALL_NAME    VARCHAR(25) NOT NULL,
    STALL_DESC    VARCHAR(100),
    STALL_TYPE    VARCHAR(25) NOT NULL,
    STALLHOLDER_ID  NUMBER(25) NOT NULL);

CREATE TABLE PRODUCTS (
    PRODUCT_ID    NUMBER(15) NOT NULL PRIMARY KEY,
    PRODUCT_NAME  VARCHAR(25) NOT NULL,
    PRODUCT_TYPE  VARCHAR(15) NOT NULL,
    PRODUCT_PRICE NUMBER(6,2) NOT NULL,
    PRODUCT_STOCK NUMBER(3) NOT NULL,    
    STALL_ID      NUMBER(15) NOT NULL); 

CREATE table ORDERLINE (
    ORDERLINE_ID  NUMBER(15) NOT NULL PRIMARY KEY,
    QUANTITY      NUMBER(2) NOT NULL,
    TOTALPRICE    DECIMAL(19,4) NOT NULL,
    ORDER_ID      NUMBER(15) NOT NULL,
    PRODUCT_ID    NUMBER(15) NOT NULL);

CREATE table USERS (
    USER_ID      NUMBER(15) NOT NULL PRIMARY KEY,
    USERNAME    VARCHAR2(25) NOT NULL,
    PASSWORD    VARCHAR2(25) NOT NULL,
    NAME        VARCHAR2(25) NOT NULL,
    SURNAME     VARCHAR2(25) NOT NULL,
    ADDRESS     VARCHAR2(100) NOT NULL,
    CONTACTNO    NUMBER(11) NOT NULL);  

CREATE table STALLHOLDER (
    STALLHOLDER_ID     NUMBER(25) NOT NULL PRIMARY KEY,
    USERNAME           VARCHAR2(25) NOT NULL,
    PASSWORD           VARCHAR2(25) NOT NULL,
    NAME               VARCHAR2(25) NOT NULL,
    SURNAME            VARCHAR2(25) NOT NULL,
    CONTACTNO          NUMBER(11) NOT NULL);

CREATE table ADMIN (
    ADMIN_ID      NUMBER(15) NOT NULL PRIMARY KEY,
    USERNAME    VARCHAR2(25) NOT NULL,
    PASSWORD    VARCHAR2(25) NOT NULL);  

INSERT INTO ADMIN VALUES (ADMIN_ID_SEQ.nextval, 'Test', 'Test');
INSERT INTO ADMIN VALUES (ADMIN_ID_SEQ.nextval, 'Admin', 'Admin');

INSERT INTO STALLHOLDER VALUES (STALLHOLDER_ID_SEQ.nextval, 'Stallholder1', 'Stallholder1', 'Stall 1', 'Stall 1', '0');
INSERT INTO STALLHOLDER VALUES (STALLHOLDER_ID_SEQ.nextval, 'Stallholder2', 'Stallholder2', 'Stall 2', 'Stall 2', '0');
INSERT INTO STALLHOLDER VALUES (STALLHOLDER_ID_SEQ.nextval, 'Stallholder3', 'Stallholder3', 'Stall 3', 'Stall 3', '0');
INSERT INTO STALLHOLDER VALUES (STALLHOLDER_ID_SEQ.nextval, 'Stallholder4', 'Stallholder4', 'Stall 4', 'Stall 4', '0');
INSERT INTO STALLHOLDER VALUES (STALLHOLDER_ID_SEQ.nextval, 'Stallholder5', 'Stallholder5', 'Stall 5', 'Stall 5', '0');
INSERT INTO STALLHOLDER VALUES (STALLHOLDER_ID_SEQ.nextval, 'Stallholder6', 'Stallholder6', 'Stall 6', 'Stall 6', '0');

INSERT INTO USERS VALUES (USER_ID_SEQ.nextval, 'Test', 'Test', 'Test', 'Test', 'Test', '0');
INSERT INTO USERS VALUES (USER_ID_SEQ.nextval, 'Test2', 'Test2', 'Test2', 'Test2', 'Test2', '0');
INSERT INTO USERS VALUES (USER_ID_SEQ.nextval, 'Test3', 'Test3', 'Test3', 'Test3', 'Test3', '0');
INSERT INTO USERS VALUES (USER_ID_SEQ.nextval, 'Test4', 'Test4', 'Test4', 'Test4', 'Test4', '0');

INSERT INTO STALLS VALUES (STALL_ID_SEQ.nextval, 'Meat Store', '', 'Meat', '1');
INSERT INTO STALLS VALUES (STALL_ID_SEQ.nextval, 'Meat Store 2', '', 'Meat', '2');
INSERT INTO STALLS VALUES (STALL_ID_SEQ.nextval, 'Confectionary Store', '', 'Confectionary', '3');
INSERT INTO STALLS VALUES (STALL_ID_SEQ.nextval, 'Confectionary Store 2', '', 'Confectionary', '4');
INSERT INTO STALLS VALUES (STALL_ID_SEQ.nextval, 'Clothing Store', '', 'Clothing', '5');
INSERT INTO STALLS VALUES (STALL_ID_SEQ.nextval, 'Phone Store', '', 'Clothing', '6');

INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Steak', 'Meat', '5.99', '15', '0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Steak', 'Meat', '9.99', '10','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Steak', 'Meat', '12.99', '10','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Pork', 'Meat', '3.99', '20','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Pork', 'Meat', '5.99', '20','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Pork', 'Meat', '8.99', '15','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Bacon', 'Meat', '1.99', '20','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Bacon', 'Meat', '2.99', '20','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Bacon', 'Meat', '3.99', '20','0', '1');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Steak', 'Meat', '5.99', '15','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Steak', 'Meat', '9.99', '20','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Steak', 'Meat', '12.99', '20','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Pork', 'Meat', '3.99', '15','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Pork', 'Meat', '5.99', '15','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Pork', 'Meat', '8.99', '15','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Bacon', 'Meat', '1.99', '20','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Bacon', 'Meat', '2.99', '20','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Bacon', 'Meat', '3.99', '20','0', '2');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Haribo', 'Sweets', '1.50', '50','0', '3');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Haribo', 'Sweets', '1.00', '50','0', '3');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'PicknMix100g', 'Sweets', '2.00', '999','0', '3');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'PicknMix200g', 'Sweets', '3.50', '999','0', '3');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'ToxicWaste', 'Sweets','1.50','50','0','3');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Haribo', 'Sweets', '1.50', '100','0', '4');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Haribo', 'Sweets', '1.00', '100','0', '4');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'PicknMix100g', 'Sweets', '2.00', '999','0', '4');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'PicknMix200g', 'Sweets', '3.50', '999','0', '4');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'ToxicWaste', 'Sweets', '1.50', '50','0', '4');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'T-Shirts', 'Clothing', '10.00', '20','0', '5');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Shorts', 'Clothing', '15.00', '20','0', '5');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Jeans', 'Clothing', '20.00', '20','0', '5');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Hoodies', 'Clothing', '20.00', '20','0', '5');
INSERT INTO PRODUCTS VALUES (PRODUCT_ID_SEQ.nextval, 'Vests', 'Clothing', '10.00', '20','0', '5');

ALTER TABLE STALLS ADD CONSTRAINT FK_STALLHOLDER_ID FOREIGN KEY (STALLHOLDER_ID) REFERENCES STALLHOLDER(STALLHOLDER_ID);
ALTER TABLE ORDERLINE ADD CONSTRAINT FK_ORDER_ID FOREIGN KEY (ORDER_ID) REFERENCES CUSTOMERORDER(ORDER_ID);
ALTER TABLE ORDERLINE ADD CONSTRAINT FK_PRODUCT_ID FOREIGN KEY (PRODUCT_ID) REFERENCES PRODUCTS(PRODUCT_ID);
ALTER TABLE CUSTOMERORDER ADD CONSTRAINT FK_COLLECTION_ID FOREIGN KEY (COLLECTION_ID) REFERENCES COLLECTION(COLLECTION_ID);
ALTER TABLE PRODUCTS ADD CONSTRAINT FK_STALL_ID FOREIGN KEY (STALL_ID) REFERENCES STALLS(STALL_ID);

1 个答案:

答案 0 :(得分:1)

错误可能来自#include <stdio.h> #include <stdlib.h> int main(int argc, char **argv){ long long int ten = 10; long long int megi = 1024*1024; long long int gigi = 1024*megi; long long int asize = (ten*gigi)/((long int) sizeof(double)); double * data = new double[asize]; long long int i=2; printf("A double is %zd bytes\n", (size_t) sizeof(double)); printf("Array size is %lli \n", asize); data[0]=0.0; data[1]=1.0; while (i<asize) { data[i]=data[i-1]+1.0; ++i; } printf("%lf\n", (double) data[asize-1]); printf("success\n"); exit(EXIT_SUCCESS); } A double is 8 bytes Array size is 1342177280 1342177279.000000 success ,因为您使用的是table.column模式而不仅仅是列名。您似乎也一直在以错误的方式放置表格和列名称,并且您在OF QUANTITY.ORDERLINE之前错过了UPDATE,并且您正试图引用表格行中的列更新而不是使用SET伪轨。所以这应该更接近:

NEW

但删除时CREATE or Replace TRIGGER updatestock AFTER DELETE OR UPDATE OF QUANTITY ON ORDERLINE FOR EACH ROW BEGIN UPDATE PRODUCTS SET PRODUCT_STOCK = PRODUCT_STOCK - :NEW.QUANTITY WHERE PRODUCT_ID = :NEW.PRODUCT_ID END; 值为空;你没有在插入中做任何调整;甚至在更新时你可能想要考虑旧的和新的数量。

即便如此,尝试在另一个表中维护运行总计只能在单用户系统中运行,或者在更新序列化的情况下才能运行。如果两个人同时更新同一NEW行,则触发器将对orderline表应用两个更新,但第二个将阻塞,直到第一个已提交,并将应用于旧该语句开头的值 - 因此您将有效地丢失第一个更新。