Re: PRIMARY KEY on a *group* of columns imply that each

From: Sebastian Böck <sebastianboeck(at)freenet(dot)de>
To: Stephane Bortzmeyer <bortzmeyer(at)nic(dot)fr>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: PRIMARY KEY on a *group* of columns imply that each
Date: 2005-04-27 14:50:23
Message-ID: 426FA6AF.9050101@freenet.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Stephane Bortzmeyer wrote:
> On Wed, Apr 27, 2005 at 10:26:30AM -0400,
> Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote
> a message of 9 lines which said:
>
>
>>If that's what you want, declare it as UNIQUE not PRIMARY KEY.
>
>
> As shown by Patrick TJ McPhee, it does not work:
>
> tests=> create table x (
> tests(> name TEXT NOT NULL,
> tests(> address INET,
> tests(> CONSTRAINT na UNIQUE (name, address)
> tests(> );
> NOTICE: CREATE TABLE / UNIQUE will create implicit index "na" for table "x"
> CREATE TABLE
> tests=> INSERT INTO x (name) values ('foobar');
> INSERT 45380 1
> tests=> INSERT INTO x (name) values ('foobar');
> INSERT 45381 1
> tests=> INSERT INTO x (name) values ('foobar');
> INSERT 45382 1
> tests=> INSERT INTO x (name) values ('foobar');
> INSERT 45383 1
> tests=> select * from x;
> name | address
> --------+---------
> foobar |
> foobar |
> foobar |
> foobar |
> (4 rows)
>

If i understand correctly, you want something like:

create table x (
name TEXT NOT NULL PRIMARY KEY,
address INET
);

CREATE UNIQUE INDEX na ON x (name, address) WHERE address IS NULL;

HTH

Sebastian

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Stephane Bortzmeyer 2005-04-27 14:57:53 Re: PRIMARY KEY on a *group* of columns imply that each column is NOT
Previous Message mmiranda 2005-04-27 14:45:44 After insert trigger question