2

如果我所问的问题已经在其他地方得到解答,请提前道歉,但是我很难找到合适的答案。我知道如何基于一个表创建简单的触发器,但是我试图实现跨两个表的链接更新。场景如下: course 表中有一个 duration 列,而 products 表中有一个 start_date 列。课程是提供课程并且两个表(课程和课程)通过 course_id 链接的实例。如果对应的课程持续时间为 5 天,我需要创建一个触发器,以防止产品表在 1 月期间具有开始日期。我已尝试创建此触发器并可以发布我的努力,但我敢肯定,我绝对远离。

CREATE OR REPLACE TRIGGER check_duration
  BEFORE UPDATE on offering
  FOR EACH ROW
BEGIN
  SELECT c.duration
  INTO duration
  FROM course c
  WHERE c.duration = 5
IF(:new.start_date BETWEEN to_date('01/01/2013', 'DD/MM/YYY') AND to_date('31/01/2013', 'DD/MM/YYY')
  THEN RAISE_APPLICATION_ERROR(20100, 'Cannot run particular course during this month')
END IF;
END;

课程表:

Course_id(pk) | Title | Duration | Cost

供品表:

offering_id(pk) | start_date | location | instructor | course_id(fk)

还有另外两个表出勤和学生我认为我不需要为这个问题发布这些表的构造

4

1 回答 1

0

如果有关于表格的任何新信息,您可能需要对此进行调整,但这里有一个基于您的评论的初步刺伤,即表格中有一个 courseid 列offering

CREATE OR REPLACE TRIGGER check_duration
  BEFORE UPDATE on offering
  FOR EACH ROW
DECLARE
  isFound NUMBER;
BEGIN
  -- Just want to know if there's a duration 5 record for the course...
  SELECT 1 INTO isFound FROM DUAL WHERE EXISTS (
    SELECT * FROM course c
    WHERE c.course_id = :NEW.course_id AND c.duration = 5);
  -- Fallthrough to here means a course was found with duration 5. Is
  -- it scheduled for January (month #1) of any year?
  IF EXTRACT(MONTH FROM :NEW.start_date) = 1 THEN
    THEN RAISE_APPLICATION_ERROR(20100, 'Cannot run particular course during this month');
  END IF;
EXCEPTION
  -- If the query didn't find a course with duration 5 it throws
  -- a NO_DATA_FOUND exception. Ignore the exception because not
  -- finding a course means it's OK to add the row.
  WHEN NO_DATA_FOUND THEN
    NULL;
END;
于 2013-03-26T18:35:43.590 回答