MySQL

15-MySQL数据类型

2021-05-20  本文已影响0人  紫荆秋雪_文

一、数值类型

数值类型包括:整数类型、浮点数类型、定点数类型

1、整数类型

整数类型.png
整数类型表示的数值范围.png

2、设置字段的显示宽度

create table t1(id int(3));
insert into t1(id) values (1), (123456);
select * from t1;
+--------+
| id     |
+--------+
|      1 |
| 123456 |
+--------+
mysql> insert into t1(id) values (1), (123456), (123456789987654321);
ERROR 1264 (22003): Out of range value for column 'id' at row 3

3、整数类型的显示宽度配合 zerofill 使用,zerofill表示在数字的显示位数不够时,可以用字符 0 进行填充

create table t2(
  id1 int zerofill,
  id2 int(3) zerofill
);
insert into t2(id1, id2) values(1, 2);
select * from t2;
+------------+------+
| id1        | id2  |
+------------+------+
| 0000000001 |  002 |
+------------+------+
mysql> show create table t2 \G
*************************** 1. row ***************************
       Table: t2
Create Table: CREATE TABLE `t2` (
  `id1` int(10) unsigned zerofill DEFAULT NULL,
  `id2` int(3) unsigned zerofill DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
create table if not exists t3 ( 
  id int not null auto_increment primary key, 
  age int 
);
insert into t3 (age) values (18), (20), (16);
create table if not exists t4 ( id int not null auto_increment unique, age int );
insert into t4 (age) values (5), (7), (9);

4、浮点数类型

浮点数类型主要有两种:单精度浮点数 FLOAT 和 双精度浮点数 DOUBLE 浮点数所需的存储空间.png
浮点数表示的数值范围.png
create table if not exists t6 (f float, d double);
insert into t6 (f, d) values (3.14, 5.98);
insert into t6 (f, d) values (3.144444444444444, 5.98999999999999999999999);
mysql> select * from t6;
+---------+------+
| f       | d    |
+---------+------+
|    3.14 | 5.98 |
| 3.14444 | 5.99 |
+---------+------+
create table if not exists t7 (
  f float(5, 2), 
  d double(5, 2)
);
insert into t7 (f, d) values (3.14, 3.14);
insert into t7 (f, d) values (3.141, 3.141);
mysql> select * from t7;
+------+------+
| f    | d    |
+------+------+
| 3.14 | 3.14 |
| 3.14 | 3.14 |
+------+------+

5、定点数类型-DECIMAL

create table if not exists t8 (   d1 decimal,    d2 decimal(5, 2) );
mysql> show create table t8 \G
*************************** 1. row ***************************
       Table: t8
Create Table: CREATE TABLE `t8` (
  `d1` decimal(10,0) DEFAULT NULL,
  `d2` decimal(5,2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
insert into t8 (d1, d2) values (3.14, 3.14);
mysql> select * from t8;
+------+------+
| d1   | d2   |
+------+------+
|    3 | 3.14 |
+------+------+
insert into t8 (d1, d2) values (3.14, 3.149);
mysql> select * from t8;
+------+------+
| d1   | d2   |
+------+------+
|    3 | 3.14 |
|    3 | 3.15 |
+------+------+

二、日期和时间类型

MySQL 提供了表示日期和时间的数据类型,主要有 YEAR类型、TIME类型、DATE类型、DATETIMEL类型和TIMESTAMP类型

日期和时间类型表示的范围.png

1、YEAR类型

在MySQL中,YEAR有以下几种存储格式

create table if not exists t9 (y year);
insert into t9 (y) values (2020), ('2020');
delete from t9;
insert into t9 (y) values (0), (00), (88), (20);
mysql> select * from t9;
+------+
| y    |
+------+
| 0000 |
| 0000 |
| 1988 |
| 2020 |
+------+
insert into t9 (y) values ('0'), ('00'), ('88'), ('20');
mysql> select * from t9;
+------+
| y    |
+------+
| 2000 |
| 2000 |
| 1988 |
| 2020 |
+------+

2、TIME类型

在MySQL中,向TIME类型的字段插入数据时,也可以使用几种不同的格式

create table t10 (t time);
insert into t10 (t) values ('2 12:30:29'), ('12:30:29'), ('12:40'), ('2 12:40'), ('45');
mysql> select * from t10;
+----------+
| t        |
+----------+
| 60:30:29 |
| 12:30:29 |
| 12:40:00 |
| 60:40:00 |
| 00:00:45 |
+----------+
insert into t10 (t) values ('123520'), ('124011'), ('0');
mysql> select * from t10;
+----------+
| t        |
+----------+
| 12:35:20 |
| 12:40:11 |
| 00:00:00 |
+----------+
insert into t10 (t) values ('12352');
mysql> select * from t10;
+----------+
| t        |
+----------+
| 12:35:20 |
| 12:40:11 |
| 00:00:00 |
| 01:23:52 |
+----------+
delete from t10;
insert into t10 (t) values (now()), (current_time);
mysql> select * from t10;
+----------+
| t        |
+----------+
| 02:45:32 |
| 02:45:32 |
+----------+

3、DATE类型

向DATE类型的字段插入数据时,同样需要满足一定的格式条件

create table t11 (d date);
insert into t11 (d) values ('2020-5-20'), ('20200520');
mysql> select * from t11;
+------------+
| d          |
+------------+
| 2020-05-20 |
| 2020-05-20 |
+------------+

4、DATETIME类型

向DATETIME类型的字段插入数据时,同样需要满足一定的格式条件

create table t12 (dt datetime);
insert into t12 (dt) values ('2021-05-20 11:08:08');
insert into t12 (dt) values ('20210520110808');
mysql> select * from t12;
+---------------------+
| dt                  |
+---------------------+
| 2021-05-20 11:08:08 |
| 2021-05-20 11:08:08 |
+---------------------+
insert into t12 (dt) values ('21-05-20 11:08:08');
insert into t12 (dt) values ('210520110808');
insert into t12 (dt) values (20210520110808);
insert into t12 (dt) values (210520110808);
mysql> select * from t12;
+---------------------+
| dt                  |
+---------------------+
| 2021-05-20 11:08:08 |
| 2021-05-20 11:08:08 |
+---------------------+
insert into t12 (dt) values (current_timestamp);
insert into t12 (dt) values (now());
mysql> select * from t12;
+---------------------+
| dt                  |
+---------------------+
| 2021-05-20 03:17:59 |
| 2021-05-20 03:18:23 |
+---------------------+

5、TIMESTAMP 类型

三、文本字符串类型

在MySQL中,字符串类型可以存储文本字符串数据,也可以存储一些图片、音频和视频数据,也就是二进制数据。因此在MySQL中,字符串类型可以分为文本字符串类型和二进制字符串类型。

1、文本字符串类型

文本字符串类型所占用存储空间.png

2、CHAR 与 VARCHAR 类型

create table t13(vc varchar(4), c char(4));
insert into t13 (vc, c) values ('abc', 'abc');
select length(vc), length(c) from t13;
insert into t13 (vc, c) values ('a  ', 'a  ');
mysql> select * from t13;
+------+------+
| vc   | c    |
+------+------+
| a    | a    |
+------+------+
mysql> select length(vc), length(c) from t13;
+------------+-----------+
| length(vc) | length(c) |
+------------+-----------+
|          3 |         1 |
+------------+-----------+
mysql> select concat(vc, 'b'), concat(c, 'b') from t13;
+-----------------+----------------+
| concat(vc, 'b') | concat(c, 'b') |
+-----------------+----------------+
| a  b            | ab             |
+-----------------+----------------+

3、TEXT类型

在MySQL中,Text用来保存文件类型的字符串,共包含4种类型

4、ENUM类型——枚举类型

create table t14 (e enum('A', 'B', 'C'));
insert into t14 (e) values ('a'), ('c');
mysql> select * from t14;
+------+
| e    |
+------+
| A    |
| C    |
+------+
insert into t14 (e) values ('1'), ('2');
mysql> select * from t14;
+------+
| e    |
+------+
| A    |
| B    |
+------+

5、SET类型

SET 表示一个字符串对象,可以包含 0 个或多个成员,但成员个数的上限为64. image.png

6、JSON类型

在MySQL5.7中支持JSON数据类型。在MySQL8.x中,JSON类型提供了可以进行自动验证的JSON文档和优化的存储结构,使得在MySQL中存储和读取JSON类型的数据更加方便和高效

create table t16 (j JSON);
insert into t16 (j) values ('{"name":"Raven", "age":18, "address":{"city":"nmg"}}');

mysql> select * from t16;
+----------------------------------------------------------+
| j                                                        |
+----------------------------------------------------------+
| {"age": 18, "name": "Raven", "address": {"city": "nmg"}} |
+----------------------------------------------------------+
mysql> select j->'$.name' from t16;
+-------------+
| j->'$.name' |
+-------------+
| "Raven"     |
+-------------+

四、二进制字符串类型

MySQL中的二进制字符串类型主要存储一些二进制数据,比如存储图片、音频和视频等二进制数据 二进制字符串类型长度与占用空间.png
上一篇 下一篇

猜你喜欢

热点阅读