Establishing a primary key

Started by Bob Pawleyalmost 19 years ago3 messagesgeneral
Jump to latest
#1Bob Pawley
rjpawley@shaw.ca

I have numerous entries in a column of table 1, some of which are duplicated.

I need to transfer this information to table 2 so that I have column that can be used as a primery key.

Any help is appreciated.

Bob Pawley

#2Charles Simard
tech@denarius.ca
In reply to: Bob Pawley (#1)
Re: Establishing a primary key

-----Original Message-----
From: pgsql-general-owner@postgresql.org

[mailto:pgsql-general-owner@postgresql.org]On Behalf Of Bob Pawley

Sent: 22 juin 2007 14:15
To: Postgresql
Subject: [GENERAL] Establishing a primary key

I have numerous entries in a column of table 1, some of which are

duplicated.

I need to transfer this information to table 2 so that I have column that

can be used as a primery key.

Any help is appreciated.

Bob Pawley

Something like:

Table1{
col1 text,
col2 text
}

Table2{
idtable2 serial,
col1 text,
col2 text,
primary key (idtable2)
}

INSERT INTO Table2 (col1, col2) SELECT col1, col2 FROM Table1;

#3Scott Marlowe
smarlowe@g2switchworks.com
In reply to: Bob Pawley (#1)
Re: Establishing a primary key

Bob Pawley wrote:

I have numerous entries in a column of table 1, some of which are
duplicated.

I need to transfer this information to table 2 so that I have column
that can be used as a primery key.

Any help is appreciated.

So, I take it you're wanting to have this so that table 1 stays as it
is, and table 2 gets the entries from table 1 made unique, and becomes
the parent of table 1?

If that's the case, you want something like this:

create table2 as select distinct idcolumn from table1;
alter table2 add primary key (idcolumn);
alter table1 add foreign key (idcolumn) references table2(idcolumn);

I think that's about right.