博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY'
阅读量:5273 次
发布时间:2019-06-14

本文共 3978 字,大约阅读时间需要 13 分钟。

OS: centos 6.3

DB: 5.5.14
测试创建yoon测试表,没有主键,没有索引,基础数据内容如下:

mysql> select * from yoon;

+----+----------+------+
| id | name | user |
+----+----------+------+
| 1 | \""##!aa | NULL |
| 2 | z2 | NULL |
| 3 | z3 | NULL |
| 4 | z4 | NULL |
| 5 | z5 | NULL |
+----+----------+------+
5 rows in set (0.00 sec)

测试通过一条命令将id设为自增主键,命令alter table yoon add constraint auto_increment primary key yoon(id);创建成功,但是插入2条数据发现报错,场景如下:
mysql> desc yoon;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | 0       |       |
| name  | varchar(20) | YES  |     | NULL    |       |
| user  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

mysql> select * from yoon;
+----+----------+------+
| id | name     | user |
+----+----------+------+
|  1 | \""##!aa | NULL |
|  2 | z2       | NULL |
|  3 | z3       | NULL |
|  4 | z4       | NULL |
|  5 | z5       | NULL |
+----+----------+------+
5 rows in set (0.00 sec)

mysql> show index from yoon;
Empty set (0.00 sec)

mysql> alter table yoon add constraint auto_increment primary key yoon(id);
Query OK, 0 rows affected (0.29 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from yoon;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| yoon  |          0 | PRIMARY  |            1 | id          | A         |           6 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.01 sec)

mysql> insert into yoon(name,user) values ('z','HHH'); 
Query OK, 1 row affected (0.02 sec)

mysql> select * from yoon;
+----+----------+------+
| id | name     | user |
+----+----------+------+
|  0 | z        | HHH  |
|  1 | \""##!aa | NULL |
|  2 | z2       | NULL |
|  3 | z3       | NULL |
|  4 | z4       | NULL |
|  5 | z5       | NULL |
+----+----------+------+
6 rows in set (0.01 sec)

mysql> insert into yoon(name,user) values ('z6','HHH'); 
ERROR 1062 (23000): Duplicate entry '0' for key 'PRIMARY'

mysql> delete from yoon where id=0;
Query OK, 1 row affected (0.01 sec)

mysql> show index from yoon;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| yoon  |          0 | PRIMARY  |            1 | id          | A         |           6 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

mysql> select * from yoon;

+----+----------+------+
| id | name     | user |
+----+----------+------+
|  1 | \""##!aa | NULL |
|  2 | z2       | NULL |
|  3 | z3       | NULL |
|  4 | z4       | NULL |
|  5 | z5       | NULL |
+----+----------+------+
5 rows in set (0.00 sec)

mysql> alter table yoon modify column id int auto_increment;
Query OK, 5 rows affected (0.03 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from yoon;
+----+----------+------+
| id | name     | user |
+----+----------+------+
|  1 | \""##!aa | NULL |
|  2 | z2       | NULL |
|  3 | z3       | NULL |
|  4 | z4       | NULL |
|  5 | z5       | NULL |
|  6 | z6       | HHH  |
+----+----------+------+
6 rows in set (0.00 sec)
总结:主要原因alter语法使用不正确,有时候不报错,并不代表命令正确。具体语法如下:

转载于:https://www.cnblogs.com/hankyoon/p/5169618.html

你可能感兴趣的文章
【HTML】网页中如何让DIV在网页滚动到特定位置时出现
查看>>
文件序列化
查看>>
C++11 中的线程、锁和条件变量
查看>>
HDU 2485 Destroying the bus stations(!最大流∩!费用流∩搜索)
查看>>
Oracle关于用户信息的一些SQL语句
查看>>
2019-02-28处理公司同事无法上网事件记录
查看>>
HTCVive使用
查看>>
Javascript 浏览器检测
查看>>
Java程序员常用工具类库
查看>>
头文件有h和没有h的区别
查看>>
数据库的查询与视图
查看>>
洪涝有源淹没算法及淹没结果分析
查看>>
Flex在使用无线电的button切换直方图横坐标和叙述性说明
查看>>
C++ AMP 介绍(两)
查看>>
C++垃圾回收器的实现
查看>>
(二)数据加密技术
查看>>
Iptables和Firewall-selinux
查看>>
C#设置程序自启动
查看>>
Hadoop基准测试(一)
查看>>
Linux下解压缩文件命令总结
查看>>