【问题标题】:Mysql insert query returns ERROR 1062 (23000): Duplicate entry '2147483647' for key 'PRIMARY'Mysql 插入查询返回 ERROR 1062 (23000): Duplicate entry '2147483647' for key 'PRIMARY'
【发布时间】:2013-09-09 17:16:53
【问题描述】:

我注意到在我的数据库中插入查询时出现错误。

mysql> insert into users (name) values ('Gepp');

返回:

错误 1062 (23000):键 'PRIMARY' 的重复条目 '2147483647'

这是我第一次收到此错误,这可能表明已达到某种限制。无论如何,我检查了其他抱怨相同错误的帖子,发现触发器可能是问题所在。不幸的是,事实并非如此:

mysql> SHOW triggers;
Empty set (0.00 sec)

编辑 我的用户表的结构如下所示:

> *************************** 1. row ***************************
  Field: uid
   Type: int(11)
   Null: NO
    Key: PRI
Default: NULL
  Extra: auto_increment
*************************** 2. row ***************************
  Field: name
   Type: varchar(50)
   Null: NO
    Key: 
Default: NULL
  Extra: 
*************************** 3. row ***************************
  Field: email
   Type: varchar(100)
   Null: NO
    Key: UNI
Default: NULL
  Extra: 
*************************** 4. row ***************************
  Field: encrypted_password
   Type: varchar(80)
   Null: NO
    Key: 
Default: NULL
  Extra: 
*************************** 5. row ***************************
  Field: salt
   Type: varchar(10)
   Null: NO
    Key: 
Default: NULL
  Extra: 
*************************** 6. row ***************************
  Field: descrizione
   Type: varchar(600)
   Null: YES
    Key: 
Default: NULL
  Extra: 
*************************** 7. row ***************************
  Field: motto
   Type: varchar(100)
   Null: NO
    Key: 
Default: 
  Extra: 
*************************** 8. row ***************************
  Field: status
   Type: varchar(100)
   Null: NO
    Key: 
Default: Hey new gambler! Share your thoughts!
  Extra: 
*************************** 9. row ***************************
  Field: game
   Type: varchar(100)
   Null: NO
    Key: 
Default: 
  Extra: 
*************************** 10. row ***************************
  Field: pokeroom
   Type: varchar(100)
   Null: NO
    Key: 
Default: 
  Extra: 
*************************** 11. row ***************************
  Field: score
   Type: int(11)
   Null: NO
    Key: 
Default: 0
  Extra: 
*************************** 12. row ***************************
  Field: created_at
   Type: datetime
   Null: YES
    Key: 
Default: NULL
  Extra: 
*************************** 13. row ***************************
  Field: updated_at
   Type: datetime
   Null: YES
    Key: 
Default: NULL
  Extra: 
*************************** 14. row ***************************
  Field: photo
   Type: varchar(500)
   Null: NO
    Key: 
Default: 
  Extra: 
*************************** 15. row ***************************
  Field: panorama
   Type: varchar(500)
   Null: NO
    Key: 
Default: 
  Extra:

我该如何解决这个问题?

【问题讨论】:

  • 2147483647 是 MySQL 的最大 int 值。你真的有 2147483647 个用户吗?
  • users 表的主键是什么?您可能还需要在INSERT INTO 中设置它的值,除非它被创建为AUTO INCREMENT
  • 您可能忘记在主键字段上设置 auto_increment,其默认值恰好是该字段的 max_int。第一次插入将起作用,第二次插入将失败,因为 int 没有自动增量。
  • 'uid' 列是 PRIMARY KEY 并且还有 AUTO INCREMENT 请看我的编辑。
  • select uid from users order by uid desc limit 1 返回什么?

标签: mysql sql-insert


【解决方案1】:

谢谢大家的帮助!表配置错误。 uid 列具有主键和 auto_increment 属性,但在我正在处理的项目中,用户是使用如下查询创建的:

INSERT INTO users(uid, name, email, encrypted_password, salt, created_at) VALUES('12342354355.54534543','bollo','sai','dsfsd','sdsdf','23')

uid 是由 PHP 函数 uniqid("",true) 生成的,这导致了问题

    select uid,id from users;
+------------+----+
| uid        | id |
+------------+----+
|        183 |  1 |
|       5224 |  2 |
|       5228 |  3 |
|      52288 |  4 |
|     515620 |  5 |
|     519030 |  6 |
|    5156147 |  8 |
|    5156151 |  9 |
|    5156205 | 10 |
|    5157726 | 11 |
|   52289002 | 12 |
|  515615576 | 13 |
| 2147483647 | 14 |
+------------+----+
15 rows in set (0.00 sec)

如您所见,由上述查询创建的新 uid 始终大于前一个。可能 auto_increment 只接受大于最后插入的值的 uid 值。我已经幸运注册了14次,然后uid值超过了列定义允许的最大值,导致报错。

我已经通过从 uid 列中删除 Primary_Key 解决了这个问题:

alter table users drop primary key;

再次修改它以删除 auto_increment 属性:

alter table users modify uid varchar(40) not null unique;

最后添加了一个名为 id 的新列,用于跟踪和统计用户注册:

alter table users add id int(11) not null auto_increment primary key;

最后这个错误是由于数据库组织不当和作用于它的函数造成的。我的错!

【讨论】:

  • 我认为这个问题的根源是由于 uniqid("",true) 返回了一个像 "546e941cec14a4.31786554" 这样的 ID,然后被 mySQL 转换为整数。如您所见,这会很快增加最大 auto_increment 值。解决方案是在 UID 上使用唯一索引,并将 ID 设置为自增字段。
【解决方案2】:

在运行大型脚本以输入 1,000 行时,我得到了一个非常相似的索引重复 PRIMARY 值。

我删除了主索引键,然后运行了我的脚本,然后重新启用了我的“id”列作为主键,现在一切正常。

【讨论】:

  • 在某些情况下,删除 PRIMARY 键是不可能的!人们并不总是使用PK_ID 作为他们的主键;它也可以是多列的混合键。
【解决方案3】:

这是因为 AUTO_INCREMENT 正在增加 ID,但您的输入数据是相同的 ID。 从表定义中删除 Auto_Increment 或从输入文件中删除 ID。

【讨论】:

    【解决方案4】:

    最近我解决了类似的问题,错误代码:1062 重复条目。从这个页面how to solve mysql error code:1062 duplicate key?我发现,这个错误是因为主键字段数据类型达到了上限,也将数据类型从 int 更改为 bigint 可能会有所帮助,但更改数据类型取决于您的要求。该页面中有一个解决方法,我认为它会帮助您更好地理解这个问题,谢谢。

    【讨论】:

      猜你喜欢
      • 2019-10-11
      • 2021-10-07
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-22
      • 1970-01-01
      • 2011-10-29
      • 1970-01-01
      相关资源
      最近更新 更多