Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
299 views
Welcome To Ask or Share your Answers For Others

1 Answer

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...