Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1a48tZ-0004ZR-4V for pgsql-sql@arkaria.postgresql.org; Wed, 02 Dec 2015 15:00:37 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1a48tY-0008Vr-9O for pgsql-sql@arkaria.postgresql.org; Wed, 02 Dec 2015 15:00:36 +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 1a48tV-0008Tk-Qv for pgsql-sql@postgresql.org; Wed, 02 Dec 2015 15:00:34 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1a48tS-0005VR-7S for pgsql-sql@postgresql.org; Wed, 02 Dec 2015 15:00:33 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 223F220D9E for ; Wed, 2 Dec 2015 10:00:26 -0500 (EST) Received: from frontend1 ([10.202.2.160]) by compute1.internal (MEProxy); Wed, 02 Dec 2015 10:00:26 -0500 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=OW9q9KD53Prm+Cw2F3PshSDXvjM=; b=LAtQ4F HGLTV/qBD8LtEWMg5s+AklYBgWc40xZdzDBfYLRIhAGbCpymXQkDG3jY6A77jN1J 7g6Z5KxHHoRX7KWJXoc0iY0Ed+PU6kPXfj/5Zfo+JikUW56N1r/9cTyJPLXHWh5I t0Ccy/w9d3cwp26U2u3HvggeMSHTE1P0rpaVI= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=OW9q9KD53Prm+Cw 2F3PshSDXvjM=; b=a+efNFarHXIRpH3vJcmjIGKwTcfpa6PrY8ipg7Qv+Hi/KjN v6a6eMZV61mPxK3qfEWo1VZK3hskJAvtnOmahrJkdryR2Cpr9pCWjEGv8d4NqkK0 6Qk/s81WUkqj0L7CLjwrhNmMg6WmoyHWLzUX3MU20AqxOAnkvC/rPIUbb8uo= X-Sasl-enc: BhWox4SdvFrN0yLjG0f3Osxs1q4v3aGPjnpi8kHSjdyT 1449068425 Received: from [192.168.1.2] (65-102-182-223.tukw.qwest.net [65.102.182.223]) by mail.messagingengine.com (Postfix) with ESMTPA id 86206C016F7; Wed, 2 Dec 2015 10:00:25 -0500 (EST) Subject: Re: unique constraint definition within create table To: Andreas Kretschmer , pgsql-sql@postgresql.org References: <20151202063621.GA5526@tux> From: Adrian Klaver Message-ID: <565F076E.20608@aklaver.com> Date: Wed, 2 Dec 2015 06:59:58 -0800 User-Agent: Mozilla/5.0 (X11; Linux i686; 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; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) 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/01/2015 10:36 PM, 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? http://www.postgresql.org/docs/9.4/interactive/sql-createtable.html Shows that expressions are not allowed in UNIQUE constraints. "UNIQUE ( column_name [, ... ] ) index_parameters whereas http://www.postgresql.org/docs/9.4/interactive/sql-createindex.html "CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ name ] ON table_name [ USING method ] ( { column_name | ( expression ) } ..." does. So no there is not a way to do that in the CREATE TABLE command. You can bundle the commands though: BEGIN; create table foo(u1 int,u2 int); create unique index idx_foo on foo(least(u1,u2),greatest(u1,u2)); COMMIT; to make sure they either both succeed or fail. > > Thx. > > Andreas > -- Adrian Klaver adrian.klaver@aklaver.com -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql