Error Code: 1062. Duplicate entry 'PRIMARY'
Asked Answered
A

5

7

So, my professor gave me tables to insert it in a database but when I execute his code, MySQL is constantly giving the Error Code: 1062. Here is the conflict tables and the inserts:

TABLES

CREATE TABLE FABRICANTES(
COD_FABRICANTE integer NOT NULL,
NOMBRE         VARCHAR(15), 
PAIS           VARCHAR(15),
primary key (cod_fabricante)
);

CREATE TABLE ARTICULOS(
ARTICULO       VARCHAR(20)NOT NULL,
COD_FABRICANTE integer NOT NULL,
PESO           integer NOT NULL ,
CATEGORIA      VARCHAR(10) NOT NULL,
PRECIO_VENTA   integer,
PRECIO_COSTO   integer,
EXISTENCIAS    integer,
primary key (articulo,cod_fabricante),
foreign key (cod_fabricante) references Fabricantes(cod_fabricante)
);

INSERT INTO:

INSERT INTO FABRICANTES VALUES(10,'CALVO', 'ESPAÑA');
INSERT INTO FABRICANTES VALUES(15,'LU', 'BELGICA');
INSERT INTO FABRICANTES VALUES(20,'BARILLA', 'ITALIA');
INSERT INTO FABRICANTES VALUES(25,'GALLO', 'ESPAÑA');
INSERT INTO FABRICANTES VALUES(30,'PRESIDENT', 'FRANCIA');

INSERT INTO ARTICULOS VALUES ('Macarrones',20, 1, 'Primera',100,98,120);
INSERT INTO ARTICULOS VALUES ('Tallarines',20, 2, 'Primera',120,100,100);
INSERT INTO ARTICULOS VALUES ('Tallarines',20, 1, 'Segunda',99,50,100);
INSERT INTO ARTICULOS VALUES ('Macarrones',20, 1, 'Tercera',80,50,100);
INSERT INTO ARTICULOS VALUES ('Atún',10, 3, 'Primera',200,150,220);
INSERT INTO ARTICULOS VALUES ('Atún',10, 3, 'Segunda',150,100,220);
INSERT INTO ARTICULOS VALUES ('Atún',10, 3, 'Tercera',100,50,220);
INSERT INTO ARTICULOS VALUES ('Sardinillas',10, 1,'Primera',250,200,200);
INSERT INTO ARTICULOS VALUES ('Sardinillas',10, 1,'Segunda',200,160,200);
INSERT INTO ARTICULOS VALUES ('Sardinillas',10, 1,'Tercera',100,150,220);
INSERT INTO ARTICULOS VALUES ('Mejillones',10, 1, 'Tercera',90,50,200);
INSERT INTO ARTICULOS VALUES ('Mejillones',10, 1, 'Primera',200,150,300);
INSERT INTO ARTICULOS VALUES ('Macarrones',25, 1, 'Primera',90,68,150);
INSERT INTO ARTICULOS VALUES ('Tallarines',25, 1, 'Primera',100,90,100);
INSERT INTO ARTICULOS VALUES ('Fideos',25, 1, 'Segunda',75,50,100);
INSERT INTO ARTICULOS VALUES ('Fideos',25, 1, 'Primera',100,80,100);
INSERT INTO ARTICULOS VALUES ('Galletas Cuadradas',15, 1, 'Primera',100,80,100);
INSERT INTO ARTICULOS VALUES ('Galletas Cuadradas',15, 1, 'Segunda',70,50,100);
INSERT INTO ARTICULOS VALUES ('Galletas Cuadradas',15, 1, 'Tercera',50,40,100);
INSERT INTO ARTICULOS VALUES ('Barquillos',15, 1, 'Primera',100,80,100);
INSERT INTO ARTICULOS VALUES ('Barquillos',15, 1, 'Segunda',100,80,100);
INSERT INTO ARTICULOS VALUES ('Canutillos',15, 2, 'Primera',170,150,110);
INSERT INTO ARTICULOS VALUES ('Canutillos',15, 2, 'Segunda',120,150,110);
INSERT INTO ARTICULOS VALUES ('Leche entera',30, 1, 'Primera',110,100,300);
INSERT INTO ARTICULOS VALUES ('Leche desnat.',30, 1, 'Primera',120,100,300);
INSERT INTO ARTICULOS VALUES ('Leche semi.',30, 1, 'Primera',130,110,300);
INSERT INTO ARTICULOS VALUES ('Leche entera',30, 2, 'Primera',210,200,300);
INSERT INTO ARTICULOS VALUES ('Leche desnat.',30, 2, 'Primera',220,200,300);
INSERT INTO ARTICULOS VALUES ('Leche semi.',30, 2, 'Primera',230,210,300);
INSERT INTO ARTICULOS VALUES ('Mantequilla',30, 1, 'Primera',510,400,200);
INSERT INTO ARTICULOS VALUES ('Mantequilla',30, 1, 'Segunda',450,340,200);

The ERROR:

Error Code: 1062. Duplicate entry 'Macarrones-20' for key 'PRIMARY'

If I delete that row gives me the same error but with 'Tallarines-20'

Sorry if there is any spell mistake. Thanks!

Alcoran answered 24/2, 2015 at 17:14 Comment(2)
Was it a problem with my answer, or why did you unaccept it?Inpour
@JonasPetersson DenisSpalenza beat you to it...Frederik
I
6

You are trying to insert two rows with the same primary key.

INSERT INTO ARTICULOS VALUES ('Tallarines',20, 2, 'Primera',120,100,100);
INSERT INTO ARTICULOS VALUES ('Tallarines',20, 1, 'Segunda',99,50,100);

You would probably need to add CATEGORIA to your primary key for table ARTICULOS because you are trying to insert multiple rows with the same primary key multiple times.

primary key (articulo,cod_fabricante, categoria)
Inpour answered 24/2, 2015 at 17:26 Comment(2)
This can also happen if you are trying to set as primary key a column that contains duplicate (my case).Diverge
youtube.com/watch?v=UrYLYV7WSHM Should not be able to do that?Inesinescapable
E
2

7th and 8th INSERT rows are equal. You can not enter more than one row with the same primary key. Note that your primary key is the set: (articulate, cod_fabricante), so any line with the same articulate and cod_fabricante will generate Error 1062.

INSERT INTO ARTICULOS VALUES ('Tallarines',20, 2, 'Primera',120,100,100);
INSERT INTO ARTICULOS VALUES ('Tallarines',20, 1, 'Segunda',99,50,100);

Remove one of the lines or change the primary key of one of them.

Emporium answered 24/2, 2015 at 17:17 Comment(1)
You can change primary key like Jonas said: primary key (articulo,cod_fabricante, categoria)Emporium
S
1

This error code 1062 is because of the duplicate entry. You are trying to insert a value which is already exists in the primary key field. Recently, I solved this issue by adding auto_increment to primary key field. I followed the fix provided in this post how to solve mysql error code : 1062 duplicate entry? it worked for me. Help you too.

Sammysamoan answered 21/1, 2016 at 14:48 Comment(0)
T
1

I had the same error when trying to set a column as the primary key. I just deleted the column and recreated it which allowed me to assign it as the primary key. This also resolves the # 1075 error where it requires an auto increment column to be a key (if you try to set the column for auto increment).

Tensive answered 6/7, 2016 at 16:15 Comment(0)
D
0

You have error of duplicate key in second table ARTICULOS. you have primary key with combination of two columns (articulo,cod_fabricante).

So all rows are uniquely defined in combination of these columns. remove duplicate rows from second table or change primary key instead.

Dwelling answered 2/7, 2015 at 17:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.