mysql可以存储整数数值的是_MySQL的数值类型

MySQL的数值类型

整数类型

整数类型包含TINYINT、SMALLINT、MEDIUMINT、INT、 BIGINT等。

存取范围类型存储大小默认显示宽度(个)范围(有符号)范围(无符号)用途TINYINT(m)1Bytem:4-128 - 1270 - 255小整数值

SMALLINT(m)2Bytem:6-32768 - 327670 - 65535大整数值

MEDIUMINT(m)3Bytem:9-8388608 - 83886070 - 16777215大整数值

INT\INTEGER(m)4Bytem:11-2147483648 - 21474836470 - 4294967295大整数值

BIGINT(m)8Bytem:20-9233372036854775808 - 92233720368547758070 - 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 1mysql> select * from test;

+----+------+------+------+------+------+

| id | a | b | c | d | e |

+----+------+------+------+------+------+

| 3 | 0001 | 1 | 1 | 1 | 1 |

+----+------+------+------+------+------+

1 row in set (0.00 sec)

范围超出

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

浮点类型

浮点类型包括FLOAT、DOUBLE、DECIMAL。

存取范围类型存储大小最大显示宽度(个)范围(有符号)范围(无符号)精确度FLOAT(m[,d])4Bytesm:255,d:30(-3.402 823 466 E+38,-1.175 494 351 E-38) - 00 - (1.175 494 351 E-38,3.402 823 466 E+38)点七位以内

DOUBLE(m[,d])8Bytesm:255,d:30(-1.797 693 134 862 315 7 E+308,-2.225 073 858 507 201 4 E-308) - 00 - (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为其整数部分显示个数,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)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值