MySQL的数值类型

整数类型

整数类型包含TINYINTSMALLINTMEDIUMINTINTBIGINT等。

存取范围

类型 存储大小 默认显示宽度(个) 范围(有符号) 范围(无符号) 用途
TINYINT(m) 1Byte m:4 -128 - 127 0 - 255 小整数值
SMALLINT(m) 2Byte m:6 -32768 - 32767 0 - 65535 大整数值
MEDIUMINT(m) 3Byte m:9 -8388608 - 8388607 0 - 16777215 大整数值
INT|INTEGER(m) 4Byte m:11 -2147483648 - 2147483647 0 - 4294967295 大整数值
BIGINT(m) 8Byte m:20 -9233372036854775808 - 9223372036854775807 0 - 18446744073709551615 极大整数值

m为其显示宽度,在为字段设置 zerofill约束条件时有效,否则将不会填充满整个显示宽度。

可选约束

unsigned:使用无符号存储。

zerofill:显示宽度不够时使用0进行填充。

显示宽度

使用一切数值类型时,指定其宽度均是为其指定显示宽度,并非存入的限制宽度。

以下示例将演示为TINYINT类型设置设置了显示宽度后,当宽度不够时将以指定字符进行填充。

mysql> CREATE TABLE `test` (
    ->   `id` int(11) NOT NULL AUTO_INCREMENT,
    ->   `a` tinyint(4) unsigned zerofill DEFAULT NULL,
    ->   `b` smallint(6) unsigned DEFAULT NULL,
    ->   `c` mediumint(9) DEFAULT NULL,
    ->   `d` int(11) DEFAULT NULL,
    ->   `e` bigint(20) DEFAULT NULL,
    ->   PRIMARY KEY (`id`)
    -> ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
Query OK, 0 rows affected, 9 warnings (0.05 sec)
mysql> desc test;
+-------+------------------------------+------+-----+---------+----------------+
| Field | Type                         | Null | Key | Default | Extra          |
+-------+------------------------------+------+-----+---------+----------------+
| id    | int                          | NO   | PRI | NULL    | auto_increment |
| a     | tinyint(4) unsigned zerofill | YES  |     | NULL    |                |
| b     | smallint unsigned            | YES  |     | NULL    |                |
| c     | mediumint                    | YES  |     | NULL    |                |
| d     | int                          | YES  |     | NULL    |                |
| e     | bigint                       | YES  |     | NULL    |                |
+-------+------------------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
mysql> INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES ('1', '1', '1', '1', '1');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO `test` (`id`, `a`, `b`, `c`, `d`, `e`) VALUES ('3', '-1', '-1', '1', '1', '1');
ERROR 1264 (22003): Out of range value for column 'a' at row 1
mysql> INSERT INTO `test` (`a`, `b`, `c`, `d`, `e`) VALUES ('333', '333', '333', '333', '333');
ERROR 1264 (22003): Out of range value for column 'a' at row 1
mysql> select * from test;
+----+------+------+------+------+------+
| id | a    | b    | c    | d    | e    |
+----+------+------+------+------+------+
|  3 | 0001 |    1 |    1 |    1 |    1 |
+----+------+------+------+------+------+
1 row in set (0.00 sec)

范围超出

当范围超出时则不允许存取,抛出异常。

浮点类型

浮点类型包括FLOATDOUBLEDECIMAL

存取范围

类型 存储大小 最大显示宽度(个) 范围(有符号) 范围(无符号) 精确度
FLOAT(m[,d]) 4Bytes m:255,d:30 (-3.402 823 466 E+38,-1.175 494 351 E-38) - 0 0 - (1.175 494 351 E-38,3.402 823 466 E+38) 点七位以内
DOUBLE(m[,d]) 8Bytes m:255,d:30 (-1.797 693 134 862 315 7 E+308,-2.225 073 858 507 201 4 E-308) - 0 0 - (2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) 点十五位以内
DECIMAL(m[,d]) m+2(如果m m:65,d:30 取决于m,d(m范围(1-65),d范围(0-30)) 取决于m,d(m范围(1-65),d范围(0-30)) 绝对精准

m为其整数部分显示个数,n为其小数部分显示个数。

DECIMAL底层由字符串进行存储,故精度不会出现偏差,也被称为定点类型。

精度问题

mysql> CREATE TABLE `test` (
    -> `id` INT NOT NULL,
    -> `a` FLOAT NULL,
    -> `b` DOUBLE NULL,
    -> `c` DECIMAL NULL,
    -> PRIMARY KEY (`id`));
Query OK, 0 rows affected (0.02 sec)
mysql> desc test;
+-------+---------------+------+-----+---------+-------+
| Field | Type          | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| id    | int           | NO   | PRI | NULL    |       |
| a     | float         | YES  |     | NULL    |       |
| b     | double        | YES  |     | NULL    |       |
| c     | decimal(10,0) | YES  |     | NULL    |       |
+-------+---------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
INSERT INTO `test` (`id`, `a`, `b`,`c`) VALUES ('1', '3.1415', '3.14159','3.14159');

INSERT INTO `test` (`id`, `a`, `b`,`c`) VALUES ('2', '1.1111111111111111', '1.1111111111111111','1.1111111111111111');
mysql> select * from test;
+----+---------+--------------------+------+
| id | a       | b                  | c    |
+----+---------+--------------------+------+
|  1 |  3.1415 |            3.14159 |    3 |
|  2 | 1.11111 | 1.1111111111111112 |    1 |
+----+---------+--------------------+------+
2 rows in set (0.01 sec)
mysql> alter table test modify `c` DECIMAL(65,30) NULL;
Query OK, 2 rows affected (0.13 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> desc test;
+-------+----------------+------+-----+---------+-------+
| Field | Type           | Null | Key | Default | Extra |
+-------+----------------+------+-----+---------+-------+
| id    | int            | NO   | PRI | NULL    |       |
| a     | float          | YES  |     | NULL    |       |
| b     | double         | YES  |     | NULL    |       |
| c     | decimal(65,30) | YES  |     | NULL    |       |
+-------+----------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> select * from test;
+----+---------+--------------------+----------------------------------+
| id | a       | b                  | c                                |
+----+---------+--------------------+----------------------------------+
|  1 |  3.1415 |            3.14159 | 3.000000000000000000000000000000 |
|  2 | 1.11111 | 1.1111111111111112 | 1.000000000000000000000000000000 |
+----+---------+--------------------+----------------------------------+
2 rows in set (0.00 sec)

mysql> INSERT INTO `test` (`id`, `a`, `b`,`c`) VALUES ('3', '1.1111111111111111', '1.1111111111111111','1.1111111111111111');
Query OK, 1 row affected (0.00 sec)

mysql> select * from test;
+----+---------+--------------------+----------------------------------+
| id | a       | b                  | c                                |
+----+---------+--------------------+----------------------------------+
|  1 |  3.1415 |            3.14159 | 3.000000000000000000000000000000 |
|  2 | 1.11111 | 1.1111111111111112 | 1.000000000000000000000000000000 |
|  3 | 1.11111 | 1.1111111111111112 | 1.111111111111111100000000000000 |
+----+---------+--------------------+----------------------------------+
3 rows in set (0.00 sec)

位类型

BIT(M)可以用来存放多位二进制数,M范围从1~64,如果不写默认为1位。

注意:对于位字段需要使用函数读取

bin()显示为二进制

hex()显示为十六进制

mysql> create table `test`(num bit);
Query OK, 0 rows affected (0.03 sec)

mysql> desc test;
+-------+--------+------+-----+---------+-------+
| Field | Type   | Null | Key | Default | Extra |
+-------+--------+------+-----+---------+-------+
| num   | bit(1) | YES  |     | NULL    |       |
+-------+--------+------+-----+---------+-------+
1 row in set (0.01 sec)

mysql> insert into `test`(num) values (1);
Query OK, 1 row affected (0.01 sec)

mysql> select * from test;
+------------+
| num        |
+------------+
| 0x01       |
+------------+
1 row in set (0.00 sec)

mysql> select bin(num),hex(num) from test;
+----------+----------+
| bin(num) | hex(num) |
+----------+----------+
| 1        | 1        |
+----------+----------+
1 row in set (0.00 sec)

mysql> alter table `test` modify num bit(5);
Query OK, 1 row affected (0.10 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> insert into `test`(num) values (8);
Query OK, 1 row affected (0.00 sec)

mysql> select * from test;
+------------+
| num        |
+------------+
| 0x01       |
| 0x08       |
+------------+
2 rows in set (0.00 sec)

mysql> select bin(num),hex(num) from test;
+----------+----------+
| bin(num) | hex(num) |
+----------+----------+
| 1        | 1        |
| 1000     | 8        |
+----------+----------+
2 rows in set (0.00 sec)

你可能感兴趣的:(MySQL的数值类型)