JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Zerofill usage, advantages and alternatives in MySQL

Author:JIYIK Last Updated:2025/04/22 Views:

In this article we will understand the uses, advantages and alternatives of ZEROFILL attribute in MySQL.


Use and benefits of the ZEROFILL attribute in MySQL

The benefit of using the ZEROFILL attribute is that it has nothing to do with input and data storage, but only affects how output values ​​are displayed. MySQL automatically adds UNSIGNED when you use ZEROFILL while column definition.

Whenever we create a table by defining a column (attribute) using the ZEROFILL attribute, it pads the output (result value) of the column with zeros on the left, up to the display width specified in the column definition.

Sample code:

CREATE TABLE table_one (
    column_one INT(8) ZEROFILL NOT NULL,
    column_two INT(8) NOT NULL
);

INSERT INTO table_one (column_one, column_two) VALUES
(9, 9),
(98, 98),
(987, 987),
(987654321, 987654321);

SELECT column_one, column_two FROM table_one;

Output:

+------------+------------+
| column_one | column_two |
+------------+------------+
|   00000009 |          9 |
|   00000098 |         98 |
|   00000987 |        987 |
|  987654321 |  987654321 |
+------------+------------+
4 rows in set (0.00 sec)

See the example above, where the column_one attribute has a width of 8. Values ​​longer than the specified display width are not truncated.

Look at the last record in the table output above, 987654321.

Suppose we store values ​​larger than the display width in an integer field with the ZEROFILL attribute. We may encounter problems when MySQL generates temporary tables for complex joins.

The MySQL server assumes that the data value fits within the field (column) display width. Columns involved in a UNION query or expression ignore the ZEROFILL attribute.

As of MySQL 8.0.17, the ZEROFILL attribute of numeric data types is deprecated. So, we should expect support for the ZEROFILL attribute as the display width of integer data types will be removed in a future version of MySQL.


Alternative functionality for the ZEROFILL attribute in MySQL

Therefore, we can use alternatives that produce the same effect on these properties. For example, we can use LPAD() to add zeros on the left until the display width or to store formatted numbers in a CHAR field.

See the following code snippet.

SELECT LPAD(column_one,8,0) FROM table_one;

Output:

+----------------------+
| LPAD(column_one,8,0) |
+----------------------+
| 00000009             |
| 00000098             |
| 00000987             |
| 98765432             |
+----------------------+
4 rows in set (0.001 sec)

Not only can we use LPAD()to fill zeros on the left side of INT type columns, we can also use it for VARCHAR()type values ​​as shown below.

SELECT LPAD('Hello',8,'??') FROM table_one;

Output:

+----------------------+
| LPAD('Hello',8,'??') |
+----------------------+
| ???Hello             |
| ???Hello             |
| ???Hello             |
| ???Hello             |
+----------------------+
4 rows in set (0.00 sec)

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Changing max_allowed_packet Size in MySQL Server

Publish Date:2025/04/22 Views:192 Category:MySQL

This article explains how to change the max_allowed_packet size in MySQL server. To understand this, we will use two operating systems, Windows 10 and Linux (Ubuntu). Changing max_allowed_packet Size in MySQL Server If we try to upload a fi

Installing MySQL Client in Linux

Publish Date:2025/04/22 Views:186 Category:MySQL

MySQL is an abbreviation for Structured Query Language which is a relational database management system. It is an open source database which is freely available and used for both large and small applications. The use cases are in school app

Subtraction in MySQL

Publish Date:2025/04/21 Views:140 Category:MySQL

MINUS Operator is used in SQL to find unique elements in table A that are not present in table B. Through this tutorial, we will see how to emulate the operator in MySQL MINUS to get the desired result. We will understand it by using NOT IN

INTERSECT Operator in MySQL

Publish Date:2025/04/21 Views:99 Category:MySQL

This article will help you understand INTERSECT the operator. Although MySQL does not support INTERSECT and MINUS / EXCEPT , there are other ways to simulate this functionality. We will see INTERSECT what is , its benefits, and learn variou

Deleting using Join in MySQL

Publish Date:2025/04/21 Views:158 Category:MySQL

This tutorial article will show you how to use MySQL JOIN methods to delete data from multiple tables. This is useful when you are simultaneously deleting records from one table that are related to a specific record in another table. DELETE

MySQL using LIKE in case insensitive search

Publish Date:2025/04/21 Views:131 Category:MySQL

This tutorial article will show you how to use LIKE the operator to search a column. We will show you how to use it correctly to include all results that meet certain criteria, regardless of whether the value is uppercase or lowercase. LIKE

Loop PHP MySQLi Get Array Function

Publish Date:2025/04/21 Views:105 Category:MySQL

MySQLi fetch function is used to access data from the database server. After fetching the data, you can also iterate over it MySQLi with queries. In this article, we will see mysqli_fetch_array() the use of functions and methods to iterate

Generate a random and unique string in MySQL

Publish Date:2025/04/21 Views:78 Category:MySQL

Today, we will learn to use various functions in MySQL to generate random and unique strings. These functions include MD5() , RAND() , , SUBSTR() and UUID() . There is no inbuilt method to generate random string in MySQL, but there are many

Changing MySQL root password on Mac

Publish Date:2025/04/21 Views:161 Category:MySQL

This article teaches you how to change the MySQL user password on OS X. root We will be using XAMPP so that you can change the password using the MySQL console. Installing XAMPP for OSX First, download and install XAMPP for OSX from Apache

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial