Skip site navigation (1) Skip section navigation (2)

Re: Primary Keys

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andrew Bell <acbell(at)iastate(dot)edu>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Primary Keys
Date: 2001-11-27 15:20:41
Message-ID: 18958.1006874441@sss.pgh.pa.us (view raw or flat)
Thread:
Lists: pgsql-novice
Andrew Bell <acbell(at)iastate(dot)edu> writes:
> Can someone tell me what advantage there is to creating a table with a 
> primary key rather than simply adding a unique index to the table at a 
> later time.

AFAIK, the only functional difference is that a primary key declaration
establishes a default column (or columns) for foreign key references
from other tables.  If you did CREATE TABLE foo (..., primary key(f1))
then another table could say just "REFERENCES foo" and f1 would be
understood to be the referenced column.  Without a primary key spec
you would always have to mention the referenced column explicitly.

BTW, I believe PRIMARY KEY also causes a NOT NULL constraint to be
attached to the columns, so it's not exactly equivalent to a unique
index.  It's equivalent to unique index + NOT NULL, except for the
REFERENCES convenience feature.

			regards, tom lane

In response to

Responses

pgsql-novice by date

Next:From: Stephan SzaboDate: 2001-11-27 15:43:28
Subject: Re: [HACKERS] upper and lower doesn't work with german
Previous:From: John BurskiDate: 2001-11-27 14:57:53
Subject: Re: Help with triggers

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group