在建立表時為 AUTO_INCREMENT 設定自定義值並使用 ZEROFILL。現在當不插入任何內容並使用 INSERT 語句時會發生什麼?


我們來看一個例子,建立一個表,其中我們把 StudentId 列設定為 AUTO_INCREMENT = 100 並且還使用了 ZEROFILL -

mysql> create table DemoTable
(
   StudentId int(7) ZEROFILL NOT NULL AUTO_INCREMENT,
   PRIMARY KEY(StudentId)
)AUTO_INCREMENT=100;
Query OK, 0 rows affected (0.48 sec)

使用 insert 命令在表中插入一些記錄。現在,當沒有插入任何內容時,值將從 101(自動增量)開始,而由於我們在建立上述表格時設定了 ZEROFILL,因此左側的其餘值將填充為 0 -

mysql> insert into DemoTable values();
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values();
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values();
Query OK, 1 row affected (0.07 sec)
mysql> insert into DemoTable values();
Query OK, 1 row affected (0.09 sec)

使用 select 語句從表中顯示所有記錄 -

mysql> select *from DemoTable;

這將生成以下輸出 -

+-----------+
| StudentId |
+-----------+
| 0000100   |
| 0000101   |
| 0000102   |
| 0000103   |
+-----------+
4 rows in set (0.00 sec)

更新於:25-9-2019

86 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

開始學習
廣告
© . All rights reserved.