2015-02-24 114 views
4

因此,我的教授給了我一張表來將它插入到數據庫中,但是當我執行他的代碼時,MySQL不斷地給出錯誤代碼:1062. 這裏是衝突表和插入件:錯誤代碼:1062.重複項'PRIMARY'

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); 

的錯誤:

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

如果我刪除該行給了我同樣的錯誤,但與 'Tallarines-20'

很抱歉,如果有任何法術的錯誤。謝謝!

+1

對我的回答有問題嗎?或者你爲什麼不接受? – 2015-03-10 16:31:10

+0

@JonasPetersson DenisSpalenza擊敗了它... – 2017-08-16 01:00:24

回答

1

第7和第INSERT行相等。您不能使用相同的主鍵輸入多行。需要注意的是你的主鍵是集:(articulate, cod_fabricante),所以用同樣的articulatecod_fabricante任何行會產生錯誤1062

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

刪除線的一個或更改其中一個主鍵。

+1

您可以像Jonas所說的那樣更改主鍵:'主鍵(關鍵字,cod_fabricante,categoria)' – 2015-02-24 18:24:29

4

您試圖插入具有相同主鍵的兩行。

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

如果您嘗試將主鍵設置爲包含主鍵重複(我的情況)。 – 2017-01-18 10:07:01

+0

https://www.youtube.com/watch?v = UrYLYV7WSHM應該無法做到這一點? – Lealo 2017-08-17 16:30:20

0

您在第二個表格ARTICULOS中有重複鍵的錯誤。 你有主鍵和兩列組合(articulo,cod_fabricante)。

因此,所有行都是以這些列的組合唯一定義的。從第二個表中刪除重複的行或改變主鍵。

1

此錯誤代碼1062是因爲重複條目。您正嘗試插入主鍵字段中已存在的值。最近,我通過將auto_increment添加到主鍵字段來解決此問題。我跟着這個帖子提供的修復程序how to solve mysql error code : 1062 duplicate entry?它爲我工作。也幫助你。

1

嘗試將列設置爲主鍵時出現同樣的錯誤。我剛剛刪除了該列並重新創建了它,允許將其分配爲主鍵。這也解決了#1075錯誤,它需要自動增量列作爲鍵(如果您嘗試將列設置爲自動增量)。

相關問題