What is the benefit of zerofill in MySQL?

MySQLMySQLi Database

ZEROFILL pads the displayed value of the field with zeros up to the display width set in the column definition. Let us understand the role of zero fill in MySQL using an example. Creating a table with two columns, one has zerofill and the second one does not. The query to create a table.

mysql> create table ZeroFillDemo
   -> (
   -> First int(18) zerofill,
   -> Second int(18)
   -> );
Query OK, 0 rows affected (0.63 sec)

We can insert records in the table with the help of INSERT command. The query is as follows.

mysql> insert into ZeroFillDemo values(1,1);
Query OK, 1 row affected (0.13 sec)

mysql>  insert into ZeroFillDemo values(12,12);
Query OK, 1 row affected (0.15 sec)

mysql>  insert into ZeroFillDemo values(123,123);
Query OK, 1 row affected (0.10 sec)

mysql>  insert into ZeroFillDemo values(123456789,123456789);
Query OK, 1 row affected (0.20 sec)

Now, we can check the benefits of zerofill column with the help of select command.

The query is as follows.

mysql> select *from ZeroFillDemo;

The following is the output.

+--------------------+-----------+
| First              | Second    |
+--------------------+-----------+
| 000000000000000001 |         1 |
| 000000000000000012 |        12 |
| 000000000000000123 |       123 |
| 000000000123456789 | 123456789 |
+--------------------+-----------+
4 rows in set (0.00 sec)

Look at the sample output,zero is filled at the beginning.

raja
Published on 30-Nov-2018 15:59:36
Advertisements