创建依赖审计触发器

时间:2012-08-18 06:02:51

标签: oracle triggers plsql

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

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也是预订表中的外键。我想创建一个触发器,它将firstname和lastname(来自customer表),concert_name(来自Concea表),event_date(来自事件表),venue_name(来自场地表)和评估(来自预订表)放入和审计表中如果客户给出零评估。我创建了审计表:

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;

请帮助!!!

1 个答案:

答案 0 :(得分: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个表中选择哪一行。您应该学习如何使用它们here。一旦您准备好编写触发器,您的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;

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