Postgres constraint for unique datetime range

后端 未结 1 1198
野的像风
野的像风 2020-12-06 07:18

My table has two columns:

  1. startsAt
  2. endsAt

Both hold date and time. I want to make following constraint:

相关标签:
1条回答
  • 2020-12-06 07:45

    You can keep your separate timestamp columns and still use an exclusion constraint on an expression:

    CREATE TABLE tbl (
       tbl_id    serial PRIMARY KEY
     , starts_at timestamp
     , ends_at   timestamp
     , EXCLUDE USING gist (tsrange(starts_at, ends_at) WITH &&) -- no overlapping
    );
    

    Constructing a tsrange value without explicit bounds as tsrange(starts_at, ends_at) automatically assumes default bounds: including lower and excluding upper - '[)', which is typically best.

    SQL Fiddle.

    Related:

    • Preventing adjacent/overlapping entries with EXCLUDE in PostgreSQL

    Add constraint to existing table

    ALTER TABLE tbl ADD CONSTRAINT tbl_no_overlapping_time_ranges
    EXCLUDE USING gist (tsrange(starts_at, ends_at) WITH &&)
    

    Syntax details are the same as for CREATE TABLE.

    0 讨论(0)
提交回复
热议问题