标签:
If you create a table using a numeric type like INT or BIGINT, you might have been surprised by the numbers that appear in the table‘s DESCRIBE:
mysql> CREATE TABLE test(a INT, b SMALLINT, c BIGINT); Query OK, 0 rows affected (1.04 sec) mysql> DESCRIBE test; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | a | int(11) | YES | | NULL | | | b | smallint(6) | YES | | NULL | | | c | bigint(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.03 sec)
Well, for an integer type (the value in parentheses called the display width of the field. This is different from (and somewhat less intuitive than) the parenthesised value in character fields—such as VARCHAR(10)—where it‘s the maximum number of characters you can store in the field, and for floating types where it describes the total number of digits you can store. The display width for integers... well it doesn‘t seem to do much really, on the surface.
Here‘s an example, using BIGINT because they‘re, well, biggest:
mysql> CREATE TABLE d1(c1 BIGINT(5), c2 BIGINT, c3 BIGINT(30)); Query OK, 0 rows affected (0.78 sec) mysql> DESC d1; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | c1 | bigint(5) | YES | | NULL | | | c2 | bigint(20) | YES | | NULL | | | c3 | bigint(30) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
mysql> INSERT INTO d1 VALUES (1, 1, 1); Query OK, 1 row affected (0.09 sec) mysql> INSERT INTO d1 VALUES (123456, 123456, 123456); Query OK, 1 row affected (0.07 sec) mysql> SELECT * FROM d1; +--------+--------+--------+ | c1 | c2 | c3 | +--------+--------+--------+ | 1 | 1 | 1 | | 123456 | 123456 | 123456 | +--------+--------+--------+ 2 rows in set (0.00 sec)
The numeric type overview calls this mysterious value "the maximum display width for integer types," and continues: "Display width is unrelated to the range of values a type can contain."
To see an effect, you can pad the columns with zeroes:
mysql> CREATE TABLE d2(c1 BIGINT(5) ZEROFILL, c2 BIGINT ZEROFILL, -> c3 BIGINT(30) ZEROFILL); Query OK, 0 rows affected (0.67 sec)
Note that ZEROFILL implicitly makes the column unsigned so it cannot store negative numbers. You couldn‘t, for example, see a number like -000123 in such a column.
The ZEROFILL option fills up the return value with zeros, as you might have guessed. It turns numbers like 123 into numbers like 000123, assuming your display width is 6. Let‘s see what it means in our table:
mysql> INSERT INTO d2 VALUES (1, 1, 1); Query OK, 1 row affected (0.06 sec) mysql> INSERT INTO d2 VALUES (123456, 123456, 123456); Query OK, 1 row affected (0.08 sec) mysql> SELECT * FROM d2; +--------+----------------------+--------------------------------+ | c1 | c2 | c3 | +--------+----------------------+--------------------------------+ | 00001 | 00000000000000000001 | 000000000000000000000000000001 | | 123456 | 00000000000000123456 | 000000000000000000000000123456 | +--------+----------------------+--------------------------------+ 2 rows in set (0.00 sec)
So it should be clear that the display width does not limit the amount of values you store in the column—you still get 123456 in a column with a display width of 5—but simply put, it affects how the values appear when they‘re padded.
Ah, but it does. Well, it does if you want it to. The mysql command-line client doesn‘t use the display width unless the field is full of zeroes, but other client applications can (and do). The display width is available to applications through the API, so they can use it to pad (with spaces, dashes, or whatever you like) the values.
For example, in PHP you could do something like this:
$result = $mysqli->query(‘SELECT c3 FROM d1‘); ... $c3metadata = $result->fetch_field_direct(0); // fetch metadata from first field (c3) $c3length = $c3metadata->length; // get the length from it (30) ... $row = $result->fetch_assoc(); printf("%${c3length}d\n", $row[‘c3‘]); // prints c3 in a column 30 chars wide
This code reads the display width of the c3 column (which we know is 30, from the code above) from the column‘s metadata into a variable $c3length, and uses that value to provided a width to the format specifier %d (for decimal integer), so you get the expression %${c3length}d, which evaluates as %30d. This prints the value of $row[‘c3‘]as an integer in a field 30 characters wide, right justified and space-padded.
The same sort of code exists in other languages; in Java, the java.sql.ResultSetMetaData interface provides the getColumnDisplaySize method, and Java‘s String.format method works similarly to the printf code above.
What does the 11 mean in INT(11)?
标签:
原文地址:http://my.oschina.net/u/658658/blog/485624