Re: unique constraint definition within create table

Поиск
Список
Период
Сортировка
От Vik Fearing
Тема Re: unique constraint definition within create table
Дата
Msg-id 566675D5.7060401@2ndquadrant.fr
обсуждение исходный текст
Ответ на unique constraint definition within create table  (Andreas Kretschmer <akretschmer@spamfence.net>)
Ответы Re: unique constraint definition within create table  (Andreas Kretschmer <akretschmer@spamfence.net>)
Список pgsql-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



В списке pgsql-sql по дате отправления:

Предыдущее
От: Sribeiro
Дата:
Сообщение: Re: ERROR while creating new user - could not open relation mapping file global/pg_filenode.map
Следующее
От: test
Дата:
Сообщение: 来自test的邮件