问题
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