使用 ALTER 命令为 MySQL 表设置自动增量初始值
mysqlmysqli database更新于 2024/12/18 18:52:17
要为 MySQL 表设置自动增量初始值,请使用 ALTER 命令。第一步是
alter table yourTableName modified yourColumnName int NOT NULL AUTO_INCREMENT PRIMARY KEY,add index(yourColumnName);
第二步如下
alter table yourTableName AUTO_INCREMENT=yourStartingValue;
为了理解上述语法,让我们创建一个表。创建表的查询如下
mysql> create table setAutoIncrementDemo -> ( -> UserId int, -> UserName varchar(20) -> ); Query OK, 0 rows affected (0.75 sec)
现在执行上述两个步骤来为 MySQL 表设置自动增量初始值。
步骤 1 -查询如下
mysql> alter table setAutoIncrementDemo modified UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,add index(UserId); Query OK, 0 rows impacted (1.51 sec) Records: 0 Duplicates: 0 Warnings: 0
步骤 2 -查询如下
mysql> alter table setAutoIncrementDemo AUTO_INCREMENT=1000; Query OK, 0 rows affected (0.34 sec) Records: 0 Duplicates: 0 Warnings: 0
使用 insert 命令在表中插入一些记录。
查询如下
mysql> INSERT INTO setAutoIncrementDemo(UserName) values('John');
Query OK, 1 row affected (0.14 sec)
mysql> INSERT INTO setAutoIncrementDemo(UserName) values('Carol');
Query OK, 1 row affected (0.12 sec)
mysql> INSERT INTO setAutoIncrementDemo(UserName) values('Sam');
Query OK, 1 row affected (0.13 sec)
使用 select 语句显示表中的所有记录。
查询如下
mysql> select *from setAutoIncrementDemo;
以下是输出 −
+--------+----------+ | UserId | UserName | +--------+----------+ | 1000 | John | | 1001 | Carol | | 1002 | Sam | +--------+----------+ 3 rows in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

