MySQL #1062 - 键“PRIMARY”的重复条目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11638505/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:15:44  来源:igfitidea点击:

#1062 - Duplicate entry for key 'PRIMARY'

mysqlsqlmysql-error-1062

提问by irosenb

So my MySQL database is behaving a little bit wierd. This is my table:

所以我的 MySQL 数据库表现得有点奇怪。这是我的表:

Name shares id  price   indvprc
cat   2     4   81      0
goog  4     4   20      20
fb    4     9   20      20

I'm getting this #1062 error when I try to insert into the table. So I looked into it further and realized that when I try to insert values into the table, in which the name and shares values are the same, it will return the #1062 error. For example, If i inserted:

当我尝试插入到表中时,我收到了这个 #1062 错误。所以我进一步研究它并意识到当我尝试将值插入表中时,其中名称和共享值相同,它将返回 #1062 错误。例如,如果我插入:

fb    4      6     20   20 

It would return an error. But if i changed the shares number to 6, it would run fine. Is it because of one of my columns that could be unique, or is it just something with mysql?

它会返回一个错误。但是,如果我将股票编号更改为 6,它会运行良好。是因为我的一个列可能是唯一的,还是只是与 mysql 相关?

采纳答案by mlishn

You need to remove sharesas your PRIMARY KEYOR UNIQUE_KEY

您需要删除shares作为您的PRIMARY KEYORUNIQUE_KEY

回答by ?oàn Nh?t Du?n

  1. Make sure PRIMARY KEYwas selected AUTO_INCREMENT.
  2. Just enable Auto increment by :
    ALTER TABLE [table name] AUTO_INCREMENT = 1
  3. When you execute the insert command you have to skip this key.
  1. 确保PRIMARY KEY被选中AUTO_INCREMENT
  2. 只需通过以下方式启用自动增量:
    ALTER TABLE [table name] AUTO_INCREMENT = 1
  3. 当您执行插入命令时,您必须跳过该键。

回答by Majid Fouladpour

Use SHOW CREATE TABLE your-table-nameto see what column isyour primary key.

SHOW CREATE TABLE your-table-name看什么列主键。

回答by Hegle Leonardo Melgar

I solved it by changing the "lock" property from "shared" to "exclusive":

我通过将“lock”属性从“shared”更改为“exclusive”来解决它:

ALTER TABLE `table` 
CHANGE COLUMN `ID` `ID` INT(11) NOT NULL AUTO_INCREMENT COMMENT '' , LOCK = EXCLUSIVE;

回答by Chris

What is the exact error message? #1062 means duplicate entry violating a primary key constraint for a column -- which boils down to the point that you cannot have two of the same values in the column. The error message should tell you which of your columns is constrained, I'm guessing "shares".

确切的错误消息是什么?#1062 表示重复条目违反了列的主键约束——归结为列中不能有两个相同的值。错误消息应该告诉您哪些列受到限制,我猜是“共享”。

回答by Kshitiz

Probably this is not the best of solution but doing the following will solve the problem

可能这不是最好的解决方案,但执行以下操作将解决问题

Step 1: Take a database dump using the following command

步骤 1:使用以下命令进行数据库转储

mysqldump -u root -p databaseName > databaseName.db

find the line

找到线

ENGINE=InnoDB AUTO_INCREMENT="*****" DEFAULT CHARSET=utf8;

Step 2: Change *******to max id of your mysql table id. Save this value.

第 2 步:更改*******为您的 mysql 表 id 的最大 id。保存此值。

Step 3: again use

第三步:再次使用

mysql -u root -p databaseName < databaseName.db

In my case i got this error when i added a manual entry to use to enter data into some other table. some how we have to set the value AUTO_INCREMENTto max id using mysql command

在我的情况下,当我添加一个手动条目以用于将数据输入到其他表中时,我遇到了这个错误。我们必须如何AUTO_INCREMENT使用 mysql 命令将值设置为 max id

回答by Guy

Repair the database by your domain provider cpanel.

通过域提供商 cpanel 修复数据库。

Or see if you didnt merged something in the phpMyAdmin

或者看看你是否没有在 phpMyAdmin 中合并一些东西

回答by bsautner

I had this error from mySQL using DataNucleus and a database created with UTF8 - the error was being caused by this annotation for a primary key:

我使用 DataNucleus 和使用 UTF8 创建的数据库从 mySQL 中遇到此错误 - 错误是由主键的此注释引起的:

@PrimaryKey
@Unique
@Persistent(valueStrategy = IdGeneratorStrategy.UUIDSTRING)
protected String id;

dropping the table and regenerating it with this fixed it.

删除表并使用此修复它重新生成它。

@PrimaryKey
@Unique
@Persistent(valueStrategy = IdGeneratorStrategy.UUIDHEX)
protected String id;