首先是MySQL官方文档的一段话:
MySQL supports an extension for optionally specifying the display width of integer data types in parentheses following the base keyword for the type. For example, INT(4) specifies an INT with a display width of four digits. This optional display width may be used by applications to display integer values having a width less than the width specified for the column by left-padding them with spaces.
然后是测试:
`type` tinyint(2) NOT NULL,
mysql> select length(type) from wp_orders;
+--------------+
| length(type) |
+--------------+
| 1 |
+--------------+
1 row in set (0.00 sec)
如果真像引言中说的,这里length(type)应该至少是2才对。
最后发现,原来要使用这个optional display width,必须设定zerofill :
drop table if exists foo;
create table foo
(
area_code smallint(4) unsigned zerofill not null default 0
)
engine=innodb;
insert into foo (area_code) values (781),(727);
select * from foo;
+-----------+
| area_code |
+-----------+
| 0781 |
| 0727 |
+-----------+
