MySQL---数据类型

文章目录

  • 前言
  • 一、数据类型分类
  • 二、数值类型
    • 1.tinyint类型
    • 2.bit类型
  • 三、小数类型
    • 1.float
    • 2. decimal
  • 三、字符串类型
    • 1.char
    • 2.varchar
    • 3.char和varchar比较
    • 四、日期和时间类型
  • 五、enum和set
      • 我们如何找到性别是男或者女呢?
      • 我们如何找到爱好有rapper呢?
  • 总结


前言


正文开始!!!

一、数据类型分类

MySQL---数据类型_第1张图片

二、数值类型

MySQL---数据类型_第2张图片

1.tinyint类型

数值越界测试:

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

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

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

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

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

mysql在这里,如果插入的数据不符合要求,直接终止操作!
数据类型在mysql这里符合条件让你操作,不符合条件,不让你操作!—约束
可以保证数据库的数据是预期的!

mysql中数据类型本质就是一直约束!!!

MySQL---数据类型_第3张图片

说明:

  • 在MySQL中,整形可以指定是有符号和无符号的,默认是有符号的.
  • 可以通过unsigned;来说明某个字段是无符号的.
  • 无符号案例
mysql> create table t2(
    -> num tinyint unsigned
    -> );
Query OK, 0 rows affected (0.01 sec)

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

mysql> insert into t2 values (-1);
ERROR 1264 (22003): Out of range value for column 'num' at row 1
mysql> insert into t2 values (255);
Query OK, 1 row affected (0.00 sec)

MySQL---数据类型_第4张图片

  • 其他类型自己推导

注意:尽量不适用unsigned,对于int类型可能存不下的数据,int unsigned同样可能存放不下.与其如此,还不如设计时,将int类型提升为bigint类型.

2.bit类型

基本语法

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

举例:

mysql> create table t3(
    -> id int,
    -> a bit(8)
    -> );
Query OK, 0 rows affected (0.02 sec)

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

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

mysql> select * from t3; ---发现很怪异的现象,a的数据10没有出现
+------+------+
| id   | a    |
+------+------+
|   10 |      |
+------+------+
1 row in set (0.00 sec)
  • bit字段在显示时,是按照ASCII码对应的值显示.
mysql> insert into t3 value (65,65);
Query OK, 1 row affected (0.01 sec)

mysql> select * from t3;
+------+------+
| id   | a    |
+------+------+
|   10 | 
    |
|   65 | A    |
+------+------+
2 rows in set (0.00 sec)
  • 如果我们有这样的值,只存放0或1,这时可以定义bit(1).这样可以节省空间.
mysql> create table t4(
    -> name varchar(32),
    -> sex bit(1)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t4 (name,sex) value ('张三',1);
Query OK, 1 row affected (0.01 sec)

mysql> insert into t4 (name,sex) value ('李四',0);
Query OK, 1 row affected (0.01 sec)

mysql> select * from t4;
+--------+------+
| name   | sex  |
+--------+------+
| 张三   |     |
| 李四   |      |
+--------+------+
2 rows in set (0.00 sec)

mysql> select * from t4 where sex=0;
+--------+------+
| name   | sex  |
+--------+------+
| 李四   |      |
+--------+------+
1 row in set (0.00 sec)

mysql> select * from t4 where sex=1;
+--------+------+
| name   | sex  |
+--------+------+
| 张三   |     |
+--------+------+
1 row in set (0.00 sec)

三、小数类型

1.float

语法:

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

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

mysql> create table t5( id int, salary float(4,2) );
Query OK, 0 rows affected (0.01 sec)

mysql> desc t5;
+--------+------------+------+-----+---------+-------+
| Field  | Type       | Null | Key | Default | Extra |
+--------+------------+------+-----+---------+-------+
| id     | int(11)    | YES  |     | NULL    |       |
| salary | float(4,2) | YES  |     | NULL    |       |
+--------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

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

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

mysql> insert into t5 values (1,99.999);
ERROR 1264 (22003): Out of range value for column 'salary' at row 1
mysql> insert into t5 values (1,999.99);
ERROR 1264 (22003): Out of range value for column 'salary' at row 1
mysql> insert into t5 values (1,1.01);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t5 values (1,99.993); #多的这一点被拿掉了
Query OK, 1 row affected (0.01 sec)

MySQL---数据类型_第5张图片

问题:当我们的float(4,2)如果是一个有符号的,则表示的范围是-99.99~99.99,如果float(6,3),那么范围是多少呢?
(-999.999~999.999)

案例:

mysql> create table t6(id int, salary float(4,2) unsigned);
Query OK, 0 rows affected (0.02 sec)

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

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

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

MySQL---数据类型_第6张图片

2. decimal

语法:

decimal(m, d) [unsigned] : 定点数m指定长度,d表示小数点的位数
mysql> create table tt8 ( id int, salary float(10,8), sa
  • decimal(5,2)表示的范围是-999.99~999.99
  • decimal(5,2) unsigned 表示的范围是0~999.99

decimal和float很像,但是有区别:float和decimal表示的精度不一样!!!

mysql> create table t7(
    -> id int,
    -> salary1 float(10,8),
    -> salary2 decimal(10,8)
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> desc t7;
+---------+---------------+------+-----+---------+-------+
| Field   | Type          | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| id      | int(11)       | YES  |     | NULL    |       |
| salary1 | float(10,8)   | YES  |     | NULL    |       |
| salary2 | decimal(10,8) | YES  |     | NULL    |       |
+---------+---------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

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

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

mysql> select * from t7;
+------+-------------+-------------+
| id   | salary1     | salary2     |
+------+-------------+-------------+
|    1 | 23.12345505 | 23.12345600 |   #我们可以发现decimal的精度更准确,因此如果我们希望某个数据表示高精度,选择decimal
|    1 | 23.12345695 | 23.12345612 |
+------+-------------+-------------+
2 rows in set (0.00 sec)

说明:float表示的精度大约是7位.

  • decimal整数最大位数m为65.支持小数最大位数d是30.如果d被省略,默认为0.如果m被省略,默认是10.

建议:如果希望小数的精度高,推荐使用decimal.

三、字符串类型

1.char

语法:

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

案例(char):

mysql> create table t8(
    -> id int,
    -> name char(2)
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> desc t8;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
| name  | char(2) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)

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

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

mysql> insert into t8 values (1,'abc');    #长度超过3个字符,不能被插入
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> insert into t8 values (1,'詹姆斯');  #长度超过3个字符,不能被插入
ERROR 1406 (22001): Data too long for column 'name' at row 1

mysql> select * from t8;
+------+--------+
| id   | name   |
+------+--------+
|    1 | ab     |
|    1 | 罗斯   |
+------+--------+
2 rows in set (0.00 sec)

mysql中的字符,指的就是一个字符,可以一个abcd1234这样的,也可以是一个汉字.

char类型最多可以存放255个字符!!!

mysql> create table t9(
    -> name char(255)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> create table t10(
    -> name char(256)
    -> );
ERROR 1074 (42000): Column length too big for column 'name' (max = 255); use BLOB or TEXT instead

2.varchar

语法:

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

案例:

mysql> create table t11(
    -> id int,
    -> name varchar(6)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> desc t11;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id    | int(11)    | YES  |     | NULL    |       |
| name  | varchar(6) | YES  |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into t11 values (1,'a');
Query OK, 1 row affected (0.00 sec)

mysql> insert into t11 values (1,'hulu');
Query OK, 1 row affected (0.00 sec)

mysql> insert into t11 values (1,'abcdef');
Query OK, 1 row affected (0.00 sec)

mysql> insert into t11 values (1,'abcdefg');
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> select * from t11;
+------+--------+
| id   | name   |
+------+--------+
|    1 | a      |
|    1 | hulu   |
|    1 | abcdef |
+------+--------+
3 rows in set (0.00 sec)

说明:
关于varchar(len),len到底是多大,这个len值,和表的编码密切相关;

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

一个字符串长度有上限和它是变长的不冲突.

mysql> create table t12(
    -> name varchar(21844)
    -> )charset=utf8;
Query OK, 0 rows affected (0.01 sec)

mysql> create table t13(
    -> name varchar(21845)
    -> )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
# 验证了utf8确实不能超过21844

3.char和varchar比较

MySQL---数据类型_第7张图片
如何选择定长或变长字符串?

  • 如果数据确定长度都一样,就使用定长(char),比如身份证,手机号,MD5
  • 如果数据长度有变化,就是用变长(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 birthday(
    -> t1 date,
    -> t2 datetime,
    -> t3 timestamp
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> desc birthday;
+-------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type      | Null | Key | Default           | Extra                       |
+-------+-----------+------+-----+-------------------+-----------------------------+
| t1    | date      | YES  |     | NULL              |                             |
| t2    | datetime  | YES  |     | NULL              |                             |
| t3    | timestamp | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+-------------------+-----------------------------+
3 rows in set (0.00 sec)

mysql> insert into birthday (t1,t2) values ('2002-09-23','2023-04-12 10:00:00');
Query OK, 1 row affected (0.00 sec)

mysql> insert into birthday (t1,t2) values ('2002-09-23','2000-10-01 10:00:00');
Query OK, 1 row affected (0.00 sec)

mysql> select * from birthday;
+------------+---------------------+---------------------+
| t1         | t2                  | t3                  |
+------------+---------------------+---------------------+
| 2002-09-23 | 2023-04-12 10:00:00 | 2023-04-12 21:25:10 |    # 添加数据时,时间戳自动补上当前时间
| 2002-09-23 | 2000-10-01 10:00:00 | 2023-04-12 21:25:35 |
+------------+---------------------+---------------------+
2 rows in set (0.00 sec)


mysql> update birthday set t1='1949-10-01';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> select * from birthday;
+------------+---------------------+---------------------+
| t1         | t2                  | t3                  |
+------------+---------------------+---------------------+
| 1949-10-01 | 2023-04-12 10:00:00 | 2023-04-12 21:27:31 |   #  更新数据,时间戳会更新成当前时间
| 1949-10-01 | 2000-10-01 10:00:00 | 2023-04-12 21:27:31 |
+------------+---------------------+---------------------+

五、enum和set

语法:

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

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

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

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

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

案例:

mysql> create table votes(
    -> name varchar(20),
    -> gender enum('男','女'),
    -> hobby set('唱','跳','rapper','篮球','足球')
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> desc votes;
+--------+---------------------------------------------+------+-----+---------+-------+
| Field  | Type                                        | Null | Key | Default | Extra |
+--------+---------------------------------------------+------+-----+---------+-------+
| name   | varchar(20)                                 | YES  |     | NULL    |       |
| gender | enum('男','女')                             | YES  |     | NULL    |       |
| hobby  | set('唱','跳','rapper','篮球','足球')       | YES  |     | NULL    |       |
+--------+---------------------------------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> insert into votes values ('蔡徐坤','男','唱,跳,rapper');
Query OK, 1 row affected (0.00 sec)

mysql> insert into votes values ('张三','男','rapper');
Query OK, 1 row affected (0.00 sec)

mysql> insert into votes values ('李四','女','rapper,足球');
Query OK, 1 row affected (0.00 sec)

mysql> insert into votes values ('迪丽热巴','女','唱,跳');
Query OK, 1 row affected (0.00 sec)

mysql> insert into votes values ('乃万','女','唱,rapper');
Query OK, 1 row affected (0.01 sec)

mysql> select * from votes;
+--------------+--------+----------------+
| name         | gender | hobby          |
+--------------+--------+----------------+
| 蔡徐坤       ||,,rapper   |
| 张三         || rapper         |
| 李四         || rapper,足球    |
| 迪丽热巴     ||,|
| 乃万         ||,rapper      |
+--------------+--------+----------------+
5 rows in set (0.00 sec)

# 测试enum类型

mysql> insert into votes values ('罗斯',1,'篮球');
Query OK, 1 row affected (0.01 sec)

mysql> insert into votes values ('杨舒予',2,'篮球');
Query OK, 1 row affected (0.00 sec)

mysql> insert into votes values ('杨舒予',3,'篮球'); # 3就不能被插入
ERROR 1265 (01000): Data truncated for column 'gender' at row 1

mysql> select * from votes;
+--------------+--------+----------------+
| name         | gender | hobby          |
+--------------+--------+----------------+
| 蔡徐坤       ||,,rapper   |
| 张三         || rapper         |
| 李四         || rapper,足球    |
| 迪丽热巴     ||,|
| 乃万         ||,rapper      |
| 罗斯         || 篮球           |
| 杨舒予       || 篮球           |
+--------------+--------+----------------+
7 rows in set (0.00 sec)

# 测试set类型

mysql> insert into votes values ('赵六',1,1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into votes values ('赵六',1,2);
Query OK, 1 row affected (0.00 sec)

mysql> insert into votes values ('赵六',1,3);
Query OK, 1 row affected (0.01 sec)

mysql> insert into votes values ('赵六',1,4);
Query OK, 1 row affected (0.00 sec)

mysql> insert into votes values ('赵六',1,5);
Query OK, 1 row affected (0.00 sec)

mysql> insert into votes values ('赵六',1,8);
Query OK, 1 row affected (0.01 sec)

mysql> select * from votes;
+--------------+--------+----------------+
| name         | gender | hobby          |
+--------------+--------+----------------+
| 蔡徐坤       ||,,rapper   |
| 张三         || rapper         |
| 李四         || rapper,足球    |
| 迪丽热巴     ||,|
| 乃万         ||,rapper      |
| 罗斯         || 篮球           |
| 杨舒予       || 篮球           |
| 赵六         |||
| 赵六         |||
| 赵六         ||,|
| 赵六         || rapper         |
| 赵六         ||,rapper      |
| 赵六         || 篮球           |
+--------------+--------+----------------+
13 rows in set (0.00 sec)

MySQL---数据类型_第8张图片

set类型中采用位图形式,比如set选项中对应00000,如果是1对应位图就是00001代表的就是唱
如果是3,对应位图就是00011,代表的就是唱,跳…
依次类推即可!!!

31对应位图就是11111.
MySQL---数据类型_第9张图片

我们如何找到性别是男或者女呢?

mysql> select * from votes where gender='男';
+-----------+--------+------------------------------+
| name      | gender | hobby                        |
+-----------+--------+------------------------------+
| 蔡徐坤    ||,,rapper                 |
| 张三      || rapper                       |
| 罗斯      || 篮球                         |
| 赵六      |||
| 赵六      |||
| 赵六      ||,|
| 赵六      || rapper                       |
| 赵六      ||,rapper                    |
| 赵六      || 篮球                         |
| 赵六      ||,,rapper,篮球,足球       |
+-----------+--------+------------------------------+
10 rows in set (0.00 sec)

mysql> select * from votes where gender='女';
+--------------+--------+---------------+
| name         | gender | hobby         |
+--------------+--------+---------------+
| 李四         || rapper,足球   |
| 迪丽热巴     ||,|
| 乃万         ||,rapper     |
| 杨舒予       || 篮球          |
+--------------+--------+---------------+
4 rows in set (0.00 sec)

mysql> select * from votes where gender=1;
+-----------+--------+------------------------------+
| name      | gender | hobby                        |
+-----------+--------+------------------------------+
| 蔡徐坤    ||,,rapper                 |
| 张三      || rapper                       |
| 罗斯      || 篮球                         |
| 赵六      |||
| 赵六      |||
| 赵六      ||,|
| 赵六      || rapper                       |
| 赵六      ||,rapper                    |
| 赵六      || 篮球                         |
| 赵六      ||,,rapper,篮球,足球       |
+-----------+--------+------------------------------+
10 rows in set (0.00 sec)

mysql> select * from votes where gender=2;
+--------------+--------+---------------+
| name         | gender | hobby         |
+--------------+--------+---------------+
| 李四         || rapper,足球   |
| 迪丽热巴     ||,|
| 乃万         ||,rapper     |
| 杨舒予       || 篮球          |
+--------------+--------+---------------+
4 rows in set (0.00 sec)

我们如何找到爱好有rapper呢?

mysql> select * from votes;
+--------------+--------+------------------------------+
| name         | gender | hobby                        |
+--------------+--------+------------------------------+
| 蔡徐坤       ||,,rapper                 |
| 张三         || rapper                       |
| 李四         || rapper,足球                  |
| 迪丽热巴     ||,|
| 乃万         ||,rapper                    |
| 罗斯         || 篮球                         |
| 杨舒予       || 篮球                         |
| 赵六         |||
| 赵六         |||
| 赵六         ||,|
| 赵六         || rapper                       |
| 赵六         ||,rapper                    |
| 赵六         || 篮球                         |
| 赵六         ||,,rapper,篮球,足球       |
+--------------+--------+------------------------------+
14 rows in set (0.00 sec)

mysql> select * from votes where hobby='rapper';
+--------+--------+--------+
| name   | gender | hobby  |
+--------+--------+--------+
| 张三   || rapper |
| 赵六   || rapper |
+--------+--------+--------+
2 rows in set (0.00 sec)

我们想找到爱好有rapper的,而不是只有rapper的该怎么办呢?

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

mysql> select * from votes where find_in_set('rapper',hobby);
+-----------+--------+------------------------------+
| name      | gender | hobby                        |
+-----------+--------+------------------------------+
| 蔡徐坤    ||,,rapper                 |
| 张三      || rapper                       |
| 李四      || rapper,足球                  |
| 乃万      ||,rapper                    |
| 赵六      || rapper                       |
| 赵六      ||,rapper                    |
| 赵六      ||,,rapper,篮球,足球       |
+-----------+--------+------------------------------+
7 rows in set (0.00 sec)

总结

(本章完!)

你可能感兴趣的:(详解MySQL,mysql,数据库,服务器)