当前位置:网站首页>四、MYSQL数据类型
四、MYSQL数据类型
2022-08-06 15:42:00 【[email protected]】
目录
数值数据类型
整数类型
对于整数数据类型,
M表示最大显示宽度。最大显示宽度为 255。显示宽度与类型可以存储的值的范围无关对于浮点和定点数据类型,
M是可以存储的总位数
位值类型。
M表示每个值的位数,从 1 到 64。如果M省略,则默认为 1。TINYINT[(M)] [UNSIGNED] [ZEROFILL]
一个非常小的整数。有符号范围
-128为127。无符号范围是0to255。这些类型是 的同义词 TINYINT(1)。零值被认为是错误的。非零值被认为是真的:
示例:0表示错误,非0表示正确
mysql> SELECT IF(0, 'true', 'false');
+------------------------+
| IF(0, 'true', 'false') |
+------------------------+
| false |
+------------------------+
mysql> SELECT IF(1, 'true', 'false');
+------------------------+
| IF(1, 'true', 'false') |
+------------------------+
| true |
+------------------------+
mysql> SELECT IF(2, 'true', 'false');
+------------------------+
| IF(2, 'true', 'false') |
+------------------------+
| true |
+------------------------+SMALLINT[(M)] [UNSIGNED] [ZEROFILL]
一个小整数。有符号范围
-32768为32767。无符号范围是0to65535。MEDIUMINT[(M)] [UNSIGNED] [ZEROFILL]
一个中等大小的整数。有符号范围
-8388608为8388607。无符号范围是0to16777215。INT[(M)] [UNSIGNED] [ZEROFILL]
一个正常大小的整数。有符号范围
-2147483648为2147483647。无符号范围是0to4294967295。INTEGER[(M)] [UNSIGNED] [ZEROFILL]
这种类型是 的同义词 INT。
BIGINT[(M)] [UNSIGNED] [ZEROFILL]
一个大整数。有符号范围
-9223372036854775808为9223372036854775807。无符号范围是0to18446744073709551615。
定点类型
DECIMAL[(M[,D])] [UNSIGNED] [ZEROFILL]
一个压缩的“精确”定点数。 M是总位数(精度),D是小数点后的位数(小数位数)。小数点和(对于负数) -符号不计入 M。如果 D为 0,则值没有小数点或小数部分。最大位数 ( M) 为 DECIMAL65。支持的最大小数位数 ( D) 为 30。如果D省略,则默认为 0。如果M被省略,默认值为 10。
浮点类型
FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]
一个小的(单精度)浮点数。允许的值为-3.402823466E+38 to -1.175494351E-38、 0和1.175494351E-38 to 3.402823466E+38。这些是基于 IEEE 标准的理论限制。根据您的硬件或操作系统,实际范围可能会稍小一些。
M是总位数,D是小数点后的位数。如果省略M 和D,则将值存储在硬件允许的范围内。单精度浮点数精确到大约 7 位小数。
添加数据类型选项
zerofill:
填充0,影响数字的显示方式,如果一个数字的宽度小于所允许的最大宽度,这个值前面会用0填充,如果声明了zerofill,该列会自动设为unsigned
auto_increment:
自动增长,适用于整数类型,一个表中只有一个列是可以自动增长的。
示例:使用auto_increment自动增长
[email protected] 21:05 mysql>create table table2(id int auto_increment not null primary key,naeme varchar(20));
Query OK, 0 rows affected (0.01 sec)
[email protected] 21:08 mysql>desc table2;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
[email protected] 21:08 mysql>insert into table2(name) values('cali');
Query OK, 1 row affected (0.00 sec)
[email protected] 21:08 mysql>select * from table2
-> ;
+----+------+
| id | name |
+----+------+
| 1 | cali |
+----+------+
1 row in set (0.00 sec)
[email protected] 21:08 mysql>insert into table2(name) values('jack');
Query OK, 1 row affected (0.01 sec)
[email protected] 21:08 mysql>select * from table2;
+----+------+
| id | name |
+----+------+
| 1 | cali |
| 2 | jack |
+----+------+
2 rows in set (0.00 sec)
[email protected] 21:08 mysql>insert into table2(id,name) values(9,'tom');
Query OK, 1 row affected (0.00 sec)
[email protected] 21:11 mysql>select * from table2;
+----+------+
| id | name |
+----+------+
| 1 | cali |
| 2 | jack |
| 9 | tom |
+----+------+
3 rows in set (0.00 sec)
[email protected] 21:12 mysql>insert into table2(name) values('rose');
Query OK, 1 row affected (0.00 sec)
[email protected] 21:12 mysql>select * from table2;
+----+------+
| id | name |
+----+------+
| 1 | cali |
| 2 | jack |
| 9 | tom |
| 10 | rose |
+----+------+
4 rows in set (0.00 sec)
#################################################################################################
字符串字符类型
char和varchar的区别

ENUM枚举类型
枚举值必须是带引号的字符串文字
示例:
[email protected] 09:30 mysql>create table shirts(name varchar(40), size enum('x-small', 'small', 'medium', 'large', 'x-large'));
Query OK, 0 rows affected (0.00 sec)
[email protected] 09:31 mysql>desc shirts;
+-------+----------------------------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------------------------------------------------+------+-----+---------+-------+
| name | varchar(40) | YES | | NULL | |
| size | enum('x-small','small','medium','large','x-large') | YES | | NULL | |
+-------+----------------------------------------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
[email protected] 09:31 mysql>INSERT INTO shirts (name, size) VALUES ('dress shirt','large'), ('t-shirt','medium'),
-> ('polo shirt','small');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
[email protected] 09:32 mysql>select name, size from shirts;
+-------------+--------+
| name | size |
+-------------+--------+
| dress shirt | large |
| t-shirt | medium |
| polo shirt | small |
+-------------+--------+
3 rows in set (0.00 sec)
如果我们插入的是枚举值之外的字符串,会报错
[email protected] 09:33 mysql>INSERT INTO shirts (name, size) VALUES ('lining','xxxxlarge');
ERROR 1265 (01000): Data truncated for column 'size' at row 1
########################################################
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_48391148/article/details/126067789
边栏推荐
- Introduction to 15 days Web3, quick guide from 0 to 1
- Ant Group builds the industry's first full-picture risk control and explains the technical architecture in detail for the first time
- 实用新型专利申请文件撰写示例
- 迄今为止见过最详细的零拷贝技术讲解
- 重新生成一堆rpm目录的repo库步骤
- Another proof of the strength of China's manufacturing, the number of Chinese companies in the top 500 is the largest
- LeetCode: 206. The inversion list - simple
- 中国制造实力强横的又一证明,500强的中国企业数量最多
- LeetCode: 392. Judgment Subsequence —— Simple
- 【freertos】013-任务通知及其实现细节
猜你喜欢
随机推荐
Helm deployment ES and Kibana (default open SSL)
组件开发实战-数字输入框和标签页组件
[Summary of leetcode Weekly Competition]
Field userService in com.zher.reggie_task_out.controller.UserController required a bean of type ‘com
RTU通讯控制器S274如何操作
一个案例搞懂工厂模式和单例模式
网上开户佣金万一安全吗?手机办理流程是怎样的
IK分词器详解
【C补充】二维数组作为形式参数的注意事项
1322_FreeRTOS中的队列使用的信息梳理以及初步队列的使用
Easyexcel导出 只显示出想要的字段
网上开户佣金万一安全吗?需要什么资料办理
网络设备批量操作
蚂蚁集团搭建行业首个全图风控,首次详解技术架构
HJLL-99/A数字零序电流继电器
7 public accounts with excellent reputation!
[freertos] 013-task notification and its implementation details
【组成原理 七 I/O系统】
Kotlin 协程之取消与异常处理探索之旅(上)
scanpy category类型注意









