Re: Unique Constraint Based on Date Range

From: Manfred Koizar <mkoi-pg(at)aon(dot)at>
To: Kevin Houle <kevin(at)houle(dot)org>
Cc: Andrew Milne <amilne(at)solutioninc(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Unique Constraint Based on Date Range
Date: 2003-09-22 09:14:37
Message-ID: 0hetmvk1gcl1nlhg710hevr1g7tk7998v2@email.aon.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Sat, 20 Sep 2003 18:55:34 -0400, Kevin Houle <kevin(at)houle(dot)org>
wrote:
> SELECT INTO result * FROM table_rates WHERE
> effective_date >= NEW.effective_date AND
> expiry_date <= NEW.expiry_date AND
> cost = NEW.cost;
> IF FOUND THEN
> RAISE EXCEPTION ''record overlaps with existing record'';
> END IF;

This only catches complete inclusion of an old range in the new one.
new b---------------------e
old b--------------e

Try
WHERE effective_date < NEW.expiry_date
AND expiry_date > NEW.effective_date

which also detects
new b--------------------e
old b-------------------e
old b------------------e
old b--------------------------------e

Servus
Manfred

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Huxton 2003-09-22 09:50:34 Re: Backup error - Pls help
Previous Message Kumar 2003-09-22 09:01:00 Backup error - Pls help