gpt4 book ai didi

mysql - 错误代码 : 1062. 重复条目 'PRIMARY'

转载 作者:可可西里 更新时间:2023-11-01 07:17:56 28 4
gpt4 key购买 nike

所以,我的教授给了我一些表格,让我将其插入数据库,但是当我执行他的代码时,MySQL 不断给出错误代码:1062。这是冲突表和插入:

表格

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”

如有拼写错误请见谅。谢谢!

最佳答案

您正试图插入具有相同主键的两行。

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)

关于mysql - 错误代码 : 1062. 重复条目 'PRIMARY',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28702069/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com