顯示從 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)

更新於: 31-12-2019

648 檢視

開啟你的 職業

完成本課程可獲得認證

開始
廣告
© . All rights reserved.