X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 57A5C9DCC5E for ; Thu, 2 Mar 2006 23:26:20 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 44427-09 for ; Thu, 2 Mar 2006 23:26:24 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by postgresql.org (Postfix) with SMTP id C41C59DCC44 for ; Thu, 2 Mar 2006 23:26:17 -0400 (AST) Received: (qmail invoked by alias); 03 Mar 2006 03:26:21 -0000 Received: from i3ED6C96F.versanet.de (EHLO [192.168.0.108]) [62.214.201.111] by mail.gmx.net (mp039) with SMTP; 03 Mar 2006 04:26:21 +0100 X-Authenticated: #14269776 Message-ID: <4407B7C1.7030101@gmx.net> Date: Fri, 03 Mar 2006 04:28:01 +0100 From: Andreas User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; de-AT; rv:1.8.0.1) Gecko/20060130 SeaMonkey/1.0 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: How to check date-interval constraints Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.06 required=5 tests=[AWL=0.060] X-Spam-Score: 0.06 X-Spam-Level: X-Archive-Number: 200603/55 X-Sequence-Number: 24401 Hi, I'd like to have a table that looks like this: my_option ( id serial primary key, myvalue double, valid_start timestamp, valid_stop timestamp ); I want to store values that are only valid in a given start-stop-interval so I could find a date-specific value for NOW() or some other given date. select myvalue from my_option where somedate between valid_start and valid_stop; How can I have a constraint, that prohibits nesting or overlapping intervals? 1 7 2006-1-1 2006-1-31 2 9 2006-2-1 2006-2-28 OK 3 5 2006-1-10 2006-1-20 BAD lies within line 1 4 3 2006-1-20 2006-2-10 BAD starts within line 1 and ends in line 2 To make it even more interesting, it'd be nice to add a type-column so I could ask: select myvalue from my_option where now() between valid_start and valid_stop AND mytype=42; Then interval should ONLY not overlap with other intervals of the SAME type.