Обсуждение: STARTING SERIAL / BIGSERIAL FIELDS FROM 1.

Поиск
Список
Период
Сортировка

STARTING SERIAL / BIGSERIAL FIELDS FROM 1.

От
JORGE MALDONADO
Дата:
How can a serial or bigserial field start from 1 again?
I need to manually remove all the records from a table and I need these kind of fields to start from 1 once again.
 
With respect,
Jorge Maldonado

Re: STARTING SERIAL / BIGSERIAL FIELDS FROM 1.

От
Jure Kobal
Дата:
For every serial/bigserial you have a table that should be named something like
<table_name>_<column_name>_seq where table_name is the name of the table in
which the serial is used and column_name is the name of the column that is the
serial. It can be that it will be only <table_name>_seq. But depends on how you
set up the table.
The table should look something like this:

 sequence_name | last_value | increment_by | ...
-------------------+------------+---------------+ ...
    album_seq     |        461   |            1      | ...

for what you want you will need to change the last_value column.


Regard
Jure

On Monday 20 of July 2009 20:54:33 JORGE MALDONADO wrote:
> How can a serial or bigserial field start from 1 again?
> I need to manually remove all the records from a table and I need these
> kind of fields to start from 1 once again.
>
> With respect,
> Jorge Maldonado

Re: STARTING SERIAL / BIGSERIAL FIELDS FROM 1.

От
Michael Wood
Дата:
2009/7/20 JORGE MALDONADO <jorgemal1960@gmail.com>:
> How can a serial or bigserial field start from 1 again?
> I need to manually remove all the records from a table and I need these kind
> of fields to start from 1 once again.

If you use pg_dump on the database you should see something like this:

SELECT pg_catalog.setval('table_column_seq', 123456789, true);

That means that the last value will be set to 123456789 if you restore
using this dump.

You can do something like this, except that you can't set it to 0, so
you need to do this instead:

SELECT pg_catalog.setval('table_column_seq', 1, false);

Then the next value used would be 1.

--
Michael Wood <esiotrot@gmail.com>

Re: STARTING SERIAL / BIGSERIAL FIELDS FROM 1.

От
Jure Kobal
Дата:
The info for the serial is stored in a seperate table which name should be
something like <table_name>_<column_name>_seq where table_name is the name of
the table and column_name the name of the serial you're using.
It should look something like

 sequence_name | last_value | increment_by | ...
-------------------+------------+---------------+ ...
    album_seq     |        461   |            1      | ...

you would need to change the last_value column.

Regards
Jure


On Monday 20 of July 2009 20:54:33 JORGE MALDONADO wrote:
> How can a serial or bigserial field start from 1 again?
> I need to manually remove all the records from a table and I need these
> kind of fields to start from 1 once again.
>
> With respect,
> Jorge Maldonado

Вложения

Re: STARTING SERIAL / BIGSERIAL FIELDS FROM 1.

От
Lacey Powers
Дата:
Hello Jorge,

An even easier way to accomplish this would be to use the following:

ALTER SEQUENCE <sequence_name> RESTART WITH 1;

As shown in the documentation, here:

http://www.postgresql.org/docs/8.3/interactive/sql-altersequence.html

Hope that helps. =)

Sincerely,

Lacey
> How can a serial or bigserial field start from 1 again?
> I need to manually remove all the records from a table and I need
> these kind of fields to start from 1 once again.
>
> With respect,
> Jorge Maldonado


--
Lacey Powers

The PostgreSQL Company - Command Prompt, Inc. 1.503.667.4564 ext 104
PostgreSQL Replication, Consulting, Custom Development, 24x7 support


Re: STARTING SERIAL / BIGSERIAL FIELDS FROM 1.

От
Michael Wood
Дата:
2009/7/21 Lacey Powers <lacey.powers@commandprompt.com>:
> Hello Jorge,
>
> An even easier way to accomplish this would be to use the following:
>
> ALTER SEQUENCE <sequence_name> RESTART WITH 1;
>
> As shown in the documentation, here:
>
> http://www.postgresql.org/docs/8.3/interactive/sql-altersequence.html

Ah, good point :)

--
Michael Wood <esiotrot@gmail.com>