Re: Varchar or integer primary key?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Rafael Charnovscki <charnovs(at)unesc(dot)rct-sc(dot)br>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Varchar or integer primary key?
Date: 2004-07-26 20:27:48
Message-ID: 9317.1090873668@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Rafael Charnovscki <charnovs(at)unesc(dot)rct-sc(dot)br> writes:
> I have a 6-digit integer number for a patient identifier which
> I intend to use as a primary key (PK) for a table. It also will
> be a foreign key in other tables. Do I have to use an integer data
> type as a domain for this PK or could I use a varchar(6)?

You don't *have to* use an integer column, but I would expect it to be
more compact and faster than the varchar alternative. How much faster
is hard to say without a lot more information about what you'll be doing
with it. You might try setting up a test database and experimenting.

The only really serious gotcha in this area is to make sure foreign key
columns are declared with exactly the same datatype as the referenced PK
column. Postgres will generally let you get away with using slightly
different types (for instance, int versus bigint or char versus varchar)
but you'll pay dearly in performance.

regards, tom lane

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Pradeepkumar, Pyatalo (IE10) 2004-07-27 02:29:38 Re: help with conversion functions
Previous Message Scott Marlowe 2004-07-26 20:23:09 Re: Varchar or integer primary key?