dbTalk Databases Forums  

sequence

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


Discuss sequence in the comp.databases.postgresql.novice forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Robert Morgan
 
Posts: n/a

Default sequence - 05-23-2004 , 04:47 PM






Is it possible to change an existing field to a sequence?

I have a field that contains ID numbers and I want this field to
autoincrement evrytime a new row is inserted.

The existing field is int8, (this database has been imported to
Postgresql using DBTools import wizard.)
Do I need to drop the existing column and create a new one using create
sequence.....?
and then I can use the nextval() in the insert query?

thanks
Bob





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

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


Reply With Quote
  #2  
Old   
Manuel Sugawara
 
Posts: n/a

Default Re: sequence - 05-23-2004 , 08:34 PM






Robert Morgan <robert_ (AT) ihug (DOT) co.nz> writes:

Quote:
Do I need to drop the existing column and create a new one using
create sequence.....?
No, or at least not necessarily. You can create the sequence and then
alter the existing column definition to get its default value from
that sequence, for instance, suppose that your table name is foo and
the column's name is bar:

create sequence new_seq;
select setval('new_seq', select max(bar) from foo);
alter table foo alter column bar set default nextval(new_seq');

Regards,
Manuel.

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match



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.