why a 10 digit phone number cant be stored in an integer of length 10?

Viewed 77

I am working on a college project having a form to store mobile numbers. Recently i found out a 10 digit phone number (without +/-) cant be stored in a data type of integer of length 10. Why is it so?

Unfortunately a phone number of 9 digit can be stored without any problem. Atlast i changed the data type to VARCHAR. But still am curious about the integer. After all there is only 10 integer digits.

2 Answers

the INT data type in mysql does not go by length of the integer, but rather the max and min value that can be stored with 4 bytes. Also, it should be noted that you can have either a SIGNED INT, or an UNSIGNED INT, which also effects the max and min value.

So for instance, a SIGNED INT can store values from -2147483648 to 2147483648, and an UNSIGNED INT can store values from 0 to 4294967295. Neither option could store a ten digit phone number.

You could however use a BIGINT, which uses 8 bytes of data to store its values, allowing a much higher min and max allowed. But you may be better served by VARCHAR, as a phone number is not really an integer. For instance, the phone number 0012345678, would just be stored as 12345678 in an INT field, and you would then have to run formatting conversions on the data before displaying.

To store mobile numbers you should use BIGINT data type in MYSQL, because 10 digits mobile number exceeds the range of INT data type as INT takes only 4 bytes ranging from 0 to 4,294,967,295 (Unsigned INT) which is not enough to store 10 digits mobile number. A BIGINT (8 bytes) can store it easily as it's range is from 0 to 18,446,744,073,709,551,615 (Unsigned BIGINT)

Related