MySQL discards leading zero from integer column

I have a field bigintthat, for example, when entering a number, such as '05555555555', 0 is disabled and only inserted '5555555555'.

What type of data should I use to prevent this?

+3
source share
5 answers

You can not. Integer columns (bigint) do not store leading zeros (i.e., in a visual representation)

Instead of trying to keep the leading zero (using the varchar field), have the format of the representation (or any other) of the integer in a string in the format you need.

If you need to save something that is actually a string in a domain model (such as a phone number), use a string, not an integer.

+4
source

BIGINT Integer , (BIGINT 8 ). 5555555555 :

00000000 00000000 00000000 00000001 01001011 00100011 00001100 11100011

, VARCHAR CHAR . . .

+2

bigint , 05555555555 5555555555 - . , . varchar .

+1

The problem is that you have a large database with one hundred thousand rows, bigint, much faster than the VARHAR field. I got a similar problem with a product database full with European Article Numbers (EAN). Some of these codes begin with the lead 0 When I change it to VARCHAR, to load some pages that are looking for EAN codes, I change 8 seconds, it BIGINT turns into 2 seconds. Big difference in speed really.

-2
source

You can examine the field change for using UNSIGNED ZEROFILL. This should allow you to keep the number with leading zeros.

-3
source

Source: https://habr.com/ru/post/1788638/


All Articles