mysql integer size 大小

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)

时间: 2024-11-06 08:33:06

mysql integer size 大小的相关文章

MYSQL BLOB 字段大小以及个数的限制测试。

测试结论 mysql版本 5.1 表类型: innodb, row_format=compact (这是默认的行格式) 插入超过10个blob, blob的数据量很小(<768字节), 插入成功. 插入超过10个blob, blob的数据量很大(>768字节), 插入失败:报 Got error 139 from storage engine. 注意,如果mysql服务器版本是5.1, innodb_file_format选项不存在, 也就无从谈起Barracuda格式. 设置row_form

MYSQL BLOB 字段大小以及个数的限制測试。

測试结论mysql版本号 5.1    表类型: innodb, row_format=compact (这是默认的行格式)    插入超过10个blob, blob的数据量非常小(<768字节), 插入成功.    插入超过10个blob, blob的数据量非常大(>768字节), 插入失败:报 Got error 139 from storage engine.     注意,假设mysqlserver版本号是5.1, innodb_file_format选项不存在, 也就无从谈起Barr

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

通过下面的例子,来了解integer比较大小需注意的几点. eg.定义Integer对象a和b,比较两者结果为:a不等于b 1 Integer a = 1; 2 Integer b = 1; 3 if(a==b){ 4 System.out.print("a等于b"); 5 }else{ 6 System.out.print("a不等于b"); 7 } 因为Integer是对象类型,虽然两者内容相同,但是两者是不同的两个对象,在虚拟机中完全是不相干的2个对象,指向不

Integer对象大小比较问题

一.问题 先来看一看例子 public class IntegerTest { public static void main(String[] args) throws Exception { Integer a1 = 127; Integer b1 = Integer.valueOf(127); System.out.println("1:"+(a1 == b1)); Integer a2 = 127; Integer b2 = 127; System.out.println(&q

Mysql 查看数据库大小

1 命令行进入数据库 [[email protected] ~]# mysql -uroot -p Enter password: 2 查看数据库 mysql> show databases; +--------------------+ | Database           | +--------------------+ | information_schema | | mysql              | | test               | +--------------

iOS根据网络图片的size大小设置UIImageView的大小

有时候在设置UIImageView的大小时候需要根据UIimage的长宽比来自动设置,不让图片原比例失真. 如果是从本地获取到的图片,[UIImage imageNamed:@""]; 这样就可以拿到image了,从而获取到image的size.但大多数时候我们都是网络请求拿到的图片, 我们需要 NSData *data = [NSData dataWithContentsOfURL:url]; image = [UIImage imageWithData:data]; 这样来得到im

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

使用mysql的时候,用到int类型的蛮多,需要注意一下: 1. 值的范围 Type Storage Minimum Value Maximum Value   (Bytes) (Signed/Unsigned) (Signed/Unsigned) TINYINT 1 -128 127     0 255 SMALLINT 2 -32768 32767     0 65535 MEDIUMINT 3 -8388608 8388607     0 16777215 INT 4 -214748364

mysql查询数据库大小和表

每个mysql都有一个库information_schema,里面有一张表TABLES存储了所有数据库表的信息,因此,可以从这张表中查看数据库大小和表大小 查询数据库大小 select concat(round((sum(data_length)+sum(index_length))/1024/1024/1024,2),'GB') as data from information_schema.tables where table_schema='esb'; 查询数据库中表大小 select c

mysql查询区分大小

查询时加上binary关键词就能在查询时区分字符大小如下: mysql> select name from pe where binary name='Claws';+-------+| name  |+-------+| Claws |+-------+mysql> select name from pe where binary name='claws';Empty set (0.00 sec)like条件的使用: mysql> select name from pe where n