在 MySQL 中生成整数序列?
mysqlmysqli database
要在 MySQL 中创建整数序列,请使用 AUTO_INCREMENT。请记住,没有特殊命令可以创建整数序列。AUTO_INCREMENT 将有助于在 MySQL 中创建整数序列。
AUTO_INCREMENT 默认从 1 开始。您可以借助 alter 命令更改为其他数字。让我们看一个例子。假设您的初始值为 1000。这样,下一个数字将是 1000 + 1 = 1001。
这是 AUTO_INCREMENT 的一个示例。创建表的查询 −
mysql> create table AutoIncrementDemo −> ( −> EmployeeId int AUTO_INCREMENT, −> primary key(EmployeeId) −> ); Query OK, 0 rows affected (0.54 sec)
插入生成整数序列的记录。查询如下 −
mysql> insert into AutoIncrementDemo values(); Query OK, 1 row affected (0.13 sec) mysql> insert into AutoIncrementDemo values(); Query OK, 1 row affected (0.27 sec) mysql> insert into AutoIncrementDemo values(); Query OK, 1 row affected (0.09 sec) mysql> insert into AutoIncrementDemo values(); Query OK, 1 row affected (0.09 sec) mysql> insert into AutoIncrementDemo values(); Query OK, 1 row affected (0.09 sec) mysql> insert into AutoIncrementDemo values(); Query OK, 1 row affected (0.07 sec)
您可以借助 select 语句检查整数序列。查询如下 −
mysql> select *from AutoIncrementDemo;
以下是输出 −
+------------+ | EmployeeId | +------------+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | +------------+ 6 rows in set (0.00 sec)