X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 65078D1B51F for ; Thu, 18 Sep 2003 18:39:41 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 25084-03 for ; Thu, 18 Sep 2003 15:39:10 -0300 (ADT) Received: from enforcer.solutionip.com (enforcerxii.solutionip.com [216.83.4.32]) by svr1.postgresql.org (Postfix) with ESMTP id C216DD1B500 for ; Thu, 18 Sep 2003 15:39:08 -0300 (ADT) Received: from [172.28.16.146] (helo=solutioninc.com) by enforcer.solutionip.com with esmtp (Exim 3.34 #1) id 1A03gH-0002bN-00 for pgsql-sql@postgresql.org; Thu, 18 Sep 2003 15:39:05 -0300 Message-ID: <3F69FC7F.1040103@solutioninc.com> Date: Thu, 18 Sep 2003 15:42:07 -0300 From: Andrew Milne User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax) X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Unique Constraint Based on Date Range Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/232 X-Sequence-Number: 15210 I'm looking to apply a unique constraint to a table such that field A must be unique based on the date range from Field B to Field C. This is for a rate based service model whereby, for example, $5 is worth 1 hour of Internet access. But next week, because of increased competition, $5 is worth 2 hours of Internet access. I want to maintain a history what $5 bought during a specific period of time. create table rates ( effective_date AS timestamp, expiry_date AS timestamp, cost AS numeric (12,2), access_time AS integer (in minutes) ); So for a given cost, there may not be a record where the effective date of one record overlaps the expiry date of another (and vice versa). Example record set (effective date, expiry date, cost, access_time): 2003-01-01 | 2003-01-15 | 5.00 | 60 2003-01-15 | infinity | 5.00 | 120 2003-01-01 | infinity | 1.00 | 10 An attempt to insert another 5.00 rate effective now would fail, because a 5.00 rate exists that doesn't expire (i.e. the expiry date would have to be updated to the effective date of the new record minus 1 second). I can enforce this from the front end, but a db constraint would be great.