MySQL定義主鍵自增長(AUTO_INCREMENT)出錯

來源:互聯網
上載者:User

今天給項目的一個表設定自增長主鍵

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;

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.