dbTalk Databases Forums  

Creating a new column with SERIAL as data type

comp.databases.postgresql.novice comp.databases.postgresql.novice


Discuss Creating a new column with SERIAL as data type in the comp.databases.postgresql.novice forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Kumar S
 
Posts: n/a

Default Creating a new column with SERIAL as data type - 09-19-2004 , 09:15 PM






Hi group,
I have an existing table with data loaded. Now I
wanted to create a column with SERIAL data type and
want to make that coulmn values as PRIMARY key.

I did the following:
friends=> alter table friend add column fr_id serial;
NOTICE: ALTER TABLE will create implicit sequence
"friend_fr_id_seq" for "serial" column "friend.fr_id"
ERROR: adding columns with defaults is not
implemented
HINT: Add the column, then use ALTER TABLE SET
DEFAULT.

Can any one suggest what is wrong here.

Thanks
Kumar




__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - Send 10MB messages!
http://promotions.yahoo.com/new_mail

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html


Reply With Quote
  #2  
Old   
Tom Lane
 
Posts: n/a

Default Re: Creating a new column with SERIAL as data type - 09-19-2004 , 10:32 PM






Kumar S <ps_postgres (AT) yahoo (DOT) com> writes:
Quote:
friends=> alter table friend add column fr_id serial;
NOTICE: ALTER TABLE will create implicit sequence "friend_fr_id_seq" for "serial" column "friend.fr_id"
ERROR: adding columns with defaults is not implemented
HINT: Add the column, then use ALTER TABLE SET DEFAULT.

Can any one suggest what is wrong here.
Just what it says: adding columns with defaults is not implemented.
(It is implemented in 8.0, but that won't help you today.) You can
do it by hand:

create sequence friend_fr_id_seq;
alter table friend add column fr_id int;
update friend set fr_id = nextval('friend_fr_id_seq');
alter table friend alter column fr_id set default nextval('friend_fr_id_seq');

This isn't an exact substitute since the sequence will appear as
a standalone object and not be hidden behind the "serial" column,
but it's functionally equivalent.

regards, tom lane

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



Reply With Quote
  #3  
Old   
Kumar S
 
Posts: n/a

Default Re: Creating a new column with SERIAL as data type - 09-19-2004 , 11:18 PM



Thank you very much Mr. Lane.
It worked.
Would you mind explaining the code that you sent.
I would appreciate and curious to learn in fact the
meaning of the pg/pl sql statements.
thank you.
Kumar.


--- Tom Lane <tgl (AT) sss (DOT) pgh.pa.us> wrote:

Quote:
Kumar S <ps_postgres (AT) yahoo (DOT) com> writes:
friends=> alter table friend add column fr_id
serial;
NOTICE: ALTER TABLE will create implicit sequence
"friend_fr_id_seq" for "serial" column
"friend.fr_id"
ERROR: adding columns with defaults is not
implemented
HINT: Add the column, then use ALTER TABLE SET
DEFAULT.

Can any one suggest what is wrong here.

Just what it says: adding columns with defaults is
not implemented.
(It is implemented in 8.0, but that won't help you
today.) You can
do it by hand:

create sequence friend_fr_id_seq;
alter table friend add column fr_id int;
update friend set fr_id =
nextval('friend_fr_id_seq');
alter table friend alter column fr_id set default
nextval('friend_fr_id_seq');

This isn't an exact substitute since the sequence
will appear as
a standalone object and not be hidden behind the
"serial" column,
but it's functionally equivalent.

regards, tom lane

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




__________________________________
Do you Yahoo!?
Yahoo! Mail is new and improved - Check it out!
http://promotions.yahoo.com/new_mail

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



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.