MySQL 5.1.41 leading zero is deleted

2020-04-08 13:03发布

I have a MySQL database where I want to store phone numbers among other things.

The fieldtype is INT(10)

When I try to insert a number starting with a 0, like 0504042858 it's stored like 504042858. This only happens with phone numbers with leading zeros. When the number start with any other number, it's stored correctly.

What am I doing wrong?

标签: mysql
3条回答
我只想做你的唯一
2楼-- · 2020-04-08 13:50

You should probably store phone numbers as a varchar. Phone numbers are only numeric by accident.

You may also be interested in checking out the following Stack Overflow posts:

查看更多
时光不老,我们不散
3楼-- · 2020-04-08 13:50

You can give length INT(11) with attribute value UNSIGNED_ZEROFILL. it will fill all 11 digits and if any digit length is less than 11, it will add zero itself before the value. This might solve your problem.

查看更多
Summer. ? 凉城
4楼-- · 2020-04-08 13:55

it is removing the leading zero because mathematically they are the same and removing the leading zero is a quick storage optimization. In addition it also makes the numbers easier to read imagine a number padded with several leading zeros in a column of several hundred numbers.

I agree with Daniel change your column to a varchar.

查看更多
登录 后发表回答