如何在 MySQL 中创建一个以 1000 开头、并具有自增长的 INT 字段(不是主键)?


为此,你需要将 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)

更新于:2019-08-23

145 次浏览

启动您的职业

通过完成课程获得认证

开始
广告
© . All rights reserved.