MySQL数据类型

文章目录

  • 数据类型分类
  • 数值类型
    • tinyint类型
    • bit类型
    • float类型
    • decimal类型
  • 字符串类型
    • char类型
    • varchar类型
    • char和varchar对比
    • 日期和时间类型
    • enum和set类型

数据类型分类

MySQL中,支持各种各样的类型,比如表示数值的整型浮点型,文本、二进制类型、时间日期型和String类型。

MySQL数据类型_第1张图片


数值类型

MySQL数据类型_第2张图片

tinyint类型

tinyint类型用于存放数值比较小的整数,取值范围-128~127

数值越界测试:

mysql> create table t1 (
    -> data tinyint
    -> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.02 sec)

mysql> desc t1;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| data  | tinyint(4) | YES  |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> insert into t1 (data) values (1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t1 (data) values (128);
ERROR 1264 (22003): Out of range value for column 'data' at row 1
mysql> insert into t1 (data) values (-129);
ERROR 1264 (22003): Out of range value for column 'data' at row 1

mysql> select * from t1;
+------+
| data |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

在C/C++中,如果赋值超出类型范围,会发生对应的数据类型截断,编译器不会报错,但是在MySQL中,数据必须具有很强的明确性,超出数据类型的范围,MySQL不允许插入数据。
所以在MySQL中,数据类型是一种约束。

说明:

  • 在MySQL中,整型可以指定是有符号的和无符号的,默认是有符号的。
  • 可以通过UNSIGNED来说明某个字段是无符号的。

案例:

mysql> create table t2 (
    -> data tinyint unsigned 
    -> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.02 sec)

mysql> desc t2;
+-------+---------------------+------+-----+---------+-------+
| Field | Type                | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| data  | tinyint(3) unsigned | YES  |     | NULL    |       |
+-------+---------------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> insert into t2 (data) values (255);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t2 (data) values (-1);
ERROR 1264 (22003): Out of range value for column 'data' at row 1

mysql> select * from t2;
+------+
| data |
+------+
|  255 |
+------+
1 row in set (0.00 sec)

bit类型

基本语法:

bit[(M)] : 位字段类型。M表示每个值的位数,范围从164。如果M被忽略,默认为1

举例:

mysql> create table t3 (
    -> id int,
    -> data bit(8)
    -> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.02 sec)

mysql> desc t3;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
| data  | bit(8)  | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into t3 (id, data) values (10, 10);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t3;
+------+------+
| id   | data |
+------+------+
|   10 | 
+------+------+
1 row in set (0.00 sec)

通过上面的案例,我们发现bit字段在显示时不是直接显示数值大小的。

bit字段在显示时,是按照ASCII码对应的值显示的。

mysql> insert into t3 (id, data) values (65, 65);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t3;
+------+------+
| id   | data |
+------+------+
|   10 | 
|   65 | A    |
+------+------+
2 rows in set (0.00 sec)

bit可以表示只存放1或0的数值需求,这样可以节省空间。

mysql> create table t4 (
    -> data bit(1)
    -> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.02 sec)

mysql> desc t4;
+-------+--------+------+-----+---------+-------+
| Field | Type   | Null | Key | Default | Extra |
+-------+--------+------+-----+---------+-------+
| data  | bit(1) | YES  |     | NULL    |       |
+-------+--------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> insert into t4 (data) values (1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t4 (data) values (0);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t4 (data) values (2);
ERROR 1406 (22001): Data too long for column 'data' at row 1

float类型

基本语法:

float[(m, d)] [unsigned] : M指定显示长度,d指定小数位数,占用空间4个字节

案例:
float(4,2)表示的范围是-99.99 ~ 99.99,MySQL在保存值时会进行四舍五入。

mysql> create table t5 (
    -> data float(4, 2)
    -> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.02 sec)

mysql> desc t5;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| data  | float(4,2) | YES  |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> insert into t5 (data) values (99.99);
Query OK, 1 row affected (0.01 sec)

mysql> insert into t5 (data) values (-99.99);
Query OK, 1 row affected (0.01 sec)

mysql> insert into t5 (data) values (-99.991);
Query OK, 1 row affected (0.01 sec)

mysql> insert into t5 (data) values (-99.996);
ERROR 1264 (22003): Out of range value for column 'data' at row 1
mysql> select * from t5;
+--------+
| data   |
+--------+
|  99.99 |
| -99.99 |
| -99.99 |
+--------+
3 rows in set (0.00 sec)

案例:
如果定义的是float(4,2) unsigned 这时,因为把它指定为无符号的数,范围是 0 ~ 99.99

mysql> create table t6 (
    -> data float(4,2) unsigned 
    -> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.01 sec)

mysql> desc t6;
+-------+---------------------+------+-----+---------+-------+
| Field | Type                | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| data  | float(4,2) unsigned | YES  |     | NULL    |       |
+-------+---------------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> insert into t6 (data) values (0);
Query OK, 1 row affected (0.01 sec)

mysql> insert into t6 (data) values (99.99);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t6 (data) values (-1);
ERROR 1264 (22003): Out of range value for column 'data' at row 1
mysql> insert into t6 (data) values (100);
ERROR 1264 (22003): Out of range value for column 'data' at row 1

decimal类型

基本语法:

decimal(m, d) [unsigned] : 定点数m指定长度,d表示小数点的位数
  • decimal(5,2) 表示的范围是 -999.99 ~ 999.99
  • decimal(5,2) unsigned 表示的范围 0 ~ 999.99
  • decimal和float很像,但是有区别: float和decimal表示的精度不一样

案例:

mysql> create table t7 (
    -> data1 float(10,8),
    -> data2 decimal(10,8)
    -> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.02 sec)

mysql> desc t7;
+-------+---------------+------+-----+---------+-------+
| Field | Type          | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| data1 | float(10,8)   | YES  |     | NULL    |       |
| data2 | decimal(10,8) | YES  |     | NULL    |       |
+-------+---------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into t7 (data1, data2) values (23.12345612, 23.12345612);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t7;
+-------------+-------------+
| data1       | data2       |
+-------------+-------------+
| 23.12345695 | 23.12345612 |
+-------------+-------------+
1 row in set (0.00 sec)

通过上面的案例,我们可以发现decimal的精度更准确,因此当我们需要高精度储存浮点数时,优先使用decimal。

  • float表示的精度大约是7位。
  • decimal整数最大位数m为65。支持小数最大位数d是30。如果d被省略,默认为0.如果m被省略,默认是10。

字符串类型

char类型

基本语法:

char(L): 固定长度字符串,L是可以存储的长度,单位为字符,最大长度值可以为255

案例:

mysql> create table t8 (
    -> data char(2)
    -> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.02 sec)

mysql> desc t8;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| data  | char(2) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.01 sec)

mysql> insert into t8 (data) values ('OK');
Query OK, 1 row affected (0.01 sec)

mysql> insert into t8 (data) values ('好的');
Query OK, 1 row affected (0.00 sec)

mysql> insert into t8 (data) values ('中国人');
ERROR 1406 (22001): Data too long for column 'data' at row 1

mysql> select * from t8;
+--------+
| data   |
+--------+
| OK     |
| 好的   |
+--------+
2 rows in set (0.00 sec)

char(2) 表示可以存放两个字符,可以是字母或汉字,但是不能超过2个, 最多只能是255

mysql> create table t8 (
    -> data char(256)
    -> )engine=innodb default charset-utf8;
ERROR 1074 (42000): Column length too big for column 'data' (max = 255); use BLOB or TEXT instead

varchar类型

基本语法:

varchar(L): 可变长度字符串,L表示字符长度,最大长度65535个字节

案例:

mysql> create table t9 (
    -> data varchar(5)
    -> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.03 sec)

mysql> desc t9;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| data  | varchar(5) | YES  |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> insert into t9 value ('hello');
Query OK, 1 row affected (0.00 sec)

mysql> insert into t9 value ('你好,世界');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t9;
+-----------------+
| data            |
+-----------------+
| hello           |
| 你好,世界      |
+-----------------+
2 rows in set (0.00 sec)

关于varchar(len),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字节)。
mysql> create table t10 (
    -> data varchar(21845)
    -> )engine=innodb default charset=utf8;
ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs

mysql> create table t10 ( data varchar(21844) )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.02 sec)

mysql> desc t10;
+-------+----------------+------+-----+---------+-------+
| Field | Type           | Null | Key | Default | Extra |
+-------+----------------+------+-----+---------+-------+
| data  | varchar(21844) | YES  |     | NULL    |       |
+-------+----------------+------+-----+---------+-------+
1 row in set (0.00 sec)

char和varchar对比

MySQL数据类型_第3张图片

如何选择定长或变长字符串?

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

日期和时间类型

常用的日期有如下三个:

  • 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 t11 (
    -> d1 date,
    -> d2 datetime,
    -> d3 timestamp
    -> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.02 sec)

mysql> desc t11;
+-------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type      | Null | Key | Default           | Extra                       |
+-------+-----------+------+-----+-------------------+-----------------------------+
| d1    | date      | YES  |     | NULL              |                             |
| d2    | datetime  | YES  |     | NULL              |                             |
| d3    | timestamp | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+-------------------+-----------------------------+
3 rows in set (0.00 sec)

mysql> insert into t11 (d1, d2) values ('2023-1-29', '2023-1-29 10:00:00');
Query OK, 1 row affected (0.00 sec)

mysql> insert into t11 (d1, d2) values ('2023-1-30', '2023-1-30 11:00:00');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t11;
+------------+---------------------+---------------------+
| d1         | d2                  | d3                  |
+------------+---------------------+---------------------+
| 2023-01-29 | 2023-01-29 10:00:00 | 2023-01-29 21:45:20 |
| 2023-01-30 | 2023-01-30 11:00:00 | 2023-01-29 21:45:29 |
+------------+---------------------+---------------------+
2 rows in set (0.00 sec)

在添加数据时,timestamp时间戳会自动添加当前时间,修改数据时同理也会修改当前时间。

mysql> select * from t11;
+------------+---------------------+---------------------+
| d1         | d2                  | d3                  |
+------------+---------------------+---------------------+
| 2023-01-29 | 2023-01-29 10:00:00 | 2023-01-29 21:45:20 |
| 2023-01-30 | 2023-01-30 11:00:00 | 2023-01-29 21:45:29 |
+------------+---------------------+---------------------+
2 rows in set (0.00 sec)

mysql> update t11 set d1='2023-1-31';
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> select * from t11;
+------------+---------------------+---------------------+
| d1         | d2                  | d3                  |
+------------+---------------------+---------------------+
| 2023-01-31 | 2023-01-29 10:00:00 | 2023-01-29 21:47:55 |
| 2023-01-31 | 2023-01-30 11:00:00 | 2023-01-29 21:47:55 |
+------------+---------------------+---------------------+
2 rows in set (0.00 sec)

enum和set类型

基本语法:

enum:枚举,“单选”类型;
enum('选项1','选项2','选项3',...);

该设定只是提供了若干个选项的值,最终一个单元格中,实际只存储了其中一个值;而且出于效率考虑,这些值实际存储的是“数字”,因为这些选项的每个选项值依次对应如下数字:1,2,3,…最多65535个;当我们添加枚举值时,也可以添加对应的数字编号。

set:集合,“多选”类型;
set('选项值1','选项值2','选项值3', ...);

该设定只是提供了若干个选项的值,最终一个单元格中,设计可存储了其中任意多个值;而且出于效率考虑,这些值实际存储的是“数字”,因为这些选项的每个选项值依次对应如下数字:1,2,4,8,16,32,… 最多64个。

说明:不建议在添加枚举值,集合值的时候采用数字的方式,因为不利于阅读。

案例:
有一个调查表votes,需要调查人的喜好, (登山,游泳,篮球,武术)中选择(可以多选),性别(男,女)单选。

mysql> create table votes (
    -> name varchar(20),
    -> hobby set('登山','游泳','武术','篮球'),
    -> gender enum('男','女')
    -> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.02 sec)

mysql> desc votes;
+--------+------------------------------------------+------+-----+---------+-------+
| Field  | Type                                     | Null | Key | Default | Extra |
+--------+------------------------------------------+------+-----+---------+-------+
| name   | varchar(20)                              | YES  |     | NULL    |       |
| hobby  | set('登山','游泳','武术','篮球')         | YES  |     | NULL    |       |
| gender | enum('男','女')                          | YES  |     | NULL    |       |
+--------+------------------------------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

插入数据:

mysql> insert into votes (name, hobby, gender) values ('Jame', '游泳,武术', '男');
Query OK, 1 row affected (0.00 sec)

mysql> insert into votes (name, hobby, gender) values ('Lisa', '游泳,登山', '女');
Query OK, 1 row affected (0.00 sec)

mysql> insert into votes (name, hobby, gender) values ('Jack', '游泳,篮球,武术', '男');
Query OK, 1 row affected (0.01 sec)

mysql> insert into votes (name, hobby, gender) values ('Maria', '游泳', '女');
Query OK, 1 row affected (0.00 sec)

mysql> select * from votes;
+-------+----------------------+--------+
| name  | hobby                | gender |
+-------+----------------------+--------+
| Jame  | 游泳,武术            ||
| Lisa  | 登山,游泳            ||
| Jack  | 游泳,武术,篮球       ||
| Maria | 游泳                 ||
+-------+----------------------+--------+
4 rows in set (0.00 sec)

有如上数据,想查找所有喜欢游泳的人:

mysql> select * from votes where hobby='游泳';
+-------+--------+--------+
| name  | hobby  | gender |
+-------+--------+--------+
| Maria | 游泳   ||
+-------+--------+--------+
1 row in set (0.00 sec)

我们会发现我们只能查找到兴趣爱好只有游泳的人,不能查询到兴趣爱好中含有游泳的人。

集合查询使用find_ in_ set函数:
find_in_set(sub,str_list) :如果 sub 在 str_list 中,则返回下标;如果不在,返回0; str_list 用逗号分
隔的字符串:

示例:

mysql> select find_in_set('a', 'a,b,c');
+---------------------------+
| find_in_set('a', 'a,b,c') |
+---------------------------+
|                         1 |
+---------------------------+
1 row in set (0.00 sec)

mysql> select find_in_set('d', 'a,b,c');
+---------------------------+
| find_in_set('d', 'a,b,c') |
+---------------------------+
|                         0 |
+---------------------------+
1 row in set (0.00 sec)

在votes表中查询喜欢游泳的人:

mysql> select * from votes where find_in_set('游泳', hobby);
+-------+----------------------+--------+
| name  | hobby                | gender |
+-------+----------------------+--------+
| Jame  | 游泳,武术            ||
| Lisa  | 登山,游泳            ||
| Jack  | 游泳,武术,篮球       ||
| Maria | 游泳                 ||
+-------+----------------------+--------+
4 rows in set (0.00 sec)

你可能感兴趣的:(MySQL,mysql,数据库,开发语言)