仅允许在预订表中使用工作时间

时间:2012-11-25 21:35:41

标签: sql postgresql database-design schedule postgresql-9.2

PostgreSql 9.2保留表定义为

CREATE EXTENSION btree_gist;
CREATE TABLE schedule (
  id serial primary key,
  during tsrange not null,
  EXCLUDE USING gist (during WITH &&)
);

假期列于表

CREATE TABLE holiday ( day primary key );

工作时间为工作日的8至18:00,只能按照30分钟的间隔完成预约。 如何在值期间添加约束,以便它只允许在工作时间进行预留:

  1. tsrange中的开始日期和结束日期始终相同。
  2. 开始和结束日期不能是星期六和星期日
  3. 开始和结束日期不能出现在公共假期表中
  4. 开始时间仅为8:00,8:30,9:00,9:30,... 16:00,16:30,17:00或17:30
  5. 结束时间仅限8:30,9:00,9:30,16:00,16:30,17:00,17:30或18:00独家
  6. 将这些或部分约束添加到此表中是否合理? 如果是,如何添加? 如果这有帮助,可以更改计划表结构。

2 个答案:

答案 0 :(得分:4)

您可以使用普通CHECK constraints

解决除第3项以外的所有内容

使用专用range functions lower(anysrange) and upper(anyrange)访问范围的下边界/上边框。

1。)tsrange中的开始和结束日期始终相同。

CONSTRAINT schedule_same_day
CHECK (lower(during)::date = upper(during)::date)

2。)开始和结束日期不能是星期六和星期日

使用isodow, not dow表示更简单的表达式。

CONSTRAINT schedule_no weekend
CHECK (EXTRACT(ISODOW FROM lower(during)) < 6)  -- upper on same day

3。)开始和结束日期不能出现在公共假期表

唯一的例外:为此你需要一个触发器,如:

CREATE OR REPLACE FUNCTION trg_during_no_holy()
  RETURNS trigger AS
$func$
BEGIN

IF EXISTS (SELECT 1 FROM holiday WHERE day = lower(NEW.during)) THEN
    RAISE EXCEPTION 'Day too holy: %', lower(NEW.during);
END IF;

RETURN NEW;

END
$func$ LANGUAGE plpgsql VOLATILE;

CREATE TRIGGER insupbef_holycheck
  BEFORE INSERT OR UPDATE
  ON schedule
  FOR EACH ROW
  EXECUTE PROCEDURE trg_during_no_holy();

4。)开始时间只能是8:00,8:30,9:00,9:30,... 16:00,16:30,17:00或17:30(含) 5.)结束时间仅限8:30,9:00,9:30,16:00,16:30,17:00,17:30或18:00独家

CONSTRAINT schedule_8_inc_to_18_exc_half_hours
 CHECK (lower(during)::time BETWEEN time '8:00'AND time '17:30' -- time range
    AND upper(during)::time BETWEEN time '8:30'AND time '18:00'
    AND EXTRACT(MINUTE FROM lower(during)) IN (0, 30) -- only :00 or :30
    AND EXTRACT(MINUTE FROM upper(during)) IN (0, 30)
    AND lower_inc(during)          -- lower bound always incl.
    AND upper_inc(during) = FALSE  -- upper bound always excl.
)

评论中的其他问题

  

如何将秒和小数秒限制为0?

最简单的方法:转换为timestamp(0)timestamptz(0)或使用此数据类型开始列。我引用the manual here

  

timetimestampinterval接受可选的精确值p   它指定了保留的小数位数   秒场。

答案 1 :(得分:1)

您需要更改表定义并添加一些check约束:

CREATE TABLE schedule (
  id serial primary key,
  during tsrange not null check(
    (lower(during)::date = upper(during)::date) and 
    (date_trunc('hour', upper(during)) + INTERVAL '30 min' * ROUND(date_part('minute', upper(during)) / 30.0) = upper(during)) and
    (date_trunc('hour', lower(during)) + INTERVAL '30 min' * ROUND(date_part('minute', lower(during)) / 30.0) = lower(during)) and
    (lower(during)::time >= '8:00'::time and upper(during)::time <= '18:00'::time) and
    (date_part('dow', lower(during)) in (1,2,3,4,5) and date_part('dow', upper(during)) in (1,2,3,4,5))
  ),
  EXCLUDE USING gist (during WITH &&)
);

支票按此顺序

  • 开始和结束日是相同的
  • 开始/结束必须在30分钟边界
  • 和8:00至18:00之间
  • 仅限工作日

我们需要holiday表格中的内容:     插入假日值('2012-11-28');

check无法引用其他表,因此我们需要触发函数(将所有检查放入此函数可能更好,即将它们放在一个位置):

create function holiday_check() returns trigger language plpgsql stable as $$
begin
    if exists (select * from holiday where day in (lower(NEW.during)::date, upper(NEW.during)::date)) then
        raise exception 'public holiday';
    else
        return NEW;
    end if;
end;
$$;

然后我们需要在insert / update之前创建触发器:

create trigger holiday_check_i before insert on schedule for each row execute procedure holiday_check();
create trigger holiday_check_u before update on schedule for each row execute procedure holiday_check();

最后,一些测试:

-- OK
insert into schedule(during) values (tsrange('2012-11-26 08:00', '2012-11-26 09:00'));
INSERT 0 1

-- out of business hours
insert into schedule(during) values (tsrange('2012-11-26 04:00', '2012-11-26 05:00'));
ERROR:  new row for relation "schedule" violates check constraint "schedule_during_check"
DETAIL:  Failing row contains (12, ["2012-11-26 04:00:00","2012-11-26 05:00:00")).

-- End time can be only 8:30, 9:00, 9:30, ... 16:00, 16:30, 17:00, 17:30 or 18:00 exclusive
insert into schedule(during) values (tsrange('2012-11-26 08:00', '2012-11-26 09:10'));
ERROR:  new row for relation "schedule" violates check constraint "schedule_during_check"
DETAIL:  Failing row contains (13, ["2012-11-26 08:00:00","2012-11-26 09:10:00")).

-- Start time can be only 8:00 , 8:30, 9:00, 9:30, ... 16:00, 16:30, 17:00 or 17:30 inclusive
insert into schedule(during) values (tsrange('2012-11-26 11:24', '2012-11-26 13:00'));
ERROR:  new row for relation "schedule" violates check constraint "schedule_during_check"
DETAIL:  Failing row contains (14, ["2012-11-26 11:24:00","2012-11-26 13:00:00")).

-- holiday
insert into schedule(during) values (tsrange('2012-11-28 10:00', '2012-11-28 13:00'));
ERROR:  public holiday