Getting started with PostgreSQL SERIAL
PostgreSQL serial data type is used to define the auto-increment number of columns in a table, PostgreSQL serial will generate a serial sequence of integer numbers.
A sequence is mainly used as the primary key column in a table.
Serial data types are not actual types, but merely a notational convenience for creating non-nullable integer columns with sequences attached.
PostgreSQL has three serial types
SMALLSERIAL 2 bytes 1 to 32,767
SERIAL 1 to 2,147,483,647
BIGSERIAL 8 bytes 1 to 9,223,372,036,854,775,807
We can create a table using serial as follows:
CREATE TABLE user (
id SERIAL
);
if you want a serial column with a unique constraint or a primary key it should be specified like any data type in PostgreSQL.
CREATE TABLE user(
ID SERIAL PRIMARY KEY,
name TEXT NOT NULL,
surname TEX NOT NULL,
);