0

我在数据库中有一组表。它们的语法是:

CREATE TABLE Concert
(concert_id NUMBER (3) NOT NULL,
 concert_name VARCHAR (14) NOT NULL,
 concert_duration NUMBER (1) NOT NULL,
 concert_type VARCHAR (10) NOT NULL,
 artist VARCHAR (14) NOT NULL,
 concert_cost NUMBER (3) NOT NULL
);

CREATE TABLE Customer
(customer_id NUMBER (4) NOT NULL,
 firstname VARCHAR (14) NOT NULL,
 lastname VARCHAR (14) NOT NULL,
 gender VARCHAR (1) NOT NULL,
 street VARCHAR (14),
 city VARCHAR (14),
 country VARCHAR (14),
 postcode VARCHAR (8) NOT NULL
);

CREATE TABLE Event
(event_id NUMBER (3) NOT NULL,
 event_date DATE NOT NULL,
 venue_id NUMBER (2) NOT NULL,
 concert_id NUMBER (3) NOT NULL
);

CREATE TABLE Venue
(venue_id NUMBER (2) NOT NULL,
 venue_name VARCHAR (14) NOT NULL,
 max_capacity NUMBER (5) NOT NULL,
 street VARCHAR (14),
 city VARCHAR (14),
 country VARCHAR (14),
 postcode VARCHAR (8) NOT NULL
);

CREATE TABLE Booking
(arrival_time VARCHAR (8) default to_char(localtimestamp,'HH:MI') check(arrival_time like '__:___AM' or arrival_time like '__:___PM'),
 vehicle_no VARCHAR (8),
 evaluation NUMBER (1) NOT NULL CONSTRAINT ck_evaluation CHECK (evaluation BETWEEN 0 AND 5),
 event_id NUMBER (3) NOT NULL,
 customer_id NUMBER (4) NOT NULL
);

Concert_id 和venue_id 是事件表中的外键。Event_id 和 customer_id 也是预订表中的外键。我想创建一个触发器,它将名字和姓氏(来自客户表)、concert_name(来自音乐会表)、event_date(来自事件表)、场地名称(来自场地表)和评估(来自预订表)放入和审计表中如果客户给出零评价。我已将审计表创建为:

CREATE TABLE audit_evaluation
(firstname VARCHAR (14),
 lastname VARCHAR (14),
 concert_name VARCHAR (14),
 event_date DATE,
 venue_name (14),
 evaluation NUMBER (1)
);

我试图用这种语法创建触发器,但它给出了一个错误:

CREATE OR REPLACE TRIGGER trg_evaluation
  AFTER INSERT ON booking
    FOR EACH ROW
BEGIN
  IF (evaluation = 0)
  THEN
    INSERT INTO audit_evaluation (firstname, lastname, concert_name, event_date, venue_name, evaluation)
      VALUES (customer.firstname, customer.lastname, concert.concert_name, event.event_date, venue.venue_name, booking.evaluation);
  END IF;
END;

请帮忙!!!

4

1 回答 1

1
CREATE OR REPLACE TRIGGER trg_evaluation
  AFTER INSERT ON booking
    FOR EACH ROW
BEGIN
  IF (evaluation = 0)
  THEN
    INSERT INTO audit_evaluation (firstname, lastname, concert_name, event_date, venue_name, evaluation)
      VALUES (customer.firstname, customer.lastname, concert.concert_name, event.event_date, venue.venue_name, booking.evaluation);
  END IF;
END;

首先,这里没有:old:new引用,因此触发器不知道从其他 4 个表中选择哪一行。您应该在这里学习如何使用它们。一旦您准备好编写触发器,您的insert语句“可能”如下所示(注意 的用法:new)-

INSERT INTO audit_evaluation (firstname, lastname, concert_name, event_date, venue_name, evaluation)
select c.firstname,c.lastname,con.concert_name,
       evt.event_date, ven.venue_name, :new.evaluation
  from customer c, concert con, event evt, venue ven
 where evt.evt_id = :new.event_id
   and c.customer_id = :new.customer_id
   and con.customer_id=c.customer_id
   and con.concert_id = evt.concert_id
   and evt.venue_id=ven.venue_id;

注意 - 不要因为这里的一些“不那么友好”的评论而气馁。人们只是想“帮助”。:)

于 2012-08-18T14:15:50.950 回答