MySQL 如何从MySQL的特定点开始自动递增?
要从特定点开始自动递增,可以使用ALTER命令。语法如下−
ALTER TABLE yourTableName auto_increment = anySpecificPoint;
为了理解上述概念,让我们创建一个表。创建表的查询语句如下−
mysql> create table AutoIncrementSpecificPoint
−> (
−> BookId int auto_increment not null,
−> Primary key(BookId)
−> );
Query OK, 0 rows affected (0.56 sec)
现在可以使用插入命令插入记录。
插入的查询语句如下−
mysql> insert into AutoIncrementSpecificPoint values();
Query OK, 1 row affected (0.17 sec)
mysql> insert into AutoIncrementSpecificPoint values();
Query OK, 1 row affected (0.17 sec)
mysql> insert into AutoIncrementSpecificPoint values();
Query OK, 1 row affected (0.09 sec)
mysql> insert into AutoIncrementSpecificPoint values();
Query OK, 1 row affected (0.10 sec)
上述插入命令从1开始,并向下一个值添加1。现在可以使用select语句检查表中的所有记录。
查询语句如下−
mysql> select *from AutoIncrementSpecificPoint;
以下是输出结果−
+--------+
| BookId |
+--------+
| 1 |
| 2 |
| 3 |
| 4 |
+--------+
4 rows in set (0.00 sec)
查看上面的输出示例,auto_increment从1开始。
现在,要更改auto_increment以从特定点开始,可以使用ALTER命令。查询语句如下−
mysql> alter table AutoIncrementSpecificPoint auto_increment = 100;
Query OK, 0 rows affected (0.25 sec)
Records: 0 Duplicates: 0 Warnings: 0
在上述查询中,我已将auto increment设置为100。现在,请使用另一次插入命令再次将记录插入表中。查询语句如下−
mysql> insert into AutoIncrementSpecificPoint values();
Query OK, 1 row affected (0.25 sec)
mysql> insert into AutoIncrementSpecificPoint values();
Query OK, 1 row affected (0.18 sec)
mysql> insert into AutoIncrementSpecificPoint values();
Query OK, 1 row affected (0.14 sec)
使用select语句从表中显示所有记录。查询语句如下−
mysql> select *from AutoIncrementSpecificPoint;
以下是显示从100开始设置的auto increment的其他值的输出−
+--------+
| BookId |
+--------+
| 1 |
| 2 |
| 3 |
| 4 |
| 100 |
| 101 |
| 102 |
+--------+
7 rows in set (0.00 sec)
阅读更多:MySQL 教程
极客教程