我写了以下触发器:
CREATE OR REPLACE TRIGGER check_exprdate_tgr
BEFORE INSERT OR UPDATE ON coupon
FOR EACH ROW
BEGIN
IF(:new.Expiry_date<= SYSDATE)
THEN
RAISE_APPLICATION_ERROR(404, 'Coupon Expiry date is not valid. Expiry date must be set to a later date.');
END IF;
END;
表格如下:
CREATE TABLE customer (
Id INTEGER
CONSTRAINT customer_id_nn NOT NULL,
Name VARCHAR2(50)
CONSTRAINT cutomer_name_nn NOT NULL,
Surname VARCHAR2(50)
CONSTRAINT customer_surname_nn NOT NULL,
Dob DATE
CONSTRAINT customer_date_nn NOT NULL,
Email VARCHAR2(50),
Gender VARCHAR2(10)
CONSTRAINT customer_gender_nn NOT NULL,
Residence_number Integer
CONSTRAINT customer_residenceno_nn NOT NULL,
Street VARCHAR2(50)
CONSTRAINT customer_street_nn NOT NULL,
Town_id INTEGER,
CONSTRAINT customer_id_pk PRIMARY KEY(Id),
CONSTRAINT customer_townId_fk FOREIGN KEY(Town_id)
REFERENCES TOWN(Id)
);
CREATE TABLE coupon (
Id INTEGER,
Expiry_date DATE
CONSTRAINT coupon_date_nn NOT NULL,
Discount_percentage NUMBER(4,2)
CONSTRAINT coupon_discperc_nn NOT NULL,
Details VARCHAR2(50),
Customer_id INTEGER
CONSTRAINT coupon_customerid_nn NOT NULL,
CONSTRAINT coupon_id_pk PRIMARY KEY(Id),
CONSTRAINT coupon_customerid_fk FOREIGN KEY(Customer_id)
REFERENCES customer(Id)
);
触发器的目的是检查到期日期输入是否设置在SYSTATE之后的日期。我编译了触发器并且编译正确,所以我真的不知道代码有什么问题。
谢谢:)
答案 0 :(得分:1)
RAISE_APPLICATION_ERROR()中的数字必须在为用户定义的错误(即-20999到-20000)预留的范围内。 It's in the documentation
RAISE_APPLICATION_ERROR(-20404
, 'Coupon Expiry date is not valid. Expiry date must be set to a later date.');
值得注意的是,每次更新COUPON表上的任何列时,都会评估此行,无论您是否已触及过EXPIRY_DATE:
IF(:new.Expiry_date<= SYSDATE)
这意味着优惠券到期后您无法更新表格。这可能就是你想要的。否则你应该考虑一个更细微的测试,比如说
IF ( INSERTING or :new.Expiry_date != :old.Expiry_date )
AND :new.Expiry_date<= SYSDATE
触发器包含在the PL/SQL Reference。
中