Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

301
Views
How to create a column that is filled automatically based on a sequence in Postgresql?

is there any way to create a column that is half serial? that is imagine that I have the following table:

create table item (
     item_number   varchar(10)    PRIMARYKEY,

)

but I want the column item_number to be like this: ITEM001,ITEM002,ITEM003 .... so I want the word ITEM then a kind of a sequence after it.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

use a sequence with a default value:

create sequence item_number_seq;

create table item 
(
  item_number varchar(10) primary key default 'ITEM'||to_char(nextval('item_number_seq'), '00000')
);

But storing the same constant value for all rows is pretty much useless. Just use a regular sequence and add that constant when you display those values. Or use a view to to do that.

over 4 years ago · Santiago Trujillo Report

0

Do it at query time or at the presentation layer:

create table item (item_number serial primary key);

select 'ITEM' || to_char(item, '999')
from item
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!