This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am creating an auto increment PRIMARY KEY using a custom sequence of the PostgreSQL.
PostgreSQL already provides two data types for auto increment id, serial (4 bytes) & bigserial (8 bytes).
When we apply serial or bigserial to the particular column, internally it creates a default sequence object for that column and automatically it generates auto increment value for that column.
We can also create custom sequence object in the PostgreSQL and it also requires in some situation where we do not want to use default sequence value.
First, Create a sequence object:Starting with 100 and increment by 10
1 2 3 4 5 |
CREATE SEQUENCE seq_MyCustom_Id START 100 INCREMENT 10 NO MAXVALUE CACHE 1; |
Create sample table:
1 2 3 4 5 |
CREATE TABLE tbl_SampleID ( ID INTEGER PRIMARY KEY ,Name CHARACTER VARYING(50) ); |
Use this sequence object and Insert sample data:
1 2 3 4 5 |
INSERT INTO tbl_SampleID VALUES (NEXTVAL('seq_MyCustom_Id'),'Anvesh') ,(NEXTVAL('seq_MyCustom_Id'),'Roy') ,(NEXTVAL('seq_MyCustom_Id'),'Martin'); |