显示从 001 开始的 MySQL 中自动递增的用户 ID 序列号?
为此,请使用 ZEROFILL 并更改表格,从相同的序列开始 −
alter table yourTableName change yourColumnName yourColumnName int(3) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT PRIMARY KEY;
为了理解上述语法,我们首先创建一个表格 −
mysql> create table DemoTable1958 ( UserId int, UserName varchar(20) ); Query OK, 0 rows affected (0.00 sec)
以下是更改生成的序列号以从 001 开始的查询
mysql> alter table DemoTable1958 change UserId UserId int(3) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT PRIMARY KEY; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0
让我们查看表格说明
mysql> desc DemoTable1958;
这将产生以下输出 −
+----------+--------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------------------+------+-----+---------+----------------+ | UserId | int(3) unsigned zerofill | NO | PRI | NULL | auto_increment | | UserName | varchar(20) | YES | | NULL | | +----------+--------------------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec)
使用 insert 命令在表格中插入一些记录 −
mysql> insert into DemoTable1958(UserName) values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1958(UserName) values('David'); Query OK, 1 row affected (0.00 sec)
使用 select 语句显示表格中的所有记录 −
mysql> select * from DemoTable1958;
这将产生以下输出 −
+--------+----------+ | UserId | UserName | +--------+----------+ | 001 | Chris | | 002 | David | +--------+----------+ 2 rows in set (0.00 sec)
广告