3

I have a test application coded in Java for creating an indexed and non indexed table in a MySQL, PostgreSQL, Oracle and Firebird database (Amongst other things).

Is it simply a case that PostgreSQL doesnt allow the auto increment feature? If not, what is the normal procedure for having an indexed coloumn?

Thanks in advance

3 Answers 3

6

You may use SERIAL in PostgreSQL to generate auto increment field,

For eg:-

CREATE TABLE user (
userid SERIAL PRIMARY KEY,
username VARCHAR(16) UNIQUE NOT NULL
)

This will create userid as auto-increment primary key indexed. If you don't want this as primary key, just remove PRIMARY KEY.

Sign up to request clarification or add additional context in comments.

Comments

3

Use a column of type SERIAL. It works the same way as AUTOINCREMEMT on some other DBs. (Check the docs for other features you can use with it.)

Comments

3

With current Postgres, you can just use SERIAL for the column type.

With older versions of Postgres, you can implement this using SEQUENCE; the relevant procedure is:

CREATE SEQUENCE mytable_myid_seq;
ALTER TABLE mytable ALTER COLUMN myid SET DEFAULT NEXTVAL('mytable_myid_seq');

A good article on this is MySQL versus PostgreSQL: Adding an Auto-Increment Column to a Table

1 Comment

Thanks, my first time with postgre :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.