Adding serial type to a table

Поиск
Список
Период
Сортировка
От Scott Marlowe
Тема Adding serial type to a table
Дата
Msg-id Pine.LNX.4.33.0204261706090.8484-100000@css120.ihs.com
обсуждение исходный текст
Ответ на Speeding query  (Uros Gruber <uros@sir-mag.com>)
Список pgsql-general
Someone needed to add a serial type to a table.  Here's the quick dirty,
lazy dba way:

say table t1 has a structure like so:

create table t1 (field1 text, id int);

and data in it, and we want id to be a serial (autoincrementing etc...)
field.

do this:

create table t2 (field1 text, id serial);

Now, assuming that all the data in t1 has a unique id, we can just do
this:

insert into t2 (select * from t1);

and voila, our table is populated.  One small problem, the current value
of the associate sequence is still set to the original number (1 I think).

So, we do this:

select setval('t2_id_seq',(select max(id) from t2));

And now we have our sequence ready to go.

Good luck!


В списке pgsql-general по дате отправления:

Предыдущее
От: Jeff Post
Дата:
Сообщение: Help Porting Pg Perl Module
Следующее
От: Lincoln Yeoh
Дата:
Сообщение: Re: delete column