在创建表时为 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.