开发者

How to auto-increment in PostgreSQL? [duplicate]

This question already has开发者_StackOverflow answers here: What's the PostgreSQL datatype equivalent to MySQL AUTO INCREMENT? (11 answers) Closed 8 years ago.

I have a table Login. It has the fields rank, username and password.

I want the rank field value to be auto incremented with respect to addition of username and password.

How do I do this in PostgreSQL ?


You are looking for a column with datatype Serial. See this page (bottom) for more information about that datatype.

So for example, your table definition could look like this:

CREATE TABLE yourtable (
    rank SERIAL NOT NULL,
    username VARCHAR(20) NOT NULL,
    password VARCHAR(50) NOT NULL
);


A Sequence can be created which will auto increment the value of rank column.

CREATE SEQUENCE rank_id_seq;

CREATE TABLE yourtable (
    rank INTEGER NOT NULL default nextval('rank_id_seq'),
    username VARCHAR(20) NOT NULL,
    password VARCHAR(50) NOT NULL
);

ALTER SEQUENCE rank_id_seq owned by yourtable.rank;


create table login (rank serial, username varchar(20), password varchar(20))

Serial datatype is what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜