ReaL_HyDRA
ReaL_HyDRA

Reputation: 365

How to add auto increment in postgreSQL?

I am new to PostgreSQL. I was trying to create a table and tried to add a primary key and auto increment in same column and came up with an ERROR

ERROR:  syntax error at or near "SERIAL"
LINE 2:  upID int SERIAL primary key

Below is my Query

create table tblIK(
    upID int SERIAL primary key,
    upName varchar(100) NOT NULL,
    upMin varchar(100) NOT NULL,
    upMax varchar(100) NOT NULL,
    upYEAR Date NOT NULL,
    upMi varchar(100)NOT NULL,
    upIK varchar(100)NOT NULL
)

Upvotes: 0

Views: 1864

Answers (2)

brandonris1
brandonris1

Reputation: 455

If you are using Django to construct the tables in the DB and you want an auto incrementing primary key, Django by default will creat an ID column which is an auto incrementing integer field. Saves you some lines of code :)

Upvotes: 0

paul41
paul41

Reputation: 676

SERIAL will create an integer column for you so you don't need to specify it.

create table tblIK(
    upID SERIAL primary key,
    upName varchar(100) NOT NULL,
    upMin varchar(100) NOT NULL,
    upMax varchar(100) NOT NULL,
    upYEAR Date NOT NULL,
    upMi varchar(100)NOT NULL,
    upIK varchar(100)NOT NULL
)

Upvotes: 1

Related Questions