欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > 【MySQL】04.数据类型

【MySQL】04.数据类型

2025/5/25 0:06:44 来源:https://blog.csdn.net/2301_80258336/article/details/148090202  浏览:    关键词:【MySQL】04.数据类型

首先我们来看一下数据类型分类:

我们接下来对红色标识的进行介绍,其他的自行了解即可。 

1. 数值类型

1.1 bit

我们以bit为例来介绍整型。

mysql> create table test_bit(-> sex bit(1)-> );
mysql> desc test_bit;
+-------+--------+------+-----+---------+-------+
| Field | Type   | Null | Key | Default | Extra |
+-------+--------+------+-----+---------+-------+
| sex   | bit(1) | YES  |     | NULL    |       |
+-------+--------+------+-----+---------+-------+
1 row in set (0.00 sec)mysql> insert into test_bit values(1);
Query OK, 1 row affected (0.00 sec)mysql> insert into test_bit values(0);
Query OK, 1 row affected (0.01 sec)mysql> insert into test_bit values(2);
ERROR 1406 (22001): Data too long for column 'sex' at row 1

我们可以看到:MySQL不会像C/C++一样对超出范围的数据截断,即只要成功插入的数据就是合法的,数据类型本身也是一种约束。

1.2 float

我们以float来介绍浮点型。

mysql> create table test_float(-> score float(4,2)-> );
Query OK, 0 rows affected, 1 warning (0.04 sec)mysql> insert into test_float values(99.94);
Query OK, 1 row affected (0.01 sec)mysql> insert into test_float values(99.949);
Query OK, 1 row affected (0.00 sec)mysql> insert into test_float values(99.950);
Query OK, 1 row affected (0.00 sec)mysql> insert into test_float values(100.100);
ERROR 1264 (22003): Out of range value for column 'score' at row 1
mysql> select * from test_float;
+-------+
| score |
+-------+
| 99.94 |
| 99.95 |
| 99.95 |
+-------+
3 rows in set (0.00 sec)

我们可以看出他会自动进行四舍五入。

decimal较float相比更精确一些。

2. 文本类型

2.1 char

mysql> create table test_char(-> name char(2)-> );
Query OK, 0 rows affected (0.03 sec)mysql> insert into test_char values('aa');
Query OK, 1 row affected (0.00 sec)mysql> insert into test_char values('aaa');
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> insert into test_char values('中国');
Query OK, 1 row affected (0.00 sec)

可以看出存储单位为字符,且只能存储定长字符。

2.2 varchar

mysql> create table test_varchar(-> name varchar(6)-> );
Query OK, 0 rows affected (0.03 sec)mysql> insert into test_varchar values('zhongg');
Query OK, 1 row affected (0.01 sec)mysql> insert into test_varchar values('zhonggu');
ERROR 1406 (22001): Data too long for column 'name' at row 1

关于varchar(len),len值和表的编码密切相关: varchar长度可以指定为0到65535之间的值,但是有1 - 3 个字节用于记录数据大小,所以说有效字节数是65532。 当我们的表的编码是utf8时,varchar(n)的参数n最大值是65532/3=21844[因为utf中,一个字符占用3个字节],如果编码是gbk,varchar(n)的参数n最大是65532/2=32766(因为gbk中,一个字符占用2字节)。

char和varchar对比:

如何选择定长或变长字符串?
如果数据确定长度都一样,就使用定长(char),比如:身份证,手机号,md5 如果数据长度有变化,就使用变长(varchar), 比如:名字,地址,但是你要保证最长的能存的进去。 定长的磁盘空间比较浪费,但是效率高。 变长的磁盘空间比较节省,但是效率低。 定长的意义是,直接开辟好对应的空间 变长的意义是,在不超过自定义范围的情况下,用多少,开辟多少。 

3. 时间日期

• date:日期 'yyyy-mm-dd' ,占用三字节
• datetime: 时间日期格式 'yyyy-mm-dd HH:ii:ss' 表示范围从 1000 到 9999 ,占用八字节  
 timestamp:时间戳,从1970年开始的 yyyy-mm-dd HH:ii:ss 格式和 datetime 完全一致,占用 四字节


mysql> create table birthday (t1 date, t2 datetime, t3 timestamp);
Query OK, 0 rows affected (0.03 sec)mysql> insert into birthday(t1,t2,t3) values('1970-08-31','2005-06-23 7:30:23','2005-06-23 7:30:23');
Query OK, 1 row affected (0.00 sec)mysql> select * from birthday;
+------------+---------------------+---------------------+
| t1         | t2                  | t3                  |
+------------+---------------------+---------------------+
| 1970-08-31 | 2005-06-23 07:30:23 | NULL                |
| 1970-08-31 | 2005-06-23 07:30:23 | 2005-06-23 07:30:23 |
+------------+---------------------+---------------------+
2 rows in set (0.00 sec)

4. String 类型

enum:枚举,“单选”类型;
set:集合,“多选”类型;

mysql> create table info( id int, gender enum('男','女'), hobby set('爬山','游泳','篮球') );
Query OK, 0 rows affected (0.03 sec)mysql> insert into info values(1,'男','爬山,篮球');
Query OK, 1 row affected (0.00 sec)mysql> insert into info values(2,'女','爬山');
Query OK, 1 row affected (0.01 sec)mysql> select *from info;
+------+--------+---------------+
| id   | gender | hobby         |
+------+--------+---------------+
|    1 | 男     | 爬山,篮球     |
|    2 | 女     | 爬山          |
+------+--------+---------------+
2 rows in set (0.00 sec)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词