开发者

MySQL Datatype for storing integers starting from Zero?

I have to store isdCodes which is in format 0091 001 009751 009665 etc. in the database, the initi开发者_JAVA技巧al will basically start from zero. i tried using int as datatype with unsigned attribute but it does not seems to work. which datatype is suitable for storing this type of value?


Does not work since IDD prefix is in most cases 00 but not always. It starts in most cases with 0 but not always.

Since the number of leading zeros matter, (i.e., 0091091), I would go for a varchar in this case.

If you really want to use some numeric type, I guess you could prepend a 1 in front of the number to keep the zeros, but it would be a bit of a hack.


I would use some varchar for that since ISD codes are not something you want to "calculate with" and are a sequence of digits rather than a whole number itself. The leading 0 and 00 thing shows that very good.

Edit Just saw that IDD prefixes like 8~10 exist. So there is no way to use a number-like datatype. You have to use something like varchar or similar if you want be able to store any ISD code.


You can use ZEROFILL, i.e.:

CREATE TABLE test (value int zerofill);

INSERT INTO test values(1), (20), (515);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜