What is the usage of zerofill in a MySQL field?


Zerofill pads the displayed value of the field with zeros up to the display width specified in the column definition. For example, if column is set int(8), therefore the width is 8. If the number is let’s say 4376, then zero will be padded on the left for total width i.e. 8 −

00004376

Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Number int(8) zerofill
   -> );
Query OK, 0 rows affected (0.50 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(10);
Query OK, 1 row affected (0.16 sec)

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

mysql> insert into DemoTable values(1);
Query OK, 1 row affected (0.47 sec)

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

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----------+
| Number   |
+----------+
| 00000010 |
| 00001234 |
| 00000001 |
| 00678965 |
+----------+
4 rows in set (0.00 sec)
raja
Published on 12-Jul-2019 07:55:41
Advertisements