数据类型
一、介绍:mysql常用数据类型概览
二、数值类型
1、整型
tinyint 1bytes
小整数,数据类型用于保存一些范围的整数数值范围:
有符号:
-128 ~ 127
无符号:
0 ~ 255
smallint 2bytes
小整数,数据类型用于保存一些范围的整数数值范围:
有符号:
-32768 ~ 32767
无符号:
0 ~ 65535
int 4bytes
整数,数据类型用于保存一些范围的整数数值范围:
有符号:
-2147483648 ~ 2147483647
无符号:
0 ~ 4294967295
bigint 8bytes
大整数,数据类型用于保存一些范围的整数数值范围:
有符号:
-9223372036854775808 ~ 9223372036854775807
无符号:
0 ~ 18446744073709551615
'''不同类型的int能够存储的数字范围不一样'''
2、验证
3、浮点型
float(255,30) [UNSIGNED] [ZEROFILL]
总共255位,小数位占30位
double(255,30) [UNSIGNED] [ZEROFILL]
总共255位,小数位占30位
decimal(65,30) [UNSIGNED] [ZEROFILL]
总共65位,小数占30位
'''
三者精确度不同
decimal > double > float
具体使用结合实际情况
'''
4、验证
mysql> create table t5(id float(255,30));
mysql> create table t6(id double(255,30));
mysql> create table t7(id decimal(65,30));
mysql> insert into t5 values(1.111111111111111111111111);
mysql> insert into t6 values(1.111111111111111111111111);
mysql> insert into t7 values(1.111111111111111111111111);
mysql> select * from t5;
+----------------------------------+
| num |
+----------------------------------+
| 1.111111164093017600000000000000 |
+----------------------------------+
mysql> select * from t6;
+----------------------------------+
| num |
+----------------------------------+
| 1.111111111111111200000000000000 |
+----------------------------------+
mysql> select * from t7;
+----------------------------------+
| num |
+----------------------------------+
| 1.111111111111111111111111000000 |
+----------------------------------+
三、字符串类型
1、字符类型
2、验证
char(10)
3、解决SQL_MODE问题
4、char与varchar差异
1.先取消底层优化操作
mysql> set global sql_mode='strict_trans_tables,pad_char_to_full_length'
2.统计某个字段数据对应的长度
char_length() # 查看字符数
# 1.验证char定长特性
create table t10(id int,name char(5));
insert into t10 values(1,'tom');
create table t11(id int,name varchar(5));
insert into t11 values(1,'tom');
mysql> select name,char_length(name) from t10;
+-------+-------------------+
| name | char_length(name) |
+-------+-------------------+
| tom | 5 |
+-------+-------------------+
mysql> select name,char_length(name) from t11;
+-------+-------------------+
| name | char_length(name) |
+-------+-------------------+
| tom | 3 |
+-------+-------------------+
# 2.到底哪个好
char
优势:整存整取 速度快
劣势:浪费存储空间
varchar
优势:节省存储空间
劣势:存取数据的时候都需要先考虑报头 速度较于char慢
char(6) varchar(6)
1bytes+jason1bytes+tony1bytes+jack1bytes+kevin
四、日期类型
1、日期
date 年月日 YYYY-MM-DD
datetime 年月日时分秒 YYYY-MM-DD HH:MM:SS
time 时分秒 HH:MM:SS
year 年 YYYY
2、验证
mysql> create table t10(
id int comment '编号',
name varchar(255) comment '姓名',
reg_time datetime comment '注册时间',
birthday date comment '生日',
study_time time comment '学习时间',
born_year year comment '年份'
);
mysql> insert into t10 values(1,'jason','2000-11-11 11:11:11','2000-11-11','11:11:11','2000');
mysql> select * from t10;
+------+------+---------------------+------------+------------+-----------+
| id | name | reg_time | birthday | study_time | born_year |
+------+------+---------------------+------------+------------+-----------+
| 1 | momo | 2021-11-11 11:11:11 | 2021-11-11 | 11:11:11 | 2021 |
+------+------+---------------------+------------+------------+-----------+
"""
字段也可以加类似于注释的说明
comment
"""
五、枚举与集合
1、枚举
# 多选一 enum
'''提前定义好数据之后 后续录入只能录定义好的内容之一'''
mysql> create table t11(
-> id int,
-> name varchar(32),
-> gender enum('male','female')
-> );
mysql> desc t11;
+--------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-----------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(32) | YES | | NULL | |
| gender | enum('male','female') | YES | | NULL | |
+--------+-----------------------+------+-----+---------+-------+
mysql> insert into t11 values(1,'momo','male');
mysql> select * from t11;
+------+------+--------+
| id | name | gender |
+------+------+--------+
| 1 | momo | male |
+------+------+--------+
mysql> insert into t11 values(1,'lily','女'); # 报错
ERROR 1265 (01000): Data truncated for column 'gender' at row 1
2、集合
# 多选多(包含多选一) set
mysql> create table t12(
-> id int,
-> name varchar(32),
-> hobbies set('play','music','read')
-> );
mysql> desc t12;
+---------+----------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+----------------------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(32) | YES | | NULL | |
| hobbies | set('play','music','read') | YES | | NULL | |
+---------+----------------------------+------+-----+---------+-------+
mysql> insert into t12 values
-> (1,'jason','play'),
-> (2,'kevin','play,read'),
-> (3,'tom','music');
mysql> select * from t12;
+------+-------+-----------+
| id | name | hobbies |
+------+-------+-----------+
| 1 | jason | play |
| 2 | kevin | play,read |
| 3 | tom | music |
+------+-------+-----------+