当前位置:网站首页>tinyint和int区别
tinyint和int区别
2022-06-12 15:28:00 【AlbenXie】
问题:int(1) 和 tinyint(1) 有什么区别呢?
像这样的设计,反正我是不会写int(1)的。
查了下,mysql中设置了储存类型后,储存是定长的,也就是说,int(1) 和 int(4) 在硬盘中所占的字节数都是一样的。
我们知道,int类型占4个字节,tinyint占1个字节。int(1) 和 int(4) 从本身长度还是存储方式上都是一样的,区别就是显示的长度不同,但是要设置一个参数:如果列制定了zerofill 就会用0填充显示,int(4)指定后就会显示为0002。
int(1)和tinyint(4) 相比,肯定int 大。
注意数字类型后面括号中的数字,不表示长度,表示的是显示宽度,这点与 varchar、char 后面的数字含义是不同的。
也就是说不管 int 后面的数字是多少,它存储的范围始终是 -2^31 到 2^31 - 1。
综上整型的数据类型括号内的数字不管是多少,所占的存储空间都是一样
tinyint一个字节 smallint 两个字节 MEDIUMINT三个字节
显而易见,int(1) 和 tinyint(1) ,在设计数据库的时候应该选择tinyint(1)。所占的储存空间越少越好,当然要够用才行。像这样储存一个 个位数的字段,还是用tinyint(1)的好。
总结:
1.规定类型之后,存储是定长的,int(1)和int(4)从本身长度还是存储方式上都是一样的。mysql里,int(1)和int(4)的区别就是显示的长度,但是要设置一个参数:如果列制定了zerofill 就会用0填充显示,如2 int(3)指定后就会显示为002
2.int 存储占4个字节, tinyint 存储占1个字节,存储长度决定了他们表示的数字范围不同。int表示的数字范围是:从 -2^31 (-2,147,483,648) 到 2^31 – 1 (2,147,483,647) 的整型数据(所有数字)。tinyint 表示的范围是0-255之间的数字。
3.tinyint(1),和tinyint(3)没什么区别,存123都能存的下,而如果tinyint(3) zerofill 的话,插入值 12,会存储012,zerofill自动左边补零,这才是限制显示长度。
上面总结的有点乱。下面精简总结:
- tinyint(1) 和 tinyint(3) 没什么区别,占用字节都是一位,存储范围都是一样的
- tinyint(3) zerofill ,当插入的数据少于3位的时候,左边自动补零,这才是限制显示长度
- int(1) 和 tinyint(1) ,够用的情况下,优先选择tinyint(1),因为占字节少、节省空间。
- tinyint一个字节 smallint 两个字节 MEDIUMINT三个字节 int 4个字节 BIGINT 8个字节。
但是,varchar(5) 这里的5 限制的是储存字符的个数,字符不分贵贱(不分 中文、英文、数字...)。
边栏推荐
猜你喜欢

Design concept of ORM framework

Seaborn的简述

Jupyter notebook new environment shortcut

解决log4j2漏洞遭到挖矿、僵尸进程病毒攻击

Tcp/ip three handshakes and four waves (interview questions)

C语言 分割bin文件程序

Idea大全(转载)

IGMP报文(TCP/IP详解卷1/卷2)

Deepin20.6 rtx3080 installer le lecteur de carte graphique 510.60.02, cuda 11.6, pytorch1.11

Acwing summer daily question (sexy prime number on June 10)
随机推荐
Raccourci vers le nouvel environnement du carnet de notes Jupiter
Singleton mode instance
[LDA] rough version notes of EM variational reasoning [to be improved
C language partition bin file program
Jupyter notebook new environment shortcut
ROS beginners write the server that the little turtle rotates a certain angle at a certain speed
[jvm learning] local method stack and heap
Getting started with JMeter
Wild pointer understanding
Use and understanding of generics
Task fruit Juicer 0611
Find the number of cells (connectivity map, wide search, deep search)
Two ways of array simulating queue
Servlet连接数据库实现用户登录功能
Kinect2.0+ORBSLAM2_ with_ pointcloud_ map
Remote control of other computers -- detailed tutorial
Pta: self test -2 prime pair conjecture (20 points)
作業提交說明 上傳作業到網盤中
【无标题】
Conversion between sparse array and array and file reading and writing