2011-04-06 12 views
8

Sto tentando di modificare il valore minimo di una sequenza esistente.Modifica del valore minimo della sequenza di Postgres

Per prima cosa ho provato

ALTER SEQUENCE product_id_seq MINVALUE 10000; 

e ho ottenuto ERROR: START value (1) cannot be less than MINVALUE (10000).

Così ho provato

ALTER SEQUENCE product_id_seq MINVALUE 10000 RESTART WITH 10000; 

ma ho ottenuto lo stesso errore.

Ovviamente potrei semplicemente rilasciarlo e crearne uno nuovo, ma penso che ci dovrebbe essere un modo per farlo. Sto usando Postgres 8.4.7.

risposta

7

Come sull'impostazione them all in una sola volta:

ALTER SEQUENCE product_id_seq 
MINVALUE 10000 
START 10000 
RESTART 10000; 

che dovrebbe cambiare il minimo, a partire, e gli attuali valori di tutti a 10000 e quindi rendere tutto coerente.

+0

Grazie. Non so come ho perso quel parametro. =) – dasony

+1

@dasony: Probabilmente la strana denominazione di 'RESTART' ha causato qualche confusione, mi sarei aspettato che si chiamasse' CURRENT'. –

0

Ho eseguito il seguente test, La mia versione è 9.0.

--create sequence 
skytf=> CREATE SEQUENCE seq_test 
skytf->  START WITH 1 
skytf->  INCREMENT BY 1 
skytf->  NO MINVALUE 
skytf->  NO MAXVALUE 
skytf->  CACHE 1; 
CREATE SEQUENCE 

skytf=> \d seq_test 
      Sequence "skytf.seq_test" 
    Column  | Type |  Value   
---------------+---------+--------------------- 
sequence_name | name | seq_test 
last_value | bigint | 1 
start_value | bigint | 1 
increment_by | bigint | 1 
max_value  | bigint | 9223372036854775807 
min_value  | bigint | 1 
cache_value | bigint | 1 
log_cnt  | bigint | 1 
is_cycled  | boolean | f 
is_called  | boolean | f 


skytf=> select nextval('seq_test'); 
nextval 
--------- 
     1 
(1 row) 

--alter sequence 
skytf=> alter sequence seq_test restart with 100; 
ALTER SEQUENCE 
skytf=> \d seq_test 
      Sequence "skytf.seq_test" 
    Column  | Type |  Value   
---------------+---------+--------------------- 
sequence_name | name | seq_test 
last_value | bigint | 100 
start_value | bigint | 1 
increment_by | bigint | 1 
max_value  | bigint | 9223372036854775807 
min_value  | bigint | 1 
cache_value | bigint | 1 
log_cnt  | bigint | 1 
is_cycled  | boolean | f 
is_called  | boolean | f 

skytf=> select nextval('seq_test'); 
nextval 
--------- 
    100 
(1 row) 
1

PostgreSQL ha diversi functions that operate on sequences. Oltre alle altre indicazioni qui, è possibile utilizzare

SELECT setval('product_id_seq ', 10000); -- Next nextval() returns 10001 
Problemi correlati