STARTING SERIAL / BIGSERIAL FIELDS FROM 1.

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

STARTING SERIAL / BIGSERIAL FIELDS FROM 1.

От:
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.
 
With respect,
Jorge Maldonado

Re: STARTING SERIAL / BIGSERIAL FIELDS FROM 1.

От:
Jure Kobal <j.kobal@gmx.com>
Дата:
For every serial/bigserial you have a table that should be named something like 
__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 _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 <esiotrot@gmail.com>
Дата:
2009/7/20 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.

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 

Re: STARTING SERIAL / BIGSERIAL FIELDS FROM 1.

От:
Jure Kobal <jure.kobal@gmx.com>
Дата:
Вложения:
The info for the serial is stored in a seperate table which name should be 
something like __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 <lacey.powers@commandprompt.com>
Дата:
Hello Jorge,

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

ALTER SEQUENCE  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 <esiotrot@gmail.com>
Дата:
2009/7/21 Lacey Powers :
> Hello Jorge,
>
> An even easier way to accomplish this would be to use the following:
>
> ALTER SEQUENCE  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 
FAQ