MySQL:奇怪的AUTO_INCREMENT

时间:2014-11-24 12:05:55

标签: mysql

表格如下:

mysql> DESC text;
+-----------------+--------------+------+-----+-------------------+----------------+
| Field           | Type         | Null | Key | Default           | Extra          |
+-----------------+--------------+------+-----+-------------------+----------------+
| id              | int(11)      | NO   | PRI | NULL              | auto_increment |
| text            | varchar(255) | YES  |     | NULL              |                |
+-----------------+--------------+------+-----+-------------------+----------------+
2 rows in set (0.00 sec)

AUTO_INCREMENT 为1:

mysql> ALTER TABLE text AUTO_INCREMENT = 1;
Query OK, 1 row affected (0.36 sec)
Records: 1  Duplicates: 0  Warnings: 0

但是我很奇怪,比如:

mysql> SELECT id FROM text;
+------------+
| id         |
+------------+
| 2147483647 |
+------------+
1 row in set (0.00 sec)

有什么问题?

1 个答案:

答案 0 :(得分:1)

更改自动增量时,会将其设置为greatest(your_value,max(column)+ 1)

虽然我无法找到提及它的文档中的部分,但它在评论中

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

ALTER TABLE text AUTO_INCREMENT = 1;

然后检查

的结果
SELECT `AUTO_INCREMENT`
FROM  INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'DatabaseName'
AND   TABLE_NAME   = 'text';

确认其实际上并非1