'Auto increment table column

Using Postgres, I'm trying to use AUTO_INCREMENT to number my primary key automatically in SQL. However, it gives me an error.

CREATE TABLE Staff   (
  ID        INTEGER NOT NULL AUTO_INCREMENT,
  Name      VARCHAR(40) NOT NULL,
  PRIMARY KEY (ID)
);

The error:

********** Error **********
ERROR: syntax error at or near "AUTO_INCREMENT"
SQL state: 42601
Character: 63

Any idea why?



Solution 1:[1]

Postgres 10 or later

(serial columns remain unchanged, see below.)

Consider a standard-SQL IDENTITY column. Can be GENERATED BY DEFAULT or (stricter) GENERATED ALWAYS.
Basics in the manual for CREATE TABLE.
Details in this blog entry by its principal author Peter Eisentraut.

Create table with IDENTITY column

CREATE TABLE staff (
  staff_id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY
, staff    text NOT NULL
);

Add IDENTITY column to existing table

Table may or may not be populated with rows.

ALTER TABLE staff ADD COLUMN staff_id int GENERATED ALWAYS AS IDENTITY;

To also make it the PK at the same time (table can't have a PK yet):

ALTER TABLE staff ADD COLUMN staff_id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY;

See:

Replace serial with IDENTITY column

See:

You can override system values or user input in INSERT commands with OVERRIDING {SYSTEM|USER} VALUE.

Postgres 9.6 or older

(Still supported in newer versions, too.)
Use the serial pseudo data type:

CREATE TABLE staff (
  staff_id serial PRIMARY KEY,
, staff    text NOT NULL
);

It creates and attaches the sequence object automatically and sets the DEFAULT to nextval() from the sequence. It does all you need.

I use legal, lower-case, unquoted identifiers in my examples. Makes your life with Postgres easier.

Solution 2:[2]

You do not specify which RDBMS you are using, however, in SQL Server you can use this syntax:

CREATE TABLE [dbo].[Staff]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] VARCHAR(40) NOT NULL,
CONSTRAINT [ID] PRIMARY KEY CLUSTERED 
(
[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

Solution 3:[3]

In the SQL server database you can use Identity(1,1) like this:

CREATE TABLE Staff
(
    ID INT IDENTITY(1,1) NOT NULL,
    Name VARCHAR(40) NOT NULL,
    PRIMARY KEY (ID)
);

Solution 4:[4]

PostgreSQL: If you absolutely must have your own auto increment value:

Then use a sequence:

ericlesc_schools=> drop table yar;
DROP TABLE
ericlesc_schools=> drop sequence user_id_seq;
DROP SEQUENCE
ericlesc_schools=> create sequence user_id_seq;
CREATE SEQUENCE
ericlesc_schools=> create table yar(
                   id int default nextval('user_id_seq'), 
                   foobar varchar);
CREATE TABLE
ericlesc_schools=> insert into yar (foobar) values('hey alex');
INSERT 0 1
ericlesc_schools=> insert into yar (foobar) values('hey what derick');
INSERT 0 1
ericlesc_schools=> insert into yar (foobar) values('I look like a hushpuppy');
INSERT 0 1

ericlesc_schools=> select * from yar;
 id |     foobar      
----+-----------------
  1 | hey alex
  2 | hey what derick
  3 | I look like a hushpuppy
(3 rows)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Barry Kaye
Solution 3 Eric Leschinski
Solution 4 Eric Leschinski