Alter table add primary key to existing table

PostreSQL tutorial – Add primary key

Add primary key to existing table

How to add primary key to existing table. Alter syntax and example.

Syntax

ALTER TABLE tabel_name 
ADD PRIMARY KEY (column_name);

Create table

CREATE TABLE users(
  userid numeric,
  username varchar(100),
  userpass varchar(250),
  usermail varchar(50)  
  );    

Example Add primary key

ALTER TABLE users 
ADD PRIMARY KEY (userid);

Alter table |
Drop table