MySQL 中的 Zerofill 有什么好处?
我只是想知道在 MySQL
中为 INT
数据类型定义 ZEROFILL
的好处/用途是什么?
`id` INT UNSIGNED ZEROFILL NOT NULL
I just want to know what is the benefit/usage of defining ZEROFILL
for INT
DataType in MySQL
?
`id` INT UNSIGNED ZEROFILL NOT NULL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
当您选择
ZEROFILL
类型的列时,它会用零填充字段的显示值,直至达到列定义中指定的显示宽度。长于显示宽度的值不会被截断。请注意,使用ZEROFILL
也意味着UNSIGNED
。使用 ZEROFILL 和显示宽度不会影响数据的存储方式。它仅影响其显示方式。
以下是一些示例 SQL,演示了
ZEROFILL
的用法:结果:
When you select a column with type
ZEROFILL
it pads the displayed value of the field with zeros up to the display width specified in the column definition. Values longer than the display width are not truncated. Note that usage ofZEROFILL
also impliesUNSIGNED
.Using
ZEROFILL
and a display width has no effect on how the data is stored. It affects only how it is displayed.Here is some example SQL that demonstrates the use of
ZEROFILL
:Result:
为了便于理解,
ZEROFILL
的使用可能会很有趣:在德国,我们有 5 位邮政编码。但是,这些代码可能以零开头,因此
80337
是 munic 的有效邮政编码,01067
是柏林的邮政编码。如您所见,任何德国公民都希望邮政编码显示为 5 位数代码,因此
1067
看起来很奇怪。为了存储这些数据,您可以使用
VARCHAR(5)
或INT(5) ZEROFILL
而零填充整数有两大优点:1067
,您仍然会返回01067
也许这个示例有助于理解
ZEROFILL
的使用。One example in order to understand, where the usage of
ZEROFILL
might be interesting:In Germany, we have 5 digit zipcodes. However, those Codes may start with a Zero, so
80337
is a valid zipcode for munic,01067
is a zipcode of Berlin.As you see, any German citizen expects the zipcodes to be displayed as a 5 digit code, so
1067
looks strange.In order to store those data, you could use a
VARCHAR(5)
orINT(5) ZEROFILL
whereas the zerofilled integer has two big advantages:1067
, you still get01067
backMaybe this example helps understanding the use of
ZEROFILL
.这是一个为那些喜欢方形盒子的不安人士提供的功能。
您插入,
但当您选择时,它会填充值
It's a feature for disturbed personalities who like square boxes.
You insert
but when you select, it pads the values
如果您需要连接这个“整数”与其他东西(另一个数字或文本),然后需要将其排序为“文本”,它有助于正确排序。
例如,
如果您需要使用整数字段编号(假设为 5)连接为 A-005 或 10/0005
It helps in correct sorting in the case that you will need to concatenate this "integer" with something else (another number or text) which will require to be sorted as a "text" then.
for example,
if you will need to use the integer field numbers (let's say 5) concatenated as A-005 or 10/0005
我知道我迟到了,但我发现填零对于 TINYINT(1) 的布尔表示很有帮助。 Null 并不总是意味着 False,有时您并不希望它如此。通过对tinyint进行零填充,您可以有效地将这些值转换为INT,并消除应用程序在交互时可能产生的任何混乱。然后,您的应用程序可以以类似于原始数据类型 True = Not(0) 的方式处理这些值
I know I'm late to the party but I find the zerofill is helpful for boolean representations of TINYINT(1). Null doesn't always mean False, sometimes you don't want it to. By zerofilling a tinyint, you're effectively converting those values to INT and removing any confusion ur application may have upon interaction. Your application can then treat those values in a manner similar to the primitive datatype True = Not(0)
ZEROFILL
这本质上意味着,如果将整数值 23 插入到宽度为 8 的 INT 列中,则剩余的可用位置将自动用零填充。
因此
变成:
ZEROFILL
This essentially means that if the integer value 23 is inserted into an INT column with the width of 8 then the rest of the available position will be automatically padded with zeros.
Hence
becomes:
http://dev.mysql.com/doc /refman/5.0/en/numeric-types.html
http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html
如果为数字列指定 ZEROFILL,MySQL 会自动向该列添加 UNSIGNED 属性。
允许 UNSIGNED 属性的数字数据类型也允许 SIGNED。但是,这些数据类型默认是有符号的,因此 SIGNED 属性不起作用。
以上描述摘自MYSQL官网。
If you specify ZEROFILL for a numeric column, MySQL automatically adds the UNSIGNED attribute to the column.
Numeric data types that permit the UNSIGNED attribute also permit SIGNED. However, these data types are signed by default, so the SIGNED attribute has no effect.
Above description is taken from MYSQL official website.