PostgreSQL Autoincrement
Yes, SERIAL is the equivalent function.
CREATE TABLE foo ( id SERIAL, bar varchar);INSERT INTO foo (bar) VALUES ('blah');INSERT INTO foo (bar) VALUES ('blah');SELECT * FROM foo;+----------+| 1 | blah |+----------+| 2 | blah |+----------+
SERIAL is just a create table time macro around sequences. You can not alter SERIAL onto an existing column.
You can use any other integer data type, such as smallint
.
Example :
CREATE SEQUENCE user_id_seq;CREATE TABLE user ( user_id smallint NOT NULL DEFAULT nextval('user_id_seq'));ALTER SEQUENCE user_id_seq OWNED BY user.user_id;
Better to use your own data type, rather than user serial data type.
If you want to add sequence to id in the table which already exist you can use:
CREATE SEQUENCE user_id_seq;ALTER TABLE user ALTER user_id SET DEFAULT NEXTVAL('user_id_seq');