PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

Come aumentare automaticamente il valore alfanumerico in postgresql?

Usa sequenze e valore predefinito per id:

postgres=# CREATE SEQUENCE xxx;
CREATE SEQUENCE
postgres=# SELECT setval('xxx', 10000);
 setval 
--------
  10000
(1 row)

postgres=# CREATE TABLE foo(id text PRIMARY KEY 
                                    CHECK (id ~ '^SO[0-9]+$' ) 
                                    DEFAULT 'SO'  || nextval('xxx'), 
                            b integer);
CREATE TABLE
postgres=# insert into foo(b) values(10);
INSERT 0 1
postgres=# insert into foo(b) values(20); 
INSERT 0 1
postgres=# SELECT * FROM foo;
   id    | b  
---------+----
 SO10001 | 10
 SO10002 | 20
(2 rows)