MySQL数据类型
MySQL支持多种数据类型,主要有数值类型、日期时间类型和字符串类型。
目录:
- 整数类型
- 浮点数和定点数类型
- 日期与时间类型
- 文本字符串类型
- 二进制字符串类型
内容较多,长期更新。。。。
整数类型
整数类型表:
整数类型的取值范围:
例1:创建一个整数类型的表
MariaDB [lyshark]> create table myint -> ( -> uid int(10), -> name varchar(20) -> ); Query OK, 0 rows affected (0.00 sec)
以上是uid就是一个整数类型的字段,注意后面的(10)意思是指定能够显示的数值中数字的个数。
例2:分别创建整形的数据类型字段看看
MariaDB [lyshark]> create table temp -> ( -> a tinyint, -> b smallint, -> c mediumint, -> d int, -> e bigint -> ); Query OK, 0 rows affected (0.00 sec) MariaDB [lyshark]> desc temp; +-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | a | tinyint(4) | YES | | NULL | | | b | smallint(6) | YES | | NULL | | | c | mediumint(9) | YES | | NULL | | | d | int(11) | YES | | NULL | | | e | bigint(20) | YES | | NULL | | +-------+--------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
浮点数类型和定点数类型
小数类型表:
浮点数取值范围:
例1:创建temp1表,其中字段 x,y,z 数据类型分别是 float(5.1) double(5.1) decimal(5.1) 并向表中插入一些数据
创建表
MariaDB [lyshark]> create table temp1 -> ( -> x float(5,1), -> y double(5,1), -> z decimal(5,1) -> ); Query OK, 0 rows affected (0.00 sec)
向表中插入数据,并查看结果
MariaDB [lyshark]> insert into temp1 values(5.12,5.22,5.123); Query OK, 1 row affected, 1 warning (0.01 sec) MariaDB [lyshark]> select * from temp1; +------+------+------+ | x | y | z | +------+------+------+ | 5.1 | 5.2 | 5.1 | +------+------+------+ 1 row in set (0.00 sec) MariaDB [lyshark]>
上图可知,mysql默认自动截断小数点后面的数据,具体截断位数由计算机硬件和操作系统决定。
日期与时间类型
日期时间类型表:
YEAR类型
主要用于存储一个年份 例如:1997 2018
例1:创建temp数据表,定义数据类型为year的字段x,并向表中插入数据
MariaDB [lyshark]> create table temp(x year); #创建一个year类型的字段 Query OK, 0 rows affected (0.00 sec) MariaDB [lyshark]> desc temp; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | x | year(4) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.01 sec) MariaDB [lyshark]> insert into temp values(2018),("2020"); #插入一些数据:注意必须是1901-2155之间的数 Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 MariaDB [lyshark]> select * from temp; +------+ | x | +------+ | 2018 | | 2020 | +------+ 2 rows in set (0.00 sec)
TIME类型
主要用于存储时间 例如:12:12:21
例1:创建temp1数据表,定义数据类型为time的字段x,并向表中插入数据
MariaDB [lyshark]> create table temp1( #创建一个time类型的字段 -> x time -> ); Query OK, 0 rows affected (0.01 sec) MariaDB [lyshark]> desc temp1; +-------+------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------+------+-----+---------+-------+ | x | time | YES | | NULL | | +-------+------+------+-----+---------+-------+ 1 row in set (0.00 sec) MariaDB [lyshark]> insert into temp1 values('11:22:05'),('23:23'),('20'); #分别插入数据:注意(%HH-%MM-%SS) Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 MariaDB [lyshark]> select * from temp1; +----------+ | x | +----------+ | 11:22:05 | | 23:23:00 | | 00:00:20 | +----------+ 3 rows in set (0.00 sec)
例2:当然啦我们可以简写省略冒号
MariaDB [lyshark]> create table temp1(x time); Query OK, 0 rows affected (0.01 sec) MariaDB [lyshark]> insert into temp1 values('102231'); Query OK, 1 row affected (0.00 sec) MariaDB [lyshark]> select * from temp1; +----------+ | x | +----------+ | 10:22:31 | +----------+ 1 row in set (0.00 sec)
例3:向temp表中的x字段插入当前系统运行时间
MariaDB [lyshark]> select * from temp; Empty set (0.00 sec) MariaDB [lyshark]> insert into temp values (CURRENT_TIME),(NOW()); Query OK, 2 rows affected, 1 warning (0.00 sec) Records: 2 Duplicates: 0 Warnings: 1 MariaDB [lyshark]> select * from temp; +----------+ | x | +----------+ | 21:27:43 | | 21:27:43 | +----------+ 2 rows in set (0.00 sec)
DATE类型
Date类型主要用于存储年月日 例如:1997-10-05
例1:创建temp表,表中是date类型的x 字段,并插入一条数据
MariaDB [lyshark]> create table temp(x date); #创建一个date类型的字段 Query OK, 0 rows affected (0.00 sec) MariaDB [lyshark]> desc temp; +-------+------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+------+------+-----+---------+-------+ | x | date | YES | | NULL | | +-------+------+------+-----+---------+-------+ 1 row in set (0.00 sec) MariaDB [lyshark]> insert into temp values('1997-10-05'),('20180523'); #插入一些数据 Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 MariaDB [lyshark]> select * from temp; +------------+ | x | +------------+ | 1997-10-05 | | 2018-05-23 | +------------+ 2 rows in set (0.00 sec)
例2:向temp表中插入系统当前日期
MariaDB [lyshark]> select * from temp; +------------+ | x | +------------+ | 1997-10-05 | | 2018-05-23 | +------------+ 2 rows in set (0.00 sec) MariaDB [lyshark]> insert into temp values(CURRENT_DATE()),(NOW()); #取出系统当前日期并插入 Query OK, 2 rows affected, 1 warning (0.00 sec) Records: 2 Duplicates: 0 Warnings: 1 MariaDB [lyshark]> select * from temp; +------------+ | x | +------------+ | 1997-10-05 | | 2018-05-23 | | 2018-06-16 | | 2018-06-16 | +------------+ 4 rows in set (0.00 sec)
DATETIME类型
DateTime类型用于存储日期和时间 例如:2018-01-24 22:12:24
例1:创建temp表,dt字段类型为datetime,并插入一条数据
MariaDB [lyshark]> create table temp(dt datetime); Query OK, 0 rows affected (0.00 sec) MariaDB [lyshark]> desc temp; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | dt | datetime | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 1 row in set (0.00 sec) MariaDB [lyshark]> insert into temp values('1997-05-10 10:22:14'),('20180616220101'); #插入日期时间 Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 MariaDB [lyshark]> select * from temp; +---------------------+ | dt | +---------------------+ | 1997-05-10 10:22:14 | | 2018-06-16 22:01:01 | +---------------------+ 2 rows in set (0.00 sec)
例2:取系统当前日期并插入temp表的dt字段
MariaDB [lyshark]> select * from temp; +---------------------+ | dt | +---------------------+ | 1997-05-10 10:22:14 | | 2018-06-16 22:01:01 | +---------------------+ 2 rows in set (0.00 sec) MariaDB [lyshark]> insert into temp values(now()); #取系统日期插入temp表的dt字段 Query OK, 1 row affected (0.00 sec) MariaDB [lyshark]> select * from temp; +---------------------+ | dt | +---------------------+ | 1997-05-10 10:22:14 | | 2018-06-16 22:01:01 | | 2018-06-16 22:03:39 | +---------------------+ 3 rows in set (0.00 sec) MariaDB [lyshark]>
TIMESTAMP类型
TimeStamp与DateTime相同,但是TimeStamp是使用的UTC(世界标准时间)
例1:创建temp表并插入timestamp类型的x字段,插入一条数据
MariaDB [lyshark]> create table temp(x timestamp); #创建一个timestamp类型的字段 Query OK, 0 rows affected (0.00 sec) MariaDB [lyshark]> desc temp; +-------+-----------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------+------+-----+-------------------+-----------------------------+ | x | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | +-------+-----------+------+-----+-------------------+-----------------------------+ 1 row in set (0.00 sec) MariaDB [lyshark]> insert into temp values('2018-06-16 22:24:00'); #插入一条时间记录 Query OK, 1 row affected (0.01 sec) MariaDB [lyshark]> select *from temp; +---------------------+ | x | +---------------------+ | 2018-06-16 22:24:00 | +---------------------+ 1 row in set (0.00 sec) MariaDB [lyshark]> set time_zone='+12:00' #将时间上调12小时 -> ; Query OK, 0 rows affected (0.00 sec) MariaDB [lyshark]> select * from temp; #再次查询已经是第二天了 +---------------------+ | x | +---------------------+ | 2018-06-17 02:24:00 | +---------------------+ 1 row in set (0.00 sec)