如何在 MySQL 中创建一个以 auto_increment 开头的 INT 字段(不是主键)?

mysqlmysqli database更新于 2025/10/18 15:08:17

为此,您需要将 AUTO_INCREMENT 设置为 1000 −

alter table yourTableName AUTO_INCREMENT = 1000;

首先我们创建一个表 −

mysql> create table DemoTable639 (
   StudentId int PRIMARY KEY,
   StudentStartId int AUTO_INCREMENT,
   StudentName VARCHAR(50),
   INDEX(StudentStartId)
);
Query OK, 0 rows affected (0.86 sec)

以下是设置自动增量为 1000 的查询 −

mysql> alter table DemoTable639 AUTO_INCREMENT = 1000;
Query OK, 0 rows affected (0.28 sec)
Records: 0 Duplicates: 0 Warnings: 0

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable639(StudentId,StudentName) values(1,'John');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable639(StudentId,StudentName) values(2,'Chris');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable639(StudentId,StudentName) values(3,'Robert');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable639(StudentId,StudentName) values(4,'David');
Query OK, 1 row affected (0.16 sec)

使用 select 语句显示表中的所有记录 −

mysql> select *from DemoTable639;

这将产生以下输出 −

+-----------+----------------+-------------+
| StudentId | StudentStartId | StudentName |
+-----------+----------------+-------------+
|         1 |           1000 | John        |
|         2 |           1001 | Chris       |
|         3 |           1002 | Robert      |
|         4 |           1003 | David       |
+-----------+----------------+-------------+
4 rows in set (0.00 sec)

相关文章


有用资源