I was always wondering what the size of numeric columns in MySQL was. Forgive me if this is obvious to someone else. But for me the MySQL manual lacks a great deal in this field.

TL;DR: It's about the display width. You only see it when you use ZEROFILL.

Usually you see something like int(11) in CREATE TABLE statements, but you can also change it to int(4).

So what does this size mean? Can you store higher values in a int(11) than in an int(4)?

Let's see what the MySQL manual says:

INT[(M)] [UNSIGNED] [ZEROFILL]
A normal-size integer. The signed range is -2147483648 to 2147483647. The unsigned range is 0 to 4294967295.

No word about the M. The entry about BOOL suggests that the size is not there for fun as it is a synonym for TINYINT(1) (with the specific size of 1).

TINYINT[(M)] [UNSIGNED] [ZEROFILL]
A very small integer. The signed range is -128 to 127. The unsigned range is 0 to 255.

BOOL, BOOLEAN
These types are synonyms for TINYINT(1). A value of zero is considered false. Non-zero values are considered true: […]

So TINYINT(1) must be different in some way from TINYINT(4) which is assumed by default when you leave the size out1. Still, you can store for example 100 into a TINYINT(1).

Finally, let's come to the place of the manual where there is the biggest hint to what the number means:

Several of the data type descriptions use these conventions:

M indicates the maximum display width for integer types. For
floating-point and fixed-point types, M is the total number of digits
that can be stored. For string types, M is the maximum length. The
maximum allowable value of M depends on the data type.

It's about the display width. The weird thing is, though2,
that, for example, if you have a value of 5 digits in a field with a
display width of 4 digits, the display width will not cut a digits off.

If the value has less digits than the display width, nothing happens
either. So it seems like the display doesn't have any effect in real
life.

Now2 ZEROFILL comes into play. It is a neat feature that pads values that are (here it comes) less than the specified display width with zeros, so that you will always receive a value of the specified length. This is for example useful for invoice ids.

So, concluding: The size is neither bits nor bytes. It's just the display width, that is used when the field has ZEROFILL specified.

If you see any more uses in the size value, please tell me. I am curious to know.

1 See this example:
mysql> create table a ( a tinyint );
Query OK, 0 rows affected (0.29 sec)
mysql> show columns from a;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| a | tinyint(4) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
1 row in set (0.26 sec)

mysql> alter table a change a a tinyint(1);
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> insert into a values (100);
Query OK, 1 row affected (0.00 sec)

mysql> select * from a;
+-----+
| a |
+-----+
| 100 |
+-----+
1 row in set (0.00 sec)

2 Some code to better explain what I described so clumsily.
mysql> create table b ( b int (4));
Query OK, 0 rows affected (0.25 sec)

mysql> insert into b values (10000);
Query OK, 1 row affected (0.00 sec)

mysql> select * from b;
+-------+
| b |
+-------+
| 10000 |
+-------+
1 row in set (0.00 sec)

mysql> alter table b change b b int(11);
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> select * from b;
+-------+
| b |
+-------+
| 10000 |
+-------+
1 row in set (0.00 sec)

mysql> alter table b change b b int(11) zerofill;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> select * from b;
+-------------+
| b |
+-------------+
| 00000010000 |
+-------------+
1 row in set (0.00 sec)

mysql> alter table b change b b int(4) zerofill;
Query OK, 1 row affected (0.08 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> select * from b;
+-------+
| b |
+-------+
| 10000 |
+-------+
1 row in set (0.00 sec)

mysql> alter table b change b b int(6) zerofill;
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0

mysql> select * from b;
+--------+
| b |
+--------+
| 010000 |
+--------+
1 row in set (0.00 sec)

mysql integer size 大小的更多相关文章

  1. mysql单表大小的限制

    mysql单表大小的限制一.MySQL数据库的MyISAM存储 引擎单表大小限制已经不是有MySQL数据库本身来决定(限制扩大到64pb),而是由所在主机的OS上面的文件系统来决定了.在mysql5. ...

  2. [转] MySQL 查询表数据大小的总结

    一:关于mysql表数据大小 我们知道mysql存储数据文件一般使用表空间存储 当mysql使用innodb存储引擎的时候,mysql使用表存储数据分为共享表空间和独享表空间两种方式 ·共享表空间:I ...

  3. mysql查看表大小

    mysql查看表大小 一:命令 show table status like 'table_name'\G; mysql> show table status like 'x'\G; . row ...

  4. 定时获取MySQL库的大小

    定时获取MySQL库的大小 获取数据库单个库的大小命令 [root@admin ~]# cat db_size.txt mysql -h 192.8.1.1 -uUSER -pPASSWORD -e' ...

  5. 两个Integer比较大小需要注意的误区

    通过下面的例子,来了解integer比较大小需注意的几点. eg.定义Integer对象a和b,比较两者结果为:a不等于b Integer a = 1; Integer b = 1; if(a==b) ...

  6. block size大小

    1.用tune2fs查看block size大小: 1 2 tune2fs -l /dev/sda1 |grep "Block size" Block size: 1024 2.用 ...

  7. Integer对象大小比较问题

    一.问题 先来看一看例子 public class IntegerTest { public static void main(String[] args) throws Exception { In ...

  8. mysql 设置max_allowed_packet 大小的办法

        show VARIABLES like '%max_allowed_packet%'; 第一句是查询  max_allowed_packet  的大小,第二句是重新设定  max_allowe ...

  9. mysql Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT

    使用mysql的时候,用到int类型的蛮多,需要注意一下: 1. 值的范围 Type Storage Minimum Value Maximum Value   (Bytes) (Signed/Uns ...

随机推荐

  1. 【转】STL之二分查找 (Binary search in STL)

    Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range 本文是对Effective STL第4 ...

  2. 十位一线专家分享Spark现状与未来----峰会摘录

    CSDN大数据技术: 十位一线专家分享Spark现状与未来(一) 十位一线专家分享Spark现状与未来(二) 十位一线专家分享Spark现状与未来(三) 部分摘录: 加州大学伯克利分校AMP实验室博士 ...

  3. [Algorithms(Princeton)] Week1 - Percolation

    public class Percolation { private boolean[] openSites; private int gridN; private WeightedQuickUnio ...

  4. POJ 3071 Football(概率DP)

    题目链接 不1Y都对不住看过那么多年的球.dp[i][j]表示i队进入第j轮的概率,此题用0-1<<n表示非常方便. #include <cstdio> #include &l ...

  5. ArcEngine开发异常:无当前记录

    使用 IFeatureWorkspace.CreateFeatureClass() 方法,出现异常:无当前记录 百度/谷歌没有找到合适的解决之道. 而是用IFeatureWorkspace.Creat ...

  6. FreeBSD Intel SYSRET Kernel Privilege Escalation Exploit

    /* * FreeBSD 9.0 Intel SYSRET Kernel Privilege Escalation exploit * Author by CurcolHekerLink * * Th ...

  7. HTML练习----注册界面

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. MySQL表的创建和表中数据操作

    这篇文章主要介绍在navicat的命令界面操作mysql.主要涉及建立表结构,和对表中数据的增加删除修改查询等动作.站在一个新手角度的简单mysql表结构和数据操作. ☆ 准备工作 1,保证自己的电脑 ...

  9. windows下Ruby开发环境搭建

    一.下载 下载RubyInstaller 二.安装 按照提示安装 安装完毕后,在cmd命令窗口,输入:ruby -v 查询rbuy版本:输入gem -v 查询gem版本 三.Ruby插件(Redis插 ...

  10. Android系统目录结构

    Android系统编译后生成三个映像文件,都是用cpio打包,gzip压缩的. ramdisk.img     文件系统,包含/system, /data, /bin等目录.kernel启动时负责初始 ...