Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 198849F927B for ; Mon, 8 Oct 2007 11:55:28 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 94953-08 for ; Mon, 8 Oct 2007 11:55:20 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by postgresql.org (Postfix) with ESMTP id AA9849F9277 for ; Mon, 8 Oct 2007 11:55:27 -0300 (ADT) Received: from news.hub.org (news.hub.org [200.46.204.72]) by news.hub.org (8.14.1/8.14.1) with ESMTP id l98EtKA2001804 for ; Mon, 8 Oct 2007 11:55:20 -0300 (ADT) (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.14.1/8.14.1/Submit) id l98EkZSk094076 for pgsql-sql@postgresql.org; Mon, 8 Oct 2007 11:46:35 -0300 (ADT) (envelope-from news) From: =?iso-8859-1?q?Rodrigo_De_Le=F3n?= X-Newsgroups: pgsql.sql Subject: Re: UNIQUEness and time interval Date: Mon, 08 Oct 2007 14:46:40 -0000 Organization: http://groups.google.com Lines: 42 Message-ID: <1191854800.465075.65670@y42g2000hsy.googlegroups.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" X-Complaints-To: groups-abuse@google.com In-Reply-To: User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: y42g2000hsy.googlegroups.com; posting-host=200.75.231.230; posting-account=ps2QrAMAAAA6_jCuRt2JEIpn5Otqf_w0 To: pgsql-sql@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200710/42 X-Sequence-Number: 29414 On Oct 4, 4:45 am, Nicolas Boullis wrote: > I'd like to define a table with a "name", a "start_date" and a > "stop_date" columns, with a constraint that ensures that 2 records with > ovelapping dates don't share the same name. Is there a way to define > such a constraint? CREATE TABLE T( NAME TEXT ,START_DATE DATE ,STOP_DATE DATE ); CREATE OR REPLACE FUNCTION F() RETURNS TRIGGER AS $$ BEGIN IF EXISTS ( SELECT 1 FROM T WHERE NAME = NEW.NAME AND (START_DATE, STOP_DATE) OVERLAPS (NEW.START_DATE, NEW.STOP_DATE) OR STOP_DATE = NEW.START_DATE ) THEN RAISE EXCEPTION 'WHATCHA DOIN'' FOO'; RETURN NULL; END IF; RETURN NEW; END; $$ LANGUAGE PLPGSQL; CREATE TRIGGER T_T BEFORE INSERT ON T FOR EACH ROW EXECUTE PROCEDURE F(); INSERT INTO T VALUES ('FOO','2007-1-1','2007-1-3'); -- OK INSERT INTO T VALUES ('FOO','2007-1-4','2007-1-6'); -- OK INSERT INTO T VALUES ('FOO','2007-1-6','2007-1-8'); -- WILL BARF