MySQL 错误代码:1062。键“PRIMARY”的重复条目“1”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14628269/
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 '1' for key 'PRIMARY'
提问by FrancescoN
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', '[email protected]', '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!
我没有自动增量数据,请帮助我!
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', '[email protected]', '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', '[email protected]', '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', '[email protected]', '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', '[email protected]', '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, '[email protected]', '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', '[email protected]', 'Ortisei.it', 'Ortisei', 'BZ');
采纳答案by John Woo
The mainreason 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.
该主为什么已经产生错误的原因是因为已经有一个现有值1
的列ID
在其中将其定义为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', ...)
回答by Indrasinh Bihola
If you are using PHPMyAdminYou 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:
第一步:选择数据库导出方式进行自定义:
Step 2:Please make sure to check truncate table before insert in data creation options:
第 2 步:请确保在插入数据创建选项之前检查 truncate table:
Now you are able to import this database successfully.
现在您可以成功导入此数据库。
回答by CodeOwl
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 转储导入将抛出这种错误。
回答by Andrada
If you have a new database and you make a fresh clean import, the problem may come from inserting data that contains a '0' incrementation and this would transform to '1' with AUTO_INCREMENT
and cause this error.
如果您有一个新数据库并进行全新的干净导入,则问题可能来自插入包含“0”增量的数据,这将转换为“1”AUTO_INCREMENT
并导致此错误。
My solution was to use in the sql import file.
我的解决方案是在 sql 导入文件中使用。
SET SESSION sql_mode='NO_AUTO_VALUE_ON_ZERO';
回答by Nita
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
回答by bito_
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*
;
如果不存在,则创建数据库*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!
而且我确定您已经有一个同名的数据库 - 在同一个服务器中 - 请检查。只需更改名称或删除此行!
回答by user4563764
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 列声明为未签名,并且与 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 的值更改为新值,我的问题就解决了。
回答by sastorsl
Also check your triggers.
还要检查你的触发器。
Encountered this with a history table trigger which tried to insert the main table id
into the history table id
instead of the correct hist-table
.source_id
column.
遇到此问题的历史表触发器试图将主id
表id
而不是正确的hist-table
. source_id
柱子。
The update statement did not touch the id
column at all so took some time to find:
更新语句根本没有触及该id
列,所以花了一些时间才找到:
UPDATE source_table SET status = 0;
The trigger tried to do something similar to this:
触发器试图做类似的事情:
FOR EACH ROW
BEGIN
INSERT INTO `history_table` (`action`,`id`,`status`,`time_created`)
VALUES('update', NEW.id, NEW.status, NEW.time_created);
END;
Was corrected to something like this:
被纠正为这样的事情:
FOR EACH ROW
BEGIN
INSERT INTO `history_table` (`action`,`source_id`,`status`,`time_created`)
VALUES('update', NEW.id, NEW.status, NEW.time_created);
END;