How to auto increment id with a character

醉酒当歌 提交于 2019-12-16 18:02:28

问题


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
);

回答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;


来源:https://stackoverflow.com/questions/22892695/how-to-auto-increment-id-with-a-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!