agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedFrom: Vik Fearing <vik@2ndquadrant.fr>
To: Andreas Kretschmer <akretschmer@spamfence.net>
To: pgsql-sql@postgresql.org
Subject: Re: unique constraint definition within create table
Date: Tue, 8 Dec 2015 07:16:53 +0100
Message-ID: <566675D5.7060401@2ndquadrant.fr> (raw)
In-Reply-To: <20151202063621.GA5526@tux>
References: <20151202063621.GA5526@tux>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>
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
view thread (6+ messages) latest in thread
Message-ID: <566675D5.7060401@2ndquadrant.fr>
Permalink: ../566675D5.7060401@2ndquadrant.fr/
Also on: postgresql.org/message-id/566675D5.7060401@2ndquadrant.fr
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: vik@2ndquadrant.fr, akretschmer@spamfence.net
Subject: Re: unique constraint definition within create table
In-Reply-To: <566675D5.7060401@2ndquadrant.fr>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox