Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

php - What is the difference (when being applied to my code) between INT(10) and INT(12)?

If I use INT(12) vs INT(10) or INT(8) what will this actually do in terms of me using in code?

(This is a spin off of a previous question) I read through the manuals and I think I understand what they're saying, but I don't actually know how it would apply to my php/mysql coding.

Can someone provide an example of where this would actually matter?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The argument to integer types in MySQL has no effect on the storage of data or the range of values supported by each data type.

The argument only applies to display width, which may be used by applications as Jonathan Fingland mentions. It also comes up when used in combination with the ZEROFILL option:

CREATE TABLE foo (
  i INT(3) ZEROFILL, 
  j INT(6) ZEROFILL, 
  k INT(11) ZEROFILL
);
INSERT INTO foo (i, j, k) VALUES (123, 456, 789);
SELECT * FROM foo;

+------+--------+-------------+
| i    | j      | k           |
+------+--------+-------------+
|  123 | 000456 | 00000000789 |
+------+--------+-------------+

See how ZEROFILL makes sure the data is zero-padded to at least the number of digits equal to the integer type argument.

Without ZEROFILL, the data is space-padded, but since spaces are often trimmed anyway, it's harder to see that difference.

What affect does it have on your PHP code? None. If you need to output columnar data, or space-pad or zero-pad values, it's more flexible to use sprintf(),


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...