How to create a named Primary Key constraint in Postgres?

1

I'm continuing to create a primary keyless table initially. Then I add a id field of serial type and try to create the constraint for the Primary Key (PK):

create table macaco(
  nome varchar(50),
  idade integer
);

--adicionando o id do tipo serial
ALTER TABLE "macaco" ADD COLUMN id SERIAL;

--criando a primary key 
ALTER TABLE "macaco" ADD PRIMARY KEY (id);

As I do above, it creates the PK with the name of macaco_pkey . So far so good. But what if I want to create the constraint with the name id_macaco_pk , what should I do?

    
asked by anonymous 16.08.2018 / 17:22

1 answer

0

What you want is this command:

ALTER TABLE macaco ADD CONSTRAINT id_macaco_pk PRIMARY KEY (id);

See here working in SQLFiddle.

    
16.08.2018 / 18:05