Thread: sequence

sequence

From
Robert Morgan
Date:
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





Re: sequence

From
Manuel Sugawara
Date:
Robert Morgan <robert_@ihug.co.nz> writes:

> 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.