Heim  >  Artikel  >  Datenbank  >  MySQL定义主键自增长(AUTO_INCREMENT)出错

MySQL定义主键自增长(AUTO_INCREMENT)出错

WBOY
WBOYOriginal
2016-06-07 16:59:161040Durchsuche

今天给项目的一个表设置自增长主键mysqlgt; ALTER TABLE `users` CHANGE `id` `id` INT(11) NOT NULL AUTO_INCREMENT; 运行却报

今天给项目的一个表设置自增长主键

mysql> ALTER TABLE `users` CHANGE `id` `id` INT(11) NOT NULL AUTO_INCREMENT;

运行却报错 ERROR 1062 (23000): Duplicate entry '1' for key 1

表中原有数据如下
+----+------+
| id | name |
+----+------+
| 0 | AAA |
| 1 | BBB |
| 2 | CCC |
+----+------+-
同事tzngvi说可能是 id = 0 的那条数据有问题,把 id = 0 那条数据删除后,,再设置自增长,问题解决。
又去 MySQL 官网查了一下,AUTO_INCREMENT是默认从 1 开始的。

To start with an AUTO_INCREMENT value other than 1, you can set that value with CREATE TABLE or ALTER TABLE, like this:
mysql> ALTER TABLE tbl AUTO_INCREMENT = 100;

linux

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn