What does the 11 mean in INT(11)?

What does the 11 mean in INT(11)?

By Jeremy Smyth-Oracle on Mar 26, 2014

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)

I didn‘t put those values
 
11,
 
6, and
 
20
 in there. Where did they come from and what are they?

They‘re the columns‘ "Display Width"

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.

An example

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) 

The
 
c1
 and
 
c3
 columns use explicit display widths. The 
c2 column uses the default display width, which is just enough to contain the largest amount of characters in a
 
BIGINT
 (including the sign). The largest negative
 
BIGINT
 is -9223372036854775808, which if you count carefully you‘ll see is 20 characters. For similar reason, the default display with of an
 
INT
 (with largest negative value -2147483648, go on, count ‘em) is 11, and so on.

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)

You can see from this that the display width has no effect on the output at all, at least when you use the
 
mysqlcommand-line client. We‘ll come back to this idea later.

So what‘s it for?

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.

What‘s the point if it doesn‘t work without ZEROFILL?

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.

In short...

  • You can ignore the display widths if you don‘t use them. The defaults are enough to display any value compatible with the type, and won‘t cause you trouble if your application uses the display width without your knowledge.
  • If you need to zero-pad your numbers, display width lets you say how much padding you need, but you‘ll need to make sure your application deals with number larger than that width either by ensuring it can handle them, or that your business logic prevents them.
  • If you want to display space-padded numbers in plaintext reports or other fixed-width output formats and you want to store the column‘s display width with the other column metadata at the database (and where else would you put it?), use the display width.
时间: 2024-10-06 00:28:12

What does the 11 mean in INT(11)?的相关文章

int(1)和int(11)的区别

在cmd中进入数据库中 creata table t(x int(1) zerofill,y int(11) zerofill); insert into t(x,y) values(1,1); select x,y from t; 然后我们再创建一张表 我们比较一下可以发现int(1)和int(11)使用zerofill后两者才会有所区别,当没有加zerofill时候两者是没有任何区别的. 因此当结合可选扩展属性zerofill使用时, 默认补充的空格用零代替.例如,对于声明为INT(5) z

mysql中int(11)

作为SQL标准的扩展,MySQL也支持整数类型TINYINT.MEDIUMINT和BIGINT. mysql int(M),M表示显示宽度和存储无关,int是4个字节. mysql数据库的有符号int能存2^31-1~-2^31-1,转成十进制是个有10位数的数字,int(11)表示可以最多显示10位的负数,如果显示的是正数的话最多10位,因为有符号的int只能存10位(字面显示长度)

mysql int(3)与int(11)的区别

总结,int(M) zerofill,加上zerofill后M才表现出有点点效果,比如 int(3) zerofill,你插入到数据库里的是10,则实际插入为010,也就是在前面补充加了一个0.如果int(3)和int(10)不加zerofill,则它们没有什么区别.M不是用来限制int个数的.int(M)的最大值和最小值与undesigned有关,最下面那副图有说明. mysql> create table t (t int(3) zerofill);Query OK, 0 rows affe

int(11)最大长度是多少,MySQL中varchar最大长度是多少(转)

int(11)最大长度是多少,MySQL中varchar最大长度是多少? int(11)最大长度是多少? 在SQL语句中int代表你要创建字段的类型,int代表整型,11代表字段的长度. 这个11代表显示宽度,整数列的显示宽度与mysql需要用多少个字符来显示该列数值,与该整数需要的存储空间的大小都没有关系,比如,不管设定了显示宽度是多少个字符,bigint都要占用8个字节. int是整型,(11)是指显示字符的长度,但要加参数的,最大为255,比如它是记录行数的id,插入10笔资料,它就显示0

Data type confusion: what is an int(11)?

http://everythingmysql.ning.com/profiles/blogs/data-type-confusion-what-is-an Over and over I see customers that don't understand what int(11) really means. Their confusion is understandable. Many know what defining a char(10) means (a fixed-sized ch

MySQL整数类型说明 int(11) vs int(20)

整数类型后面跟的是显示的宽度.M指示最大显示宽度.最大有效显示宽度是255.显示宽度与存储大小或类型包含的值的范围无关. 实践出真知: mysql> create table test2 ( a int , b int(20) unsigned not null , c int(40) unsigned zerofill ) engine = innodb ; Query OK, 0 rows affected (0.06 sec)   mysql> show create table tes

int(3)与int(11)的区别

注意:这里的M代表的并不是存储在数据库中的具体的长度,以前总是会误以为int(3)只能存储3个长度的数字,int(11)就会存储11个长度的数字,这是大错特错的.其实当我们在选择使用int的类型的时候,不论是int(3)还是int(11),它在数据库里面存储的都是4个字节的长度,在使用int(3)的时候如果你输入的是10,会默认给你存储位010,也就是说这个3代表的是默认的一个长度,当你不足3位时,会帮你不全,当你超过3位时,就没有任何的影响.前天组管问我 int(10)与int(11)有什么区

int(11)中的11是什么意思

1 bytes = 8 bit ,一个字节最多可以代表的数据长度是2的8次方 11111111 在计算机中也就是    -128到127 1.BIT[M] 位字段类型,M表示每个值的位数,范围从1到64,如果M被忽略,默认为1 2.TINYINT[(M)] [UNSIGNED] [ZEROFILL]  M默认为4 很小的整数.带符号的范围是-128到127.无符号的范围是0到255. 3. BOOL,BOOLEAN 是TINYINT(1)的同义词.zero值被视为假.非zero值视为真. 4.S

MYSQL 中的 int(11) 到底代表什么意思?

int(11) 的含义: 11 代表的并不是长度,而是字符的显示宽度: 在字段类型为int时,无论你显示宽度设置为多少,int类型能存储的最大值和最小值永远都是固定的 当int字段类型设置为无符号且填充零(UNSIGNED ZEROFILL)时,当数值位数未达到设置的显示宽度时,会在数值前面补充零直到满足设定的显示宽度,为什么会有无符号的限制呢,是因为ZEROFILL属性会隐式地将数值转为无符号型,因此不能存储负的数值. 注意:如果用navicate软件查询出来并不会显示左边的0,但把数据导出时