0

How do I auto-increment an ID of a member in my table together with a character with it for example: M_01, M_02, M_03:

CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
);
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228

1 Answers1

1

The answer is: Don't.

Use a basic serial column. You can always format the column on output.
A sensible table definition could look like this (added more suggestions):

CREATE TABLE company(
    company_id serial PRIMARY KEY
  , birth_year int    NOT NULL
  , company    text   NOT NULL
  , address    text
  , salary     int            -- in cents
);

Then:

SELECT to_char(companyid, '"M_"FM00000')  -- produces M_00001 etc.
FROM   company;
Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228