MySQL的数值类型
来源:SegmentFault
时间:2023-02-19 08:17:31 484浏览 收藏
数据库小白一枚,正在不断学习积累知识,现将学习到的知识记录一下,也是将我的所得分享给大家!而今天这篇文章《MySQL的数值类型》带大家来了解一下MySQL的数值类型,希望对大家的知识积累有所帮助,从而弥补自己的不足,助力实战开发!
MySQL的数值类型
整数类型
整数类型包含
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)
范围超出
当范围超出时则不允许存取,抛出异常。
浮点类型
浮点类型包括
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)
位类型
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的相关知识,希望对你有所帮助;关于数据库的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
499 收藏
-
244 收藏
-
235 收藏
-
157 收藏
-
101 收藏
最新阅读
更多>
-
366 收藏
-
265 收藏
-
499 收藏
-
423 收藏
-
487 收藏
-
264 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习