MySQL 中的 Zerofill 有什么好处?

发布于 2024-10-21 07:43:38 字数 173 浏览 1 评论 0原文

我只是想知道在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

孤独患者 2024-10-28 07:43:38

当您选择 ZEROFILL 类型的列时,它会用零填充字段的显示值,直至达到列定义中指定的显示宽度。长于显示宽度的值不会被截断。请注意,使用 ZEROFILL 也意味着 UNSIGNED

使用 ZEROFILL 和显示宽度不会影响数据的存储方式。它仅影响其显示方式。

以下是一些示例 SQL,演示了 ZEROFILL 的用法:

CREATE TABLE yourtable (x INT(8) ZEROFILL NOT NULL, y INT(8) NOT NULL);
INSERT INTO yourtable (x,y) VALUES
(1, 1),
(12, 12),
(123, 123),
(123456789, 123456789);
SELECT x, y FROM yourtable;

结果:

        x          y
 00000001          1
 00000012         12
 00000123        123
123456789  123456789

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 of ZEROFILL also implies UNSIGNED.

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:

CREATE TABLE yourtable (x INT(8) ZEROFILL NOT NULL, y INT(8) NOT NULL);
INSERT INTO yourtable (x,y) VALUES
(1, 1),
(12, 12),
(123, 123),
(123456789, 123456789);
SELECT x, y FROM yourtable;

Result:

        x          y
 00000001          1
 00000012         12
 00000123        123
123456789  123456789
再可℃爱ぅ一点好了 2024-10-28 07:43:38

为了便于理解,ZEROFILL 的使用可能会很有趣:

在德国,我们有 5 位邮政编码。但是,这些代码可能以零开头,因此 80337 是 munic 的有效邮政编码,01067 是柏林的邮政编码。

如您所见,任何德国公民都希望邮政编码显示为 5 位数代码,因此 1067 看起来很奇怪。

为了存储这些数据,您可以使用 VARCHAR(5)INT(5) ZEROFILL 而零填充整数有两大优点:

  1. 硬盘上的存储空间更少disk
  2. 如果插入 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) or INT(5) ZEROFILL whereas the zerofilled integer has two big advantages:

  1. Lot lesser storage space on hard disk
  2. If you insert 1067, you still get 01067 back

Maybe this example helps understanding the use of ZEROFILL.

我纯我任性 2024-10-28 07:43:38

这是一个为那些喜欢方形盒子的不安人士提供的功能。

您插入,

1
23
123 

但当您选择时,它会填充值

000001
000023
000123

It's a feature for disturbed personalities who like square boxes.

You insert

1
23
123 

but when you select, it pads the values

000001
000023
000123
白首有我共你 2024-10-28 07:43:38

如果您需要连接这个“整数”与其他东西(另一个数字或文本),然后需要将其排序为“文本”,它有助于正确排序。

例如,

如果您需要使用整数字段编号(假设为 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

蓝眸 2024-10-28 07:43:38

我知道我迟到了,但我发现填零对于 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)

肩上的翅膀 2024-10-28 07:43:38
mysql> CREATE TABLE tin3(id int PRIMARY KEY,val TINYINT(10) ZEROFILL);
Query OK, 0 rows affected (0.04 sec)

mysql> INSERT INTO tin3 VALUES(1,12),(2,7),(4,101);
Query OK, 3 rows affected (0.02 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM tin3;
+----+------------+
| id | val        |
+----+------------+
|  1 | 0000000012 |
|  2 | 0000000007 |
|  4 | 0000000101 |
+----+------------+
3 rows in set (0.00 sec)

mysql>

mysql> SELECT LENGTH(val) FROM tin3 WHERE id=2;
+-------------+
| LENGTH(val) |
+-------------+
|          10 |
+-------------+
1 row in set (0.01 sec)


mysql> SELECT val+1 FROM tin3 WHERE id=2;
+-------+
| val+1 |
+-------+
|     8 |
+-------+
1 row in set (0.00 sec)
mysql> CREATE TABLE tin3(id int PRIMARY KEY,val TINYINT(10) ZEROFILL);
Query OK, 0 rows affected (0.04 sec)

mysql> INSERT INTO tin3 VALUES(1,12),(2,7),(4,101);
Query OK, 3 rows affected (0.02 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM tin3;
+----+------------+
| id | val        |
+----+------------+
|  1 | 0000000012 |
|  2 | 0000000007 |
|  4 | 0000000101 |
+----+------------+
3 rows in set (0.00 sec)

mysql>

mysql> SELECT LENGTH(val) FROM tin3 WHERE id=2;
+-------------+
| LENGTH(val) |
+-------------+
|          10 |
+-------------+
1 row in set (0.01 sec)


mysql> SELECT val+1 FROM tin3 WHERE id=2;
+-------+
| val+1 |
+-------+
|     8 |
+-------+
1 row in set (0.00 sec)
拍不死你 2024-10-28 07:43:38

ZEROFILL

这本质上意味着,如果将整数值 23 插入到宽度为 8 的 INT 列中,则剩余的可用位置将自动用零填充。

因此

23

变成:

00000023

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

23

becomes:

00000023
冷了相思 2024-10-28 07:43:38

当与
可选(非标准)属性
ZEROFILL,默认填充
空格被零替换。为了
例如,对于声明为的列
INT(4) ZEROFILL,值为 5
检索为 0005。

http://dev.mysql.com/doc /refman/5.0/en/numeric-types.html

When used in conjunction with the
optional (nonstandard) attribute
ZEROFILL, the default padding of
spaces is replaced with zeros. For
example, for a column declared as
INT(4) ZEROFILL, a value of 5 is
retrieved as 0005.

http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html

不疑不惑不回忆 2024-10-28 07:43:38

如果为数字列指定 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文