MySQL 如何从MySQL的特定点开始自动递增?

MySQL 如何从MySQL的特定点开始自动递增?

要从特定点开始自动递增,可以使用ALTER命令。语法如下−

ALTER TABLE yourTableName auto_increment = anySpecificPoint;
Mysql

为了理解上述概念,让我们创建一个表。创建表的查询语句如下−

mysql> create table AutoIncrementSpecificPoint
   −> (
   −> BookId int auto_increment not null,
   −> Primary key(BookId)
   −> );
Query OK, 0 rows affected (0.56 sec)
Mysql

现在可以使用插入命令插入记录。

插入的查询语句如下−

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)
Mysql

上述插入命令从1开始,并向下一个值添加1。现在可以使用select语句检查表中的所有记录。

查询语句如下−

mysql> select *from AutoIncrementSpecificPoint;
Mysql

以下是输出结果−

+--------+
| BookId |
+--------+
|      1 |
|      2 |
|      3 |
|      4 |
+--------+
4 rows in set (0.00 sec)
Mysql

查看上面的输出示例,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
Mysql

在上述查询中,我已将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)
Mysql

使用select语句从表中显示所有记录。查询语句如下−

mysql> select *from AutoIncrementSpecificPoint;
Mysql

以下是显示从100开始设置的auto increment的其他值的输出−

+--------+
| BookId |
+--------+
|      1 |
|      2 |
|      3 |
|      4 |
|    100 |
|    101 |
|    102 |
+--------+
7 rows in set (0.00 sec)
Mysql

阅读更多:MySQL 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册