dbTalk Databases Forums  

Change primary key in Postgres 7.3?

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss Change primary key in Postgres 7.3? in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Michael Hannon
 
Posts: n/a

Default Change primary key in Postgres 7.3? - 10-12-2004 , 08:10 PM






Greetings. We're running Postgres 7.3 on an Intel linux box (Redhat
Enterprise server, version 3.0). We find ourselves in an awkward
position: we have a database of attributes relating to students that
uses as its primary key the ID number of the student. This is awkward
for the following reasons.

Our university used to use social-security numbers for student ID's.
They stopped doing that a few years ago, but didn't force the change on
existing students. Recently they've made a forced, retroactive change
such that ALL students, past and present, now have a student ID that's
not related to social-security number.

I think this a well-justified change, but, unfortunately for us, it
makes many of the primary keys in our database invalid. This problem is
compounded by the fact that the programmer that set up our Postgres
databases has moved on to another job.

Our current programmer would like to start from scratch, redefine the
schema, rebuild the database, etc. Unfortunately, there are a number of
high-profile applications that depend on the database, and many of them
would surely get broken by this kind of transition.

We expect that we WILL eventually rebuild the database, but right now
we're looking for a quick fix. Our current programmer tells me that he
can't find a way to simply change the primary key "in place" in Postgres.

Is there a way to do this?

Thanks.

- Mike
--
Michael Hannon mailto:hannon (AT) physics (DOT) ucdavis.edu
Dept. of Physics 530.752.4966
University of California 530.752.4717 FAX
Davis, CA 95616-8677


---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings


Reply With Quote
  #2  
Old   
Gavin M. Roy
 
Posts: n/a

Default Re: Change primary key in Postgres 7.3? - 10-12-2004 , 08:27 PM






DROP CONSTRAINT should be able to drop your pkey and as long as your
data supports your new key... you should be set

Gavin

Michael Hannon wrote:

Quote:
Greetings. We're running Postgres 7.3 on an Intel linux box (Redhat
Enterprise server, version 3.0). We find ourselves in an awkward
position: we have a database of attributes relating to students that
uses as its primary key the ID number of the student. This is awkward
for the following reasons.

Our university used to use social-security numbers for student ID's.
They stopped doing that a few years ago, but didn't force the change
on existing students. Recently they've made a forced, retroactive
change such that ALL students, past and present, now have a student ID
that's not related to social-security number.

I think this a well-justified change, but, unfortunately for us, it
makes many of the primary keys in our database invalid. This problem
is compounded by the fact that the programmer that set up our Postgres
databases has moved on to another job.

Our current programmer would like to start from scratch, redefine the
schema, rebuild the database, etc. Unfortunately, there are a number
of high-profile applications that depend on the database, and many of
them would surely get broken by this kind of transition.

We expect that we WILL eventually rebuild the database, but right now
we're looking for a quick fix. Our current programmer tells me that
he can't find a way to simply change the primary key "in place" in
Postgres.

Is there a way to do this?

Thanks.

- Mike


---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings



Reply With Quote
  #3  
Old   
Michael Fuhr
 
Posts: n/a

Default Re: Change primary key in Postgres 7.3? - 10-12-2004 , 09:09 PM



On Tue, Oct 12, 2004 at 06:10:12PM -0700, Michael Hannon wrote:
Quote:
We expect that we WILL eventually rebuild the database, but right now
we're looking for a quick fix. Our current programmer tells me that he
can't find a way to simply change the primary key "in place" in Postgres.
Does ALTER TABLE not work?

http://www.postgresql.org/docs/7.3/s...ltertable.html

Whatever you decide to do, hopefully you have a development system
on which to test your changes.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster



Reply With Quote
  #4  
Old   
Mike Mascari
 
Posts: n/a

Default Re: Change primary key in Postgres 7.3? - 10-12-2004 , 09:42 PM



Michael Fuhr wrote:
Quote:
On Tue, Oct 12, 2004 at 06:10:12PM -0700, Michael Hannon wrote:

We expect that we WILL eventually rebuild the database, but right now
we're looking for a quick fix. Our current programmer tells me that he
can't find a way to simply change the primary key "in place" in Postgres.

Does ALTER TABLE not work?

http://www.postgresql.org/docs/7.3/s...ltertable.html

Whatever you decide to do, hopefully you have a development system
on which to test your changes.
I'm not sure what the original poster is asking precisely, but if they
have declared all foreign keys referencing the primary table's primary
key with ON UPDATE CASCADE, then all they need to do is update the
primary table's primary key.

[test@lexus] create table foo (key integer not null primary key);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
"foo_pkey" for table "foo"
CREATE TABLE
[test@lexus] create table bar(other integer not null primary key,
foo_key integer not null references foo(key) on delete cascade on update
cascade);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
"bar_pkey" for table "bar"
CREATE TABLE
[test@lexus] insert into foo values (1);
INSERT 2433708 1
[test@lexus] insert into bar values (100, 1);
INSERT 2433709 1
[test@lexus] update foo set key = 2;
UPDATE 1
[test@lexus] select * from bar;
other | foo_key
-------+---------
100 | 2
(1 row)


Is that what the original poster is trying to achieve?

Mike Mascari

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend



Reply With Quote
  #5  
Old   
John
 
Posts: n/a

Default Re: Change primary key in Postgres 7.3? - 10-13-2004 , 08:56 AM



Michael Hannon wrote:

Quote:
Greetings. We're running Postgres 7.3 on an Intel linux box (Redhat
Enterprise server, version 3.0). We find ourselves in an awkward
position: we have a database of attributes relating to students that
uses as its primary key the ID number of the student. This is awkward
for the following reasons.

Our university used to use social-security numbers for student ID's.
They stopped doing that a few years ago, but didn't force the change on
existing students. Recently they've made a forced, retroactive change
such that ALL students, past and present, now have a student ID that's
not related to social-security number.

I think this a well-justified change, but, unfortunately for us, it
makes many of the primary keys in our database invalid. This problem is
compounded by the fact that the programmer that set up our Postgres
databases has moved on to another job.

Our current programmer would like to start from scratch, redefine the
schema, rebuild the database, etc. Unfortunately, there are a number of
high-profile applications that depend on the database, and many of them
would surely get broken by this kind of transition.

We expect that we WILL eventually rebuild the database, but right now
we're looking for a quick fix. Our current programmer tells me that he
can't find a way to simply change the primary key "in place" in Postgres.

Is there a way to do this?

Thanks.

- Mike
It can be done (see other posts), but I suspect that your programmer has
a good reason to be reluctant.

<unwanted_advice>

Let your programmer start from scratch on a new server. Once he has it
set up how he wants it, and has tested it against the other
applications, make the switch. If it doesn't work right away, you can
always switch back.

</unwanted_advice>

John


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.