Re: An easy question about creating a primary key

From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: "Ligia Pimentel" <lmpimentel(at)yahoo(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: An easy question about creating a primary key
Date: 2001-12-03 19:20:07
Message-ID: web-519076@davinci.ethosmedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-odbc pgsql-sql

Ligia,

> I want to create a primary key constraint on an existing table. The
> documentation says I can't . Please confirm. If this is true... How
> can I
> rename the existing table so I can create the new one and copy the
> data?

FYI, this question is more appropriate for the NOVICE list.

You would use the same method that you use to drop and recreate the
table for other reasons:

CREATE TABLE tablea_temp AS
SELECT * FROM tablea;

DROP tablea;

CREATE tablea (
primary_key SERIAL ...
<snip>
);

INSERT INTO tablea (column list)
SELECT (column list) FROM tablea_temp;

And don't forget to re-build your indexes!

-Josh Berkus

______AGLIO DATABASE SOLUTIONS___________________________
Josh Berkus
Complete information technology josh(at)agliodbs(dot)com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Manuel Sugawara 2001-12-03 19:41:25 date formatting and tab-complete patch
Previous Message Stephan Szabo 2001-12-03 19:16:14 Re: An easy question about creating a primary key

Browse pgsql-odbc by date

  From Date Subject
Next Message Christopher Kings-Lynne 2001-12-04 01:36:18 Re: An easy question about creating a primary key
Previous Message Stephan Szabo 2001-12-03 19:16:14 Re: An easy question about creating a primary key

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2001-12-03 19:23:07 Re: Error in executing plpgsql function
Previous Message Stephan Szabo 2001-12-03 19:16:14 Re: An easy question about creating a primary key