Databases · Senior · Question 64 of 75
How can PostgreSQL prevent two overlapping bookings of the same room at the database level?
ALTER TABLE bookings ADD CONSTRAINT no_overlap
EXCLUDE USING gist (room_id WITH =, during WITH &&);- ✓Correct answer: With an exclusion constraint over a range column: no two rows may have the same room and overlapping time ranges
- 2With a unique index on
room_id - 3It's impossible; it must be checked in application code
Explanation
It's race-free, unlike checking for overlaps before insert. It needs the btree_gist extension for the equality part on room_id.