Re: unique constraint definition within create table

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andreas Kretschmer <akretschmer(at)spamfence(dot)net>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: unique constraint definition within create table
Date: 2015-12-02 14:46:09
Message-ID: 14838.1449067569@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Andreas Kretschmer <akretschmer(at)spamfence(dot)net> writes:
> 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?

No. Per SQL standard, the argument of a UNIQUE (or PRIMARY KEY)
constraint can only be a list of column names.

We would consider relaxing that, except it would break the
information_schema views for such constraints.

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Adrian Klaver 2015-12-02 14:59:58 Re: unique constraint definition within create table
Previous Message Andreas Kretschmer 2015-12-02 06:36:21 unique constraint definition within create table