1、整数类型
整数类型:TINYINT SMALLINT MEDIUMINT INT BIGINT
作用:存储年龄,等级,id,各种号码等
======================================== tinyint[(m)] [unsigned] [zerofill] 小整数,数据类型用于保存一些范围的整数数值范围: 有符号: -128 ~ 127 无符号: 0 ~ 255 PS: MySQL中无布尔值,使用tinyint(1)构造。 ======================================== int[(m)][unsigned][zerofill] 整数,数据类型用于保存一些范围的整数数值范围: 有符号: -2147483648 ~ 2147483647 无符号: 0 ~ 4294967295 ======================================== bigint[(m)][unsigned][zerofill] 大整数,数据类型用于保存一些范围的整数数值范围: 有符号: -9223372036854775808 ~ 9223372036854775807 无符号: 0 ~ 18446744073709551615
=========有符号和无符号tinyint==========
#tinyint默认为有符号
mysql> create database db4 charset utf8; Query OK, 1 row affected (0.12 sec) mysql> use db4; Database changed
默认为有符号,即数字前有正负号
mysql> create table t1(id tinyint); Query OK, 0 rows affected (0.32 sec) mysql> desc t1; +-------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------------+------+-----+---------+-------+ | id | tinyint(4) | YES | | NULL | | +-------+------------+------+-----+---------+-------+ 1 row in set (0.21 sec)
验证
mysql> insert into t1 values(-1),(-2); Query OK, 2 rows affected (0.09 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from t1; +------+ | id | +------+ | -1 | | -2 | +------+ 2 rows in set (0.06 sec)
mysql> insert into t1 values(-129),(-128),(127),(128); Query OK, 4 rows affected, 2 warnings (0.03 sec) Records: 4 Duplicates: 0 Warnings: 2 mysql> select * from t1; +------+ | id | +------+ | -128 | | -128 | | 127 | | 127 | +------+ 4 rows in set (0.00 sec)
#-129存成了-128
#有符号,最小值为-128
#有符号,最大值127
#128存成了127
#设置无符号tinyint
后面加上unsignded
mysql> create table t2(id tinyint unsigned); Query OK, 0 rows affected (0.06 sec)
验证
mysql> insert into t2 values(-1),(0),(127),(256); Query OK, 4 rows affected, 2 warnings (0.01 sec) Records: 4 Duplicates: 0 Warnings: 2 mysql> select * from t2; +------+ | id | +------+ | 0 | | 0 | | 127 | | 255 | +------+ 4 rows in set (0.00 sec)
-1存成了0
无符号,最小值为0
#无符号,最大值为255
#256存成了255