在表格中输入值时定义间隔

时间:2015-06-22 07:29:52

标签: oracle plsql

我创建了一个表格如下:

CREATE table screening
(
  screening_id NUMBER(6,0) PRIMARY KEY,
  plan_id NUMBER(4,0),
  theatre_id NUMBER(1,0),
  screening_date DATE,
  screening_start_hh24 NUMBER(2,0) CHECK(screening_start_hh24 BETWEEN 9 AND 22),
  screening_start_mm60 NUMBER(2,0) CHECK(screening_start_mm60 BETWEEN 0 AND 59),
  CONSTRAINT fk_plan_id FOREIGN KEY(plan_id)
    REFERENCES screening_plan(plan_id),
  CONSTRAINT fk_theatre_id_screening FOREIGN KEY(theatre_id)
    REFERENCES theatre(theatre_id)
)

'screening_start_hh24'用于以小时为单位的开始时间,'screening_start_mm60'用于以分钟为单位的开始时间。 如何在同一个剧院的两次连续放映之间添加至少30分钟的休息时间?我正在使用oracle。

1 个答案:

答案 0 :(得分:0)

创建存储过程(在包内或包中)以添加筛选并实现以下逻辑:

CREATE SEQUENCE screening_seq;
/
CREATE OR REPLACE PACKAGE screening_pkg AS
  PROCEDURE addScreening(
    i_plan_id IN  screening.plan_id%TYPE,
    i_theatre_id IN  screening.theatre_id%TYPE,
    i_start   IN  screening.screening_date%TYPE,
    o_error   OUT VARCHAR2
  );
END screening_pkg;
/
CREATE OR REPLACE PACKAGE BODY screening_pkg AS
  PROCEDURE addScreening(
    i_plan_id    IN  screening.plan_id%TYPE,
    i_theatre_id IN  screening.theatre_id%TYPE,
    i_start      IN  screening.screening_date%TYPE,
    o_error      OUT VARCHAR2
  )
  AS
  BEGIN
    DECLARE
      v_exists NUMBER(1,0);
    BEGIN
      SELECT 1
      INTO   v_exists
      FROM   screening
      WHERE  theatre_id = i_theatre_id
      AND    screening_date BETWEEN i_start - INTERVAL '30' MINUTE
                            AND     i_start + INTERVAL '30' MINUTE;

      o_error := 'Screening exists within 30 minutes.';
      RETURN;
    EXCEPTION
      WHEN NO_DATA_FOUND THEN
        NULL;
    END;

    INSERT INTO screening (
      screening_id,
      plan_id,
      theatre_id,
      screening_date,
      screening_start_hh24,
      screening_start_mm60
    ) VALUES (
      screening_seq.NEXTVAL,
      i_plan_id,
      i_theatre_id,
      i_start,
      TO_NUMBER( TO_CHAR( i_start, 'HH24' ) ),
      TO_NUMBER( TO_CHAR( i_start, 'MI' ) )
    );
  END addScreening;
END screening_pkg;
/

此外,您不需要小时和分钟列 - 您只需使用日期列。