错误代码:1062。密钥'PRIMARY'的重复条目'1'

时间:2021-03-28 07:37:12

I have a problem on this error message, when i try this:

我有这个错误消息的问题,当我尝试这个:

INSERT INTO `PROGETTO`.`UFFICIO-INFORMAZIONI` (`ID`, `viale`, `num_civico`,  
`data_apertura`, `data_chiusura`, `orario_apertura`, `orario_chiusura`, 
`telefono`, `mail`, `web`, `Nome-paese`, `Comune`) 
VALUES (1, 'Viale Cogel ', '120', '2012-05-21', '2012-09-30', '08:00', '23:30',
'461801243', 'informazioni@bolzano.it', 'Bolzanoturismo.it', 'Bolzano', 'BZ')

Error Code: 1062. Duplicate entry '1' for key 'PRIMARY'

错误代码:1062。密钥'PRIMARY'的重复条目'1'

I haven't auto_increment data, PLEASE HELP me!

我没有auto_increment数据,请帮助我!

This is the table related, UFFICIO-INFORMAZIONI

这是与表相关的,UFFICIO-INFORMAZIONI

CREATE  TABLE IF NOT EXISTS `PROGETTO`.`UFFICIO-INFORMAZIONI` (
  `ID` INT(11) NOT NULL ,
  `viale` VARCHAR(45) NULL ,
  `num_civico` VARCHAR(5) NULL ,
  `data_apertura` DATE NULL ,
  `data_chiusura` DATE NULL ,
  `orario_apertura` TIME NULL ,
  `orario_chiusura` TIME NULL ,
  `telefono` VARCHAR(15) NULL ,
  `mail` VARCHAR(100) NULL ,
  `web` VARCHAR(100) NULL ,
  `Nome-paese` VARCHAR(45) NOT NULL ,
  `Comune` CHAR(2) NOT NULL ,
  PRIMARY KEY (`ID`) ,
  INDEX `Nome_paese` (`Nome-paese` ASC) ,
  INDEX `Comune` (`Comune` ASC) ,
  CONSTRAINT `Nome_paese`
    FOREIGN KEY (`Nome-paese` )
    REFERENCES `PROGETTO`.`PAESE` (`Nome-paese` )
    ON DELETE NO ACTION
    ON UPDATE CASCADE,
  CONSTRAINT `Comune`
    FOREIGN KEY (`Comune` )
    REFERENCES `PROGETTO`.`PAESE` (`Comune` )
    ON DELETE NO ACTION
    ON UPDATE CASCADE)
ENGINE = InnoDB

INSERT INTO

插入

INSERT INTO `PROGETTO`.`UFFICIO-INFORMAZIONI` (`ID`, `viale`, `num_civico`, `data_apertura`, `data_chiusura`, `orario_apertura`, `orario_chiusura`, `telefono`, `mail`, `web`, `Nome-paese`, `Comune`) VALUES (1, 'Viale Cogel ', '120', '2012-05-21', '2012-09-30', '08:00', '23:30', '461801243', 'informazioni@bolzano.it', 'Bolzanoturismo.it', 'Bolzano', 'BZ');
    INSERT INTO `PROGETTO`.`UFFICIO-INFORMAZIONI` (`ID`, `viale`, `num_civico`, `data_apertura`, `data_chiusura`, `orario_apertura`, `orario_chiusura`, `telefono`, `mail`, `web`, `Nome-paese`, `Comune`) VALUES (2, 'Via Olmo', '45', '2012-05-01', '2012-09-30', '08:00', '23:30', '393495169301', 'informazioni@lech.it', 'Lechinformation.it', 'Lech', 'BZ');
    INSERT INTO `PROGETTO`.`UFFICIO-INFORMAZIONI` (`ID`, `viale`, `num_civico`, `data_apertura`, `data_chiusura`, `orario_apertura`, `orario_chiusura`, `telefono`, `mail`, `web`, `Nome-paese`, `Comune`) VALUES (3, 'Via Quercia', '37', '2012-05-11', '2012-09-30', '08:00', '23:30', '393381679321', 'info@trento.it', 'Trentoinformaiozni.it', 'Trento', 'TN');
    INSERT INTO `PROGETTO`.`UFFICIO-INFORMAZIONI` (`ID`, `viale`, `num_civico`, `data_apertura`, `data_chiusura`, `orario_apertura`, `orario_chiusura`, `telefono`, `mail`, `web`, `Nome-paese`, `Comune`) VALUES (4, 'Via Atene', '76', '2012-06-01', '2012-09-15', '08:00', '23:30', '39349361345', 'info@sanmartinodicastrozza.it', 'SanMartino.it', 'San Martino di Castrozza', 'TN');
    INSERT INTO `PROGETTO`.`UFFICIO-INFORMAZIONI` (`ID`, `viale`, `num_civico`, `data_apertura`, `data_chiusura`, `orario_apertura`, `orario_chiusura`, `telefono`, `mail`, `web`, `Nome-paese`, `Comune`) VALUES (5, 'Via Salice', '45', '2012-05-01', '2012-09-20', '08:00', '23:30', NULL, 'info@pejo.it', 'Pejoturismo.it', 'Pejo', 'TN');
    INSERT INTO `PROGETTO`.`UFFICIO-INFORMAZIONI` (`ID`, `viale`, `num_civico`, `data_apertura`, `data_chiusura`, `orario_apertura`, `orario_chiusura`, `telefono`, `mail`, `web`, `Nome-paese`, `Comune`) VALUES (6, 'Piazza Sempreverde', '34', '2012-05-15', '2012-09-15', '08:00', '23:30', '392516789', 'info@ortisei.it', 'Ortisei.it', 'Ortisei', 'BZ');

6 个解决方案

#1


17  

The main reason why the error has been generated is because there is already an existing value of 1 for the column ID in which you define it as PRIMARY KEY (values are unique) in the table you are inserting.

生成错误的主要原因是因为列ID中已存在值1,您在其中将其定义为要插入的表中的PRIMARY KEY(值是唯一的)。

Why not set the column ID as AUTO_INCREMENT?

为什么不将列ID设置为AUTO_INCREMENT?

CREATE  TABLE IF NOT EXISTS `PROGETTO`.`UFFICIO-INFORMAZIONI` (
  `ID` INT(11) NOT NULL AUTO_INCREMENT,
  `viale` VARCHAR(45) NULL ,
   .....

and when you are inserting record, you can now skip the column ID

当您插入记录时,您现在可以跳过列ID

INSERT INTO `PROGETTO`.`UFFICIO-INFORMAZIONI` (`viale`, `num_civico`, ...) 
VALUES ('Viale Cogel ', '120', ...)

#2


14  

If you are using PHPMyAdmin You can be solved this issue by doing this:

如果您使用的是PHPMyAdmin,可以通过以下方式解决此问题:

CAUTION: Don't use this solution if you want to maintain existing records in your table.

注意:如果要在表中维护现有记录,请不要使用此解决方案。

Step 1: Select database export method to custom:

第1步:选择数据库导出方法为自定义:

错误代码:1062。密钥'PRIMARY'的重复条目'1'

Step 2: Please make sure to check truncate table before insert in data creation options:

第2步:请确保在插入数据创建选项之前检查truncate table:

错误代码:1062。密钥'PRIMARY'的重复条目'1'

Now you are able to import this database successfully.

现在您可以成功导入此数据库。

#3


7  

If you are trying to populate a table from a SQL dump, make sure that the table listed in the "INSERT INTO" statements of the dump is the same one you are trying to populate. Opening "MyTable" and importing with a SQL dump will throw exactly that kind of error if the dump is trying to put entries into "MyOtherTable", which may already have entries.

如果您尝试从SQL转储填充表,请确保转储的“INSERT INTO”语句中列出的表与您尝试填充的表相同。如果转储尝试将条目放入“MyOtherTable”(可能已有条目),则打开“MyTable”并使用SQL转储导入将完全抛出该类错误。

#4


2  

The problem is related with your file - you are trying to create a DB using a copy - at the top of your file you will find something like this:

问题与您的文件有关 - 您正在尝试使用副本创建数据库 - 在文件的顶部,您会发现类似这样的内容:

CREATE DATABASE IF NOT EXISTS *THE_NAME_OF_YOUR_DB* DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci; USE *THE_NAME_OF_YOUR_DB*;

创建数据库,如果不是EXISTS * THE_NAME_OF_YOUR_DB *默认字符集latin1 COLLATE latin1_general_ci;使用* THE_NAME_OF_YOUR_DB *;

and I'm sure that you already have a DB with this name - IN THE SAME SERVER - please check. Just change the name OR ERASE THIS LINE!

我相信你已经有一个这个名字的数据库 - 在同一台服务器 - 请检查。只需更改名称或删除此行!

#5


2  

When I get this kind of error I had to update the data type by a notch. For Example, if I have it as "tiny int" change it to "small int" ~ Nita

当我遇到这种错误时,我不得不更新数据类型。例如,如果我将它作为“tiny int”将其更改为“small int”~Nita

#6


1  

I just encountered the same issue but here it seemed to come from the fact that I declared the ID-column to be UNsigned and that in combination with an ID-value of '0' (zero) caused the import to fail...

我刚刚遇到了同样的问题,但这里似乎是因为我声明ID列是UNsigned,并且ID值为'0'(零)导致导入失败...

So by changing the value of every ID (PK-column) that I'd declared '0' and every corresponding FK to the new value, my issue was solved.

因此,通过将我声明为'0'的每个ID(PK列)的值和每个相应的FK更改为新值,我的问题就解决了。

#1


17  

The main reason why the error has been generated is because there is already an existing value of 1 for the column ID in which you define it as PRIMARY KEY (values are unique) in the table you are inserting.

生成错误的主要原因是因为列ID中已存在值1,您在其中将其定义为要插入的表中的PRIMARY KEY(值是唯一的)。

Why not set the column ID as AUTO_INCREMENT?

为什么不将列ID设置为AUTO_INCREMENT?

CREATE  TABLE IF NOT EXISTS `PROGETTO`.`UFFICIO-INFORMAZIONI` (
  `ID` INT(11) NOT NULL AUTO_INCREMENT,
  `viale` VARCHAR(45) NULL ,
   .....

and when you are inserting record, you can now skip the column ID

当您插入记录时,您现在可以跳过列ID

INSERT INTO `PROGETTO`.`UFFICIO-INFORMAZIONI` (`viale`, `num_civico`, ...) 
VALUES ('Viale Cogel ', '120', ...)

#2


14  

If you are using PHPMyAdmin You can be solved this issue by doing this:

如果您使用的是PHPMyAdmin,可以通过以下方式解决此问题:

CAUTION: Don't use this solution if you want to maintain existing records in your table.

注意:如果要在表中维护现有记录,请不要使用此解决方案。

Step 1: Select database export method to custom:

第1步:选择数据库导出方法为自定义:

错误代码:1062。密钥'PRIMARY'的重复条目'1'

Step 2: Please make sure to check truncate table before insert in data creation options:

第2步:请确保在插入数据创建选项之前检查truncate table:

错误代码:1062。密钥'PRIMARY'的重复条目'1'

Now you are able to import this database successfully.

现在您可以成功导入此数据库。

#3


7  

If you are trying to populate a table from a SQL dump, make sure that the table listed in the "INSERT INTO" statements of the dump is the same one you are trying to populate. Opening "MyTable" and importing with a SQL dump will throw exactly that kind of error if the dump is trying to put entries into "MyOtherTable", which may already have entries.

如果您尝试从SQL转储填充表,请确保转储的“INSERT INTO”语句中列出的表与您尝试填充的表相同。如果转储尝试将条目放入“MyOtherTable”(可能已有条目),则打开“MyTable”并使用SQL转储导入将完全抛出该类错误。

#4


2  

The problem is related with your file - you are trying to create a DB using a copy - at the top of your file you will find something like this:

问题与您的文件有关 - 您正在尝试使用副本创建数据库 - 在文件的顶部,您会发现类似这样的内容:

CREATE DATABASE IF NOT EXISTS *THE_NAME_OF_YOUR_DB* DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci; USE *THE_NAME_OF_YOUR_DB*;

创建数据库,如果不是EXISTS * THE_NAME_OF_YOUR_DB *默认字符集latin1 COLLATE latin1_general_ci;使用* THE_NAME_OF_YOUR_DB *;

and I'm sure that you already have a DB with this name - IN THE SAME SERVER - please check. Just change the name OR ERASE THIS LINE!

我相信你已经有一个这个名字的数据库 - 在同一台服务器 - 请检查。只需更改名称或删除此行!

#5


2  

When I get this kind of error I had to update the data type by a notch. For Example, if I have it as "tiny int" change it to "small int" ~ Nita

当我遇到这种错误时,我不得不更新数据类型。例如,如果我将它作为“tiny int”将其更改为“small int”~Nita

#6


1  

I just encountered the same issue but here it seemed to come from the fact that I declared the ID-column to be UNsigned and that in combination with an ID-value of '0' (zero) caused the import to fail...

我刚刚遇到了同样的问题,但这里似乎是因为我声明ID列是UNsigned,并且ID值为'0'(零)导致导入失败...

So by changing the value of every ID (PK-column) that I'd declared '0' and every corresponding FK to the new value, my issue was solved.

因此,通过将我声明为'0'的每个ID(PK列)的值和每个相应的FK更改为新值,我的问题就解决了。