Data structure for non-overlapping ranges within a single dimension

前端 未结 8 1378
再見小時候
再見小時候 2020-12-30 11:26

I need a data structure that can store non-overlapping ranges within a single dimension. The entire range of the dimension need not be completely covered.

An example

8条回答
  •  既然无缘
    2020-12-30 12:22

    If you are lucky (!) enough to be using Postgres, you can use a tstzrange column, and apply a constraint to prevent overlaps. The bonus of using a range type is that it will inherently prevent start being greater than finish.

    ALTER TABLE "booking" 
    ADD CONSTRAINT "overlapping_bookings" 
    EXCLUDE USING gist ("period" WITH &&, "room" WITH =);
    

    You may need to CREATE EXTENSION IF NOT EXISTS btree_gist, as creating a gist using && is not supported without that extension.

提交回复
热议问题