You can define the default value for your column as the concatenation of S
and the regular sequence
, as shown below:
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
How to use sequences
How to use the to_char function.
source share