mysql int范围与最大值分析

时间:2012-03-26 18:24:00 

   这个代表显示宽度
整数列的显示宽度与mysql需要用多少个字符来显示该列数值,与该整数需要的存储空间的大小都没有关系,比如,不管设定了显示宽度是多少个字符,bigint都要占用8个字节。


    int是整型,(11)是指显示字符的长度,但要加参数的,最大为255,比如它是记录行数的id,插入10笔资料,它就显示00000000001 ~~~00000000010,当字符的位数超过11,它也只显示11位,如果你没有加那个让它未满11位就前面加0的参数,它不会在前面加0


声明整型数据列时,我们可以为它指定个显示宽度M(1~255),如INT(5),指定显示宽度为5个字符,如果没有给它指定显示宽度,MySQL会为它指定一个默认值。显示宽度只用于显示,并不能限制取值范围和占用空间,如:INT(3)会占用4个字节的存储空间,并且允许的最大值也不会是999,而是INT整型所允许的最大值。

MySQL有五种整型数据列类型,即TINYINT,SMALLINT,MEDIUMINT,INT和BIGINT。它们之间的区别是取值范围不同,存储空间也各不相同。
在整型数据列后加上UNSIGNED属性可以禁止负数,取值从0开始。

int范围


Type Bytes Minimum Value Maximum Value
    (Signed/Unsigned) (Signed/Unsigned)
TINYINT 1 -128 127
    0 255
SMALLINT 2 -32768 32767
    0 65535
MEDIUMINT 3 -8388608 8388607
    0 16777215
INT 4 -2147483648 2147483647
    0 4294967295
BIGINT 8 -9223372036854775808 9223372036854775807
    0 18446744073709551615


下面为官网的说明

 代码如下 复制代码
Be careful when considering ENUM('T','F') as "true binary".

Example:
CREATE TABLE `bits` (
`val` ENUM('T','F') NOT NULL
);

mysql> INSERT INTO `bits` (`val`) VALUES ('W'), ('T'), ('F');
Query OK, 3 rows affected, 1 warning (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 1

mysql> SHOW WARNINGS;
 


+---------+------+------------------------------------------+| Level   | Code | Message                                  |+---------+------+------------------------------------------+| Warning | 1265 | Data truncated for column 'val' at row 1 |+---------+------+------------------------------------------+1 row in set (0.00 sec)

mysql> SELECT COUNT(DISTINCT val) FROM bits;

+---------------------+| COUNT(DISTINCT val) |+---------------------+|                   3 |+---------------------+1 row in set (0.00 sec)

Well, shouldn't a binary type have only two distinct values?
(Note that it isn't NULL.)

Explanation from manual (10.4.4. The ENUM Type):
-----
If you insert an invalid value into an ENUM (that is, a string not present in the list of permitted values), the empty string is inserted instead as a special error value. This string can be distinguished from a “normal” empty string by the fact that this string has the numeric value 0. More about this later

标签:mysql
0
投稿

猜你喜欢

  • python实现记事本功能

    2022-04-02 09:54:19
  • asp制作验证码的方法

    2008-05-08 12:50:00
  • Keepalived+HAProxy实现MySQL高可用负载均衡的配置

    2024-01-18 23:08:52
  • python使用matplotlib画饼状图

    2023-04-06 12:23:29
  • js constructor的实际作用分析

    2024-02-25 01:41:31
  • 谈谈CSS样式表的命名规范

    2007-10-08 12:41:00
  • Python 不同对象比较大小示例探讨

    2023-06-11 01:13:32
  • 使用XML技术上传文件的例子

    2008-05-29 11:33:00
  • python的移位操作实现详解

    2021-05-05 22:51:30
  • tensorflow2.0如何实现cnn的图像识别

    2022-05-01 21:19:58
  • tensorflow 使用flags定义命令行参数的方法

    2021-03-20 10:43:23
  • Python实现的微信好友数据分析功能示例

    2023-09-18 06:00:21
  • Python3 元组tuple入门基础

    2023-01-16 12:13:22
  • Yahoo!网站性能最佳体验的34条黄金守则——图片、Coockie与移动应用

    2008-05-29 13:44:00
  • 爬虫Python验证码识别入门

    2021-01-31 15:31:19
  • 内网ssh/mysql登录缓慢的解决方法

    2024-01-15 15:41:44
  • JS实现淘宝支付宝网站的控制台菜单效果

    2023-07-22 22:26:12
  • asp textarea 多行数组分割处理方法

    2011-03-03 10:55:00
  • python模块内置属性概念及实例

    2023-11-23 15:34:27
  • python提取图像的名字*.jpg到txt文本的方法

    2021-10-31 00:54:44
  • asp之家 网络编程 m.aspxhome.com