postgres primary key change change

0

Hello having a created table I would like to know if I can change the primary key for example for my table where I want anho to be the new primary key instead of id

id | anho | modelo
------------------
 1 | 2010 | new 

 2 | 2015 | new2

 3 | 2018 | new

 4 | 2034 | new2
    
asked by NEFEGAGO 05.07.2018 в 16:09
source

2 answers

0

I'll leave you the following code:

-- 1. ELIMINANDO LA CLAVE PRIMARIA
ALTER TABLE mi_tabla DROP CONSTRAINT clave_primaria_actual
-- 2. Creando la nueva clave primaria
ALTER TABLE mi_tabla ADD PRIMARY KEY  nueva_clave_primaria

I hope it works for you ... you just have to change the table and your primary keys .. !!

    
answered by 05.07.2018 в 16:29
0

I have solved it in the following way:

--elimino la primary key correspondiente a la tabla
ALTER TABLE {NombreDeTabla} DROP CONSTRAINT {NombreDeTabla}_pkey;

-- luego asigno la primary key a la tabla con el nuevo nuevo campo(anho)
ALTER  TABLE  {NombreDeTabla} ADD PRIMARY KEY (anho);
  

Note: Replace {TableName} with the name of your table

    
answered by 05.07.2018 в 17:59