Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1a6Bb9-0005gL-No for pgsql-sql@arkaria.postgresql.org; Tue, 08 Dec 2015 06:18:03 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1a6Bb9-0005CS-AS for pgsql-sql@arkaria.postgresql.org; Tue, 08 Dec 2015 06:18:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1a6BaA-00045m-JR for pgsql-sql@postgresql.org; Tue, 08 Dec 2015 06:17:02 +0000 Received: from fr1.2ndquadrant.fr ([2001:41d0:2:127c::1]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1a6Ba7-0004Sr-2y for pgsql-sql@postgresql.org; Tue, 08 Dec 2015 06:17:02 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by fr1.2ndquadrant.fr (Postfix) with ESMTP id 4B2376ACA0; Tue, 8 Dec 2015 07:16:56 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at fr1.2ndquadrant.fr Received: from fr1.2ndquadrant.fr ([127.0.0.1]) by localhost (fr1.2ndquadrant.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ERRqgTmRgTDF; Tue, 8 Dec 2015 07:16:54 +0100 (CET) Received: from [11.0.0.137] (110.171-242-81.adsl-dyn.isp.belgacom.be [81.242.171.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: charles) by fr1.2ndquadrant.fr (Postfix) with ESMTPSA id B99AA646D8; Tue, 8 Dec 2015 07:16:54 +0100 (CET) Subject: Re: unique constraint definition within create table To: Andreas Kretschmer , pgsql-sql@postgresql.org References: <20151202063621.GA5526@tux> From: Vik Fearing Message-ID: <566675D5.7060401@2ndquadrant.fr> Date: Tue, 8 Dec 2015 07:16:53 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 MIME-Version: 1.0 In-Reply-To: <20151202063621.GA5526@tux> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org On 12/02/2015 07:36 AM, Andreas Kretschmer wrote: > Hi @ll, > > i'm trying to create a table with 2 int-columns and a constraint that a > pair of (x,y) cannot be as (y,x) inserted: > > test=# create table foo(u1 int,u2 int, unique (least(u1,u2),greatest(u1,u2))); > ERROR: syntax error at or near "(" > LINE 1: create table foo(u1 int,u2 int, unique (least(u1,u2),greates... > > > I know, i can solve that in this way: > > test=*# create table foo(u1 int,u2 int); > CREATE TABLE > test=*# create unique index idx_foo on foo(least(u1,u2),greatest(u1,u2)); > CREATE INDEX > > > But is there a way to define the unique constraint within the create table - command? You can use exclusion constraints for this. CREATE TABLE foo ( u1 integer, u2 integer, EXCLUDE USING btree ( least(u1, u2) WITH =, greatest(u1, u2) WITH =) ); -- Vik Fearing +33 6 46 75 15 36 http://2ndQuadrant.fr PostgreSQL : Expertise, Formation et Support -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql