DATABASE – Postgres unique primary key

Create an auto-incrementing surrogate primary key in PostgreSQL

CREATE TABLE fruit (
    fruit_id integer GENERATED ALWAYS AS IDENTITY (START WITH 100 INCREMENT BY 10) PRIMARY KEY,
    type character varying (10)
);

INSERT INTO fruit (type)
VALUES ('Apple'), ('Grape'), ('Watermelon');

SELECT * FROM fruit;

DROP TABLE fruit;

Leave a Reply

Your email address will not be published. Required fields are marked *