1

可能重复:
如何强制 MySQL 将 0 作为有效的自动增量值

我知道我可以对现有行进行更新,但是是否可以插入 id 为 0 的行?

mysql> create table inc( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, sz TEXT);
Query OK, 0 rows affected (0.10 sec)

mysql> insert into inc(id, sz) select 25, "a";
Query OK, 1 row affected (0.02 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into inc(id, sz) select 25, "a";
ERROR 1062 (23000): Duplicate entry '25' for key 'PRIMARY'
mysql> insert into inc(id, sz) select 27, "b";
Query OK, 1 row affected (0.04 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into inc(id, sz) select -5, "c";
Query OK, 1 row affected (0.03 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into inc(id, sz) select 0, "d";
Query OK, 1 row affected (0.15 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from inc;
+----+------+
| id | sz   |
+----+------+
| -5 | c    |
| 25 | a    |
| 27 | b    |
| 28 | d    |
+----+------+
4 rows in set (0.00 sec)
4

1 回答 1

1

来自http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

“没有为 AUTO_INCREMENT 列指定值,因此 MySQL 自动分配了序列号。您也可以显式地将 NULL 或 0 分配给该列以生成序列号。”

所以似乎使用 0 只是告诉它 auto_increment。

但是,如果您要分配 ID 号,我不确定您为什么将列定义为使用 AUTO_INCREMENT?

于 2012-10-07T02:44:14.547 回答