Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL auto increment pgadmin 4

Tags:

sql

I am trying to make a simple database with an number generator but why do I get the error below?

ERROR: syntax error at or near "AUTO_INCREMENT"
LINE 2: IDNumber int NOT NULL AUTO_INCREMENT,

Code:

CREATE TABLE Finance
(
    IDNumber int NOT NULL AUTO_INCREMENT,
    FinName varchar(50) NOT NULL,
    PRIMARY KEY(IDNumber)
);
like image 471
bhr Avatar asked Jan 25 '18 15:01

bhr


People also ask

Is there auto increment in PostgreSQL?

By simply setting our id column as SERIAL with PRIMARY KEY attached, Postgres will handle all the complicated behind-the-scenes work and automatically increment our id column with a unique, primary key value for every INSERT .

How do you auto increment a variable in SQL?

The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature. In the example above, the starting value for IDENTITY is 1, and it will increment by 1 for each new record. Tip: To specify that the "Personid" column should start at value 10 and increment by 5, change it to IDENTITY(10,5) .


3 Answers

The subject of the question mentions pgAdmin 4, so here's how to do it there.

First, add a column to your table, then click the little edit icon:

enter image description here

Then go to Constraints and select the Identity type:

enter image description here

This generates SQL similar to this:

CREATE TABLE public.my_table_name
(
    id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
    PRIMARY KEY (id)
);
like image 133
Johnny Oshika Avatar answered Oct 16 '22 12:10

Johnny Oshika


For Postgres you have to use SERIAL

CREATE TABLE Finance
(
    IDNumber SERIAL PRIMARY KEY,
    FinName varchar(50) NOT NULL
);
like image 18
waka Avatar answered Oct 16 '22 14:10

waka


IN pgadmin-2. step 01: create seq: enter image description here

and set info:

enter image description here

step 02: go to ID in table and set constraints

enter image description here

like image 16
Nguyen Duc Hai Avatar answered Oct 16 '22 14:10

Nguyen Duc Hai