博客园  :: 首页  :: 管理

关于MySQL中整数类型-IntegerTypes-的说明

Posted on 2022-11-27 14:36  520_1351  阅读(229)  评论(0编辑  收藏  举报

本文Mysql的版本为当前最新的:Server version: 8.0.30 MySQL Community Server - GPL

对于Mysql中对整数类型的支持说明如下:

MySQL supports the SQL standard integer types INTEGER (or INT) and SMALLINT. As an extension to the standard,

MySQL also supports the integer types TINYINTMEDIUMINT, and BIGINT. The following table shows the required storage and range for each integer type.

因此Mysql支持5种类型用来表示整型数字,分别用1,2,3,4,8 个字节来表示

tinyint   smallint    mediumint    int     bigint

表示的范围如下图所示:

对于上面的整数类型,创建表时,不特殊声明,默认就是有符号类型的,即同时支持 负数,0,正数

对于无符号的整数类型,创建时,需要在类型后,加上unsigned进行特别声明,如下:

mysql> create table integer_demo (
    -> a int,
    -> b int unsigned
    -> );
Query OK, 0 rows affected (0.02 sec)

这样integer_demo表的b字段,就只能存储规定范围内的自然数了,不能存储负数

另外,笔者曾查询过,对于负数的存储原理,是取负数的绝对值的原码,再取其反码,最后再+1得到补码进行存储,因此tinyint 有符号类型,最小的为-128

因此如-128在数据库的存储为 1000 0000, -1的存储为1111 1111   ,可以知道负数的最高位为始终为1

 

尊重别人的劳动成果 转载请务必注明出处:https://www.cnblogs.com/5201351/p/16929644.html