创建折扣触发器

时间:2017-02-06 14:33:40

标签: sql db2 database-trigger

我想创建一个触发器,它会根据表格中的折扣更新表格票证中的价格。但是我在插入

时遇到了错误

表票:

create table Ticket(
Ticket_ID integer not null,
Class_Name varchar(50),
Payment_Method varchar(60),
Audience_ID integer,
Seating_Number varchar(20),
Price integer,

primary key(Ticket_ID),
foreign key(Class_Name) references Class)
foreign key(Audience_ID) references Audience,
foreign key(Seating_Number) references Seating);

表格受众

CREATE TABLE AUDIENCE
(Audience_ID integer not null,
 Audience_Age integer,
 Audience_Type varchar (50),
 Discount Decimal(7,2),
 Primary key (Audience_ID)
 );



CREATE Trigger Audience_Type
    AFTER INSERT ON AUDIENCE
    FOR EACH ROW MODE DB2SQL
        UPDATE AUDIENCE
        SET Audience_Type = (SELECT Price FROM TICKET) &&
        (Price-(Price*Discount))
        WHERE AUDIENCE.Discount = TICKET.Price 

1 个答案:

答案 0 :(得分:0)

您没有更新故障单表,而是更新了受众表。 试试:

CREATE Trigger T_name
AFTER INSERT ON AUDIENCE
FOR EACH ROW MODE DB2SQL
    UPDATE Ticket   -- here you should update Ticket table 
    SET price = Price-(Price*:new.Discount)
    WHERE ticket.AUDIENCE_id= AUDIENCE.AUDIENCE_id

阅读一些关于:new和:old当你使用触发器时。此外,如果更新受众表,则不会更新故障单表。您可以说“插入或更新后......”