Error Code: 1062. Duplicate entry '2' for key 'PRIMARY'

Asked

Viewed 1,335 times

0

I am trying to update the local table with the data from the cursoProunine table with the following command :

Insert into local (idlocal, Uf, city) SELECT idlocal, uf_search, city_search FROM cursosprounine; but is giving error "Duplicate entry '2' for key PRIMARY, help me Please?

Tabela Local

Tabela Curso Prouni

1 answer

0


Your SELECT idlocal, uf_busca, cidade_busca FROM cursosprounine; is returning the result as in the second image you posted, correct ?! Note that there is more than one record with the idlocal with the value 2.

And you get the code with insert into local (idlocal, uf, cidade) i.e.: You want to create a new record for the local table with the result of your SELECT.

However, for the idlocal primary key column of the local table you are trying to enter the value '2' and there is already a record with this id '2' in your local table.

That’s exactly what the error is telling you: "Duplicate primary key value '2'".

In order to be able to accomplish what you want, you should return only one result from your SELECT, try adding a GROUP BY idlocal, it would look like this:

INSERT INTO local(idlocal, uf, cidade) SELECT idlocal, uf_busca, cidade_busca FROM cursosprounine GROUP BY idlocal;

  • Thanks Caio, it worked, thanks!!!

  • I’m glad it worked ! Have it :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.