Re: Is a primary key made of a couple columns so much better than a unique() constraint ?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Martijn van Oosterhout <kleptog(at)svana(dot)org>
Cc: David Pradier <david(dot)pradier(at)clarisys(dot)fr>, pgsql-general(at)postgresql(dot)org
Subject: Re: Is a primary key made of a couple columns so much better than a unique() constraint ?
Date: 2005-10-10 15:22:11
Message-ID: 21047.1128957731@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Martijn van Oosterhout <kleptog(at)svana(dot)org> writes:
> On Mon, Oct 10, 2005 at 04:44:54PM +0200, David Pradier wrote:
>> i'd like to know if it is better to use a primary key made of a couple
>> columns, than to use a constraint UNIQUE() on this couple columns,
>> regarding the sake of postgresql.

> In PostgreSQL, both primary keys and UNIQUE constraints are implemented
> via UNIQUE indexes, ergo there is no difference...

Just for the sake of completeness, there are exactly two differences:

* PRIMARY KEY implies NOT NULL on the key columns; UNIQUE doesn't.

* PRIMARY KEY creates a default target for foreign key references,
ie, if you've declared a primary key then you can later just say
"REFERENCES mytab" instead of spelling out "REFERENCES mytab(keycol)".

So "UNIQUE + NOT NULL" is pretty dang close to the same as "PRIMARY
KEY", but not quite.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David Pradier 2005-10-10 15:30:26 Re: Is a primary key made of a couple columns so much better than a unique() constraint ?
Previous Message Michael Fuhr 2005-10-10 15:20:47 Re: PostgreSQL 8.1 vs. MySQL 5.0?