Database – Two Ways to Add a Primary Key Constraint in Postgres

Add column and constraint in one step

ALTER TABLE employees

ADD EmployeeID INT GENERATED ALWAYS AS IDENTITY(START WITH 1000 INCREMENT BY 1) PRIMARY KEY;

Add column constraint in two steps

ALTER TABLE employees
ADD EmployeeID INT GENERATED ALWAYS AS IDENTITY(START WITH 1000 INCREMENT BY 1)

ALTER TABLE employees
ADD CONSTRAINT PK_EmployeeID PRIMARY KEY (EmployeeID);

Leave a Reply

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