MySql数据类型
数值类型
整数类型
tinyint
smallint
mediumint
int
bigint
MySQL数据类型 | 含义(有符号) |
---|---|
tinyint(m) | 1个字节 范围(-128~127) |
smallint(m) | 2个字节 范围(-32768~32767) |
mediumint(m) | 3个字节 范围(-8388608~8388607) |
int(m) | 4个字节 范围(-2147483648~2147483647) |
bigint(m) | 8个字节 范围(±9.22*10的18次方) |
取值范围如果加了unsigned,则最大值翻倍,如tinyint unsigned的取值范围为(0~256)。
int(m)里的m是表示SELECT查询结果集中的显示宽度,并不影响实际的取值范围,没有影响到显示的宽度,不知道这个m有什么用。
默认宽度是int(11)
一般配合zerofill使用,zerofill就是用“0”,填充的意思.
整数类型数据的属性
可选属性unsigned(无符号类型):取值下限是0,取值上限在原来基础上翻倍.
例如:tinyint 有符号范围是:-128-127,无符号范围是0-255,
指定zerofii的类型,mysql会自动为其添加unsigned属性
可选属性:auto increment:此属性只是用于整数类型而且自动产生一个唯一的标识.
对一个想要使用auto increment属性的数据,必须设置为not null并且需要定义primary key或者unique
语法:
create table AI(ID int auto_increment not null primary key);
create table AI(ID int auto_increment not null ,primary key(ID));
create table AI(ID int auto_increment not null ,unique key(ID));
小数
MySQL分为两种标示小数的方法:浮点数,定点数.
浮点型在数据库中存放的是近似值,而定点类型在数据库中存放的是精确值。
浮点数
float(单精度)
double(双精度)
设一个字段定义为float(5,3),如果插入一个数123.45678,实际数据库里存的是123.457,但总个数还以实际为准,即6位
定点数
decimal(定点数):在MySQL中以字符串的形式存放,比浮点数更精确,适合用来标示货币等精度高的数据
decimal(m,d) 参数m<65 是总个数,d<30且 d<m 是小数位。
MySQL数据类型 | 含义 |
---|---|
float(m,d) | 单精度浮点型 8位精度(4字节) m总个数,d小数位 |
double(m,d) | 双精度浮点型 16位精度(8字节) m总个数,d小数位 |
注意:浮点数如果不写精度和标度,那么默认会按照实际存入的小数,去存
但是定点数如果不写精度和标度,那么会按照默认的decimal(10,0)来操作,并且如果数据超过了精度和标度值,系统会报错
位类型
bit
语法:create table t2(id bit(1));
select bin(id),hex(id) from t3;
±--------±--------+
| bin(id) | hex(id) |
±--------±--------+
| 1 | 1 |
±--------±--------+
1 row in set (0.00 sec)
日期时间类
MySQL数据类型 | 含义 | 字节 | 最大值 | 最小值 |
---|---|---|---|---|
date | 日期 ‘2008-12-2’ | 4 | 9999-12-3 | 1000-01-01 |
time | 时间 ‘12:25:36’ | 3 | 838:59:59 | -838:59:59 |
datetime | 日期时间 ‘2008-12-2 22:06:44’ | 8 | 9999-12-31 23:59:59 | 1000-01-01 00:00:00 |
timestamp | 自动存储记录修改时间 | 4 | 2038年某个时刻 | |
year | 1 | 2155 | 1901 | |
代码:
展示date,time,datetime三种表示方式的不同
添加当前时间可以使用now()函数
MariaDB [test1]> create table t(d date,t time,dt datetime);
Query OK, 0 rows affected (0.01 sec)
MariaDB [test1]> desc t
-> ;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| d | date | YES | | NULL | |
| t | time | YES | | NULL | |
| dt | datetime | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)
MariaDB [test1]> insert into t values(now(),now(),now());
Query OK, 1 row affected, 2 warnings (0.01 sec)
MariaDB [test1]> select * from t;
+------------+----------+---------------------+
| d | t | dt |
+------------+----------+---------------------+
| 2020-10-09 | 11:25:08 | 2020-10-09 11:25:08 |
+------------+----------+---------------------+
1 row in set (0.00 sec)
timestamp
注意:
- 系统会自动给timestamp创建默认值CURRENT_TIMESTAMP(系统日期),
- 但是只会给表中的第一个timestamp字段设置默认值为系统日期.如果有第二个,timestamp类型,那么设置默认值为0
- 系统规定一列只能有一个默认值为CURRENT_TIMESTAMP(系统日期)的数据,所以不能将其他数据的默认值改为CURRENT_TIMESTAMP(系统日期)
- timestamp和时区相关
MariaDB [test1]> create table t_time(id1 timestamp);
Query OK, 0 rows affected (0.01 sec)
MariaDB [test1]> desc t_time;
+-------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+-------------------+-----------------------------+
| id1 | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+-------------------+-----------------------------+
1 row in set (0.00 sec)
MariaDB [test1]> insert into t values(null);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
MariaDB [test1]> insert into t_time values(null);
Query OK, 1 row affected (0.01 sec)
MariaDB [test1]> select * from t_time;
+---------------------+
| id1 |
+---------------------+
| 2020-10-09 11:27:23 |
+---------------------+
1 row in set (0.00 sec)
MariaDB [test1]> alter table t_time add id2 timestamp;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [test1]> select * from t_time;
+---------------------+---------------------+
| id1 | id2 |
+---------------------+---------------------+
| 2020-10-09 11:27:23 | 0000-00-00 00:00:00 |
+---------------------+---------------------+
1 row in set (0.00 sec)
MariaDB [test1]> show create table t_time \G;
*************************** 1. row ***************************
Table: t_time
Create Table: CREATE TABLE `t_time` (
`id1` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`id2` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)
ERROR: No query specified
注意:timestamp于时区相关
#查看时区
MariaDB [test1]> show variables like 'time_zone';
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| time_zone | SYSTEM |
+---------------+--------+
1 row in set (0.00 sec
更改时区
#讲时区改为东九区.
MariaDB [test1]> set time_zone='+9:00';
Query OK, 0 rows affected (0.00 sec)
MariaDB [test1]> select * from t8;
+---------------------+---------------------+
| id1 | id2 |
+---------------------+---------------------+
| 2020-10-09 14:33:37 | 2020-10-09 13:33:37 |
+---------------------+---------------------+
1 row in set (0.00 sec)
提示:TIMESTAMP 和 DATATIME 除了存储字节和支持的范围不同外,还有一个最大的区别就是:DATETIME 在存储日期数据时,按实际输入的格式存储,即输入什么就存储什么,与时区无关;而 TIMESTAMP 值的存储是以 UTC (世界标准时间)格式保存的,存储时对当前时区进行转换,检索时再转换回当前时区。即查询时,根据当前时区的不同,显示的时间值是不同的。
字符串类型
MySQL数据类型 | 含义 |
---|---|
char(n) | 固定长度,最多255个字符 |
varchar(n) | 固定长度,最多65535个字符 |
tinytext | 可变长度,最多255个字符 |
text | 可变长度,最多65535个字符 |
mediumtext | 可变长度,最多2的24次方-1个字符 |
longtext | 可变长度,最多2的32次方-1个字符 |
char和varchar的区别
相同:
1. 都用来保存MySQL中较短的字符串
不同:
- char(n) 若存入字符数小于n,则以空格补于其后,查询之时再将空格去掉。所以char类型存储的字符串末尾不能有空格,varchar不限于此。
- char(n) 固定长度,char(4)不管是存入几个字符,都将占用4个字节,varchar是存入的实际字符数+1个字节(n<=255)或2个字节(n>255),
- char类型的字符串检索速度要比varchar类型的快。
MariaDB [test1]> create table vc(v varchar(4),c char(4));
Query OK, 0 rows affected (0.01 sec)
MariaDB [test1]> insert into vc values('ab ','ab ');
Query OK, 1 row affected, 1 warning (0.01 sec)
MariaDB [test1]> select * from vc;
+------+------+
| v | c |
+------+------+
| ab | ab |
+------+------+
1 row in set (0.00 sec)
MariaDB [test1]> select length(v),length(c) from vc;
+-----------+-----------+
| length(v) | length(c) |
+-----------+-----------+
| 4 | 2 |
+-----------+-----------+
1 row in set (0.00 sec)
MariaDB [test1]> select concat(v,"xys"),concat(c,"xys") from vc;
+-----------------+-----------------+
| concat(v,"xys") | concat(c,"xys") |
+-----------------+-----------------+
| ab xys | abxys |
+-----------------+-----------------+
1 row in set (0.00 sec)
binary和varbinary
binary和varbinary类似于char和varchar,不同的是他们包含二进制字符串二不包含非二进制字符串.
MariaDB [test1]> create table t4(c binary(3));
Query OK, 0 rows affected (0.01 sec)
MariaDB [test1]> insert into t4 set c = 'a';
Query OK, 1 row affected (0.00 sec)
MariaDB [test1]> select * ,hex(c),c= 'a' ,c = 'a\0' ,c='a\0\0' from t4;
+------+--------+--------+-----------+-----------+
| c | hex(c) | c= 'a' | c = 'a\0' | c='a\0\0' |
+------+--------+--------+-----------+-----------+
| a | 610000 | 0 | 0 | 1 |
+------+--------+--------+-----------+-----------+
1 row in set (0.00 sec)
enum枚举类型
特点:
- 存储时不区分大小写,显示时默认全部转换为大写
- 对于一个不存在的值,没有错误警告,而是存储enum(‘M’,“F”)中的第一个值M