MySQL 错误代码:1062。重复条目“PRIMARY”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28702069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error Code: 1062. Duplicate entry 'PRIMARY'
提问by Mike
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:
所以,我的教授给了我将它插入数据库的表,但是当我执行他的代码时,MySQL 不断给出错误代码:1062。这是冲突表和插入:
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'
如果我删除该行会给我同样的错误,但会出现“Tallarines-20”
Sorry if there is any spell mistake. Thanks!
抱歉,如果有任何拼写错误。谢谢!
回答by Jonas Petersson
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.
您可能需要CATEGORIA
为表添加主键,ARTICULOS
因为您尝试多次插入具有相同主键的多行。
primary key (articulo,cod_fabricante, categoria)
回答by Denis Spalenza
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.
第 7INSERT
行和第 8行相等。您不能使用相同的主键输入多于一行。请注意,您的主键是 set: (articulate, cod_fabricante)
,因此任何具有相同articulate
和的行cod_fabricante
都会生成错误 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.
删除其中一行或更改其中一行的主键。
回答by John Cen
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.
此错误代码 1062 是由于重复条目。您正在尝试插入主键字段中已存在的值。最近,我通过在主键字段中添加 auto_increment 解决了这个问题。我遵循了这篇文章中提供的修复方法如何解决 mysql 错误代码:1062 重复条目?它对我有用。也帮你。
回答by m3ldEr7ake
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).
尝试将列设置为主键时遇到了同样的错误。我刚刚删除了该列并重新创建了它,这允许我将其分配为主键。这也解决了 #1075 错误,它要求自动增量列作为键(如果您尝试将列设置为自动增量)。
回答by Aman Aggarwal
You have error of duplicate key in second table ARTICULOS. you have primary key with combination of two columns (articulo,cod_fabricante).
您在第二个表 ARTICULOS 中出现重复键错误。你有两列组合的主键(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.
因此,所有行都是在这些列的组合中唯一定义的。从第二个表中删除重复的行或改为更改主键。