所以,我的教授给了我表格将其插入数据库但是当我执行他的代码时,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 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);
错误:
Error Code: 1062. Duplicate entry 'Macarrones-20' for key 'PRIMARY'
如果我删除该行会给我同样的错误,但使用' Tallarines-20'
对不起,如果有任何拼写错误。谢谢!
答案 0 :(得分:5)
您正尝试使用相同的主键插入两行。
INSERT INTO ARTICULOS VALUES ('Tallarines',20, 2, 'Primera',120,100,100);
INSERT INTO ARTICULOS VALUES ('Tallarines',20, 1, 'Segunda',99,50,100);
您可能需要将CATEGORIA
添加到表ARTICULOS
的主键中,因为您尝试多次插入多个具有相同主键的行。
primary key (articulo,cod_fabricante, categoria)
答案 1 :(得分:2)
第7行和第8行INSERT
行相等。您不能使用相同的主键输入多行。请注意,您的主键是设置:(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);
删除其中一行或更改其中一行的主键。
答案 2 :(得分:1)
此错误代码1062是由于重复条目。您正在尝试插入主键字段中已存在的值。最近,我通过将auto_increment添加到主键字段来解决了这个问题。我按照这篇文章how to solve mysql error code : 1062 duplicate entry?中提供的修复程序对我有用。也帮助你。
答案 3 :(得分:1)
尝试将列设置为主键时遇到了同样的错误。我刚刚删除了该列并重新创建了它,这使我可以将其指定为主键。这也解决了#1075错误,它需要一个自动增量列作为键(如果您尝试将列设置为自动增量)。
答案 4 :(得分:0)
第二个表ARTICULOS中存在重复键错误。 你有两个组合的主键(articulo,cod_fabricante)。
因此,所有行都是在这些列的组合中唯一定义的。从第二个表中删除重复的行或改为更改主键。