unique constraint definition within create table - Mailing list pgsql-sql

From Andreas Kretschmer
Subject unique constraint definition within create table
Date
Msg-id 20151202063621.GA5526@tux
Whole thread Raw
Responses Re: unique constraint definition within create table  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: unique constraint definition within create table  (Adrian Klaver <adrian.klaver@aklaver.com>)
Re: unique constraint definition within create table  ("David G. Johnston" <david.g.johnston@gmail.com>)
Re: unique constraint definition within create table  (Vik Fearing <vik@2ndquadrant.fr>)
List pgsql-sql
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?

Thx.

Andreas
-- 
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect.                              (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly."   (unknown)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°



pgsql-sql by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: Re: ERROR while creating new user - could not open relation mapping file global/pg_filenode.map
Next
From: Tom Lane
Date:
Subject: Re: unique constraint definition within create table