12

I am using "PostgreSQL 9.3.5"

I have a Table(StackOverflowTable) with columns (SoId,SoName,SoDob).

I want a Sequence generator for column SoId which is a Alpha-numeric value.

I want to auto increment a Alpha-Numeric Value in postgresql.

For eg : SO10001, SO10002, SO10003.....SO99999.

Edit:

If tomorrow i need to generate a Sequence which can be as SO1000E100, SO1000E101,... and which has a good performance. Then what is the best solution!

1
  • 1
    If all values always have the prefix S0 I would not generate that value at all. I would rather use a serial column and a view that formats the generated (numeric) id in the way you want it. Requires less space and less overhead to generate the number. Commented Nov 25, 2014 at 8:43

3 Answers 3

10

Use sequences and default value for 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)
Sign up to request clarification or add additional context in comments.

1 Comment

It might be simpler to use START [ WITH ] 10000 instead of separate setval('xxx', 10000).
7

You can define default value of your column as a concatenation of S and a normal sequence as bellow:

CREATE SEQUENCE sequence_for_alpha_numeric
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;


CREATE TABLE table1
(
  alpha_num_auto_increment_col character varying NOT NULL,
  sample_data_col character varying,
  CONSTRAINT table1_pkey PRIMARY KEY (alpha_num_auto_increment_col)
)
;

ALTER TABLE table1 ALTER COLUMN alpha_num_auto_increment_col SET DEFAULT TO_CHAR(nextval('sequence_for_alpha_numeric'::regclass),'"S"fm000000');

Test:

                          ^
insert into table1 (sample_data_col) values ('test1');
insert into table1 (sample_data_col) values ('test2');
insert into table1 (sample_data_col) values ('test3');

select * from table1;
 alpha_num_auto_increment_col | sample_data_col
------------------------------+-----------------
 S000001                      | test1
 S000002                      | test2
 S000003                      | test3
(3 lignes)

How to use sequences

How to use to_char function.

1 Comment

Thanks for your response :). As FIFO i had voted Pavel Stehule Answer.
1

Create A sequence like below

 CREATE SEQUENCE seq_autoid
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 10000

Create A Function to generate alpha numeric id

create or replace function auto_id () returns varchar as $$
select 'SO'||nextval('seq_autoid')
$$ language sql 

and try this example table

create table AAA(id text ,namez text)

insert into AAA values (auto_id(),'MyName')
insert into AAA values (auto_id(),'MyName1')
insert into AAA values (auto_id(),'MyName2')

1 Comment

Thanks for your response :). As FIFO i had voted Pavel Stehule Answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.