本篇文章MySQL版本:
+-----------+
| version() |
+-----------+
| 5.5.53 |
+-----------+
目錄
日期和時間類型
- MySQL中提供了多種用於存儲日期和時間的類型;
- 一般根據需要進行選擇,並注意格式。
YEAR(M)
M 可以指定2,或者4
4位數年份存儲1901-2155;
2位數年份存儲1970-2069。
創建表
創建一個 4位年份的字段,以及一個 2位年份的字段:
mysql> create table t_year(
-> col1 year(4),
-> col2 year(2)
-> );
Query OK, 0 rows affected, 1 warning (0.00 sec)
正常操作
分別插入 2019 19數據:
mysql> insert into t_year values(2019,19);
Query OK, 1 row affected (0.00 sec)
mysql> select * from t_year;
+------+------+
| col1 | col2 |
+------+------+
| 2019 | 19 |
+------+------+
1 row in set (0.00 sec)
插入0操作
mysql> insert into t_year values(0,0);
Query OK, 1 row affected (0.01 sec)
mysql> select * from t_year;
+------+------+
| col1 | col2 |
+------+------+
| 2019 | 19 |
| 0000 | 00 |
+------+------+
2 rows in set (0.00 sec)
無效數據,直接補0處理。
插入‘0’操作
mysql> insert into t_year values('0','0');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t_year;
+------+------+
| col1 | col2 |
+------+------+
| 2019 | 19 |
| 0000 | 00 |
| 2000 | 00 |
+------+------+
4 rows in set (0.00 sec)
單引號,把數字括進去,當前年份最右面位數,4位默認從2000開始,補0,找最多0的年份。
TIME
小時:分鐘:秒鐘
範圍:過去的時間開始計算。
創建表
mysql> create table t_time(
-> col1 time
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> desc t_time;
+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+-------+
| col1 | time | YES | | NULL | |
+-------+------+------+-----+---------+-------+
1 row in set (0.00 sec)
正常操作
插入一條正常數據,用“”包括:
mysql> insert into t_time values ('10:10:10');
Query OK, 1 row affected (0.01 sec)
不帶:操作
mysql> insert into t_time values (1122);
Query OK, 1 row affected (0.00 sec)
mysql> select * from t_time;
+----------+
| col1 |
+----------+
| 10:10:10 |
| 00:11:22 |
+----------+
2 rows in set (0.00 sec)
沒有:的時候,MySQL默認爲最右面的數字爲秒。當然不帶:的情況下,用不用“”都是一樣的效果。
空格操作
mysql> insert into t_time values ('2 10:10');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t_time;
+----------+
| col1 |
+----------+
| 10:10:10 |
| 00:11:22 |
| 58:10:00 |
+----------+
3 rows in set (0.00 sec)
2空格,代表 在原有的 10:10:00 加上2天,也就是48個小時的意思。
因爲 加了:所以按着10左邊來計算,第一個是就是小時計算。
mysql> insert into t_time values ('2 10');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t_time;
+----------+
| col1 |
+----------+
| 10:10:10 |
| 00:11:22 |
| 58:10:00 |
| 58:00:00 |
+----------+
4 rows in set (0.00 sec)
10前後都沒有:被認爲小時,2天加10 爲58小時。
mysql> insert into t_time values ('10');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t_time;
+----------+
| col1 |
+----------+
| 10:10:10 |
| 00:11:22 |
| 58:10:00 |
| 58:00:00 |
| 00:00:10 |
+----------+
5 rows in set (0.00 sec)
沒有:引號包括,最右計算,爲秒。
DATE
創建表
mysql> create table t_date (
-> col1 date
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> desc t_date;
+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+-------+
| col1 | date | YES | | NULL | |
+-------+------+------+-----+---------+-------+
1 row in set (0.00 sec)
正常操作
mysql> insert into t_date values ('2019-7-16');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t_date;
+------------+
| col1 |
+------------+
| 2019-07-16 |
+------------+
1 row in set (0.00 sec)
DATETIME
一般情況不要使用datetime因爲會佔用8 bytes,而time + date 才佔用6 bytes。
創建表
mysql> create table t_dt ( col1 datetime );
Query OK, 0 rows affected (0.00 sec)
mysql> desc t_dt;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| col1 | datetime | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
1 row in set (0.00 sec)
正常操作
mysql> insert into t_dt values ('1999-10-1');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t_dt;
+---------------------+
| col1 |
+---------------------+
| 1999-10-01 00:00:00 |
+---------------------+
1 row in set (0.00 sec)
datetime完整類型是 年月日 時分秒,如果省略了 時分秒,默認會補全。
完整格式
mysql> insert into t_dt values ('2012-10-1 10:10:10');
Query OK, 1 row affected (0.01 sec)
mysql> select * from t_dt;
+---------------------+
| col1 |
+---------------------+
| 1999-10-01 00:00:00 |
| 2012-10-01 10:10:10 |
+---------------------+
2 rows in set (0.00 sec)
簡寫格式
mysql> insert into t_dt values ('20-10-1 10:10:10');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t_dt;
+---------------------+
| col1 |
+---------------------+
| 1999-10-01 00:00:00 |
| 2012-10-01 10:10:10 |
| 2020-10-01 10:10:10 |
+---------------------+
3 rows in set (0.00 sec)
TIMESTAMP(時間戳)
創建表
mysql> create table t_tt ( col1 timestamp );
Query OK, 0 rows affected (0.00 sec)
mysql> desc t_tt;
+-------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+-------------------+-----------------------------+
| col1 | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+-------------------+-----------------------------+
1 row in set (0.00 sec)
正常操作
mysql> insert into t_tt values (now());
Query OK, 1 row affected (0.00 sec)
mysql> select * from t_tt;
+---------------------+
| col1 |
+---------------------+
| 2019-07-16 23:28:18 |
+---------------------+
now()受當前系統的影響。
可以修改時區看一下效果:
mysql> show variables like 'time_zone';
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| time_zone | SYSTEM |
+---------------+--------+
1 row in set (0.00 sec)
mysql> set time_zone='+10:00';
髒數據
mysql> insert into t_tt values ('88)');
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> select * from t_tt;
+---------------------+
| col1 |
+---------------------+
| 0000-00-00 00:00:00 |
+---------------------+
2 rows in set (0.00 sec)
插入一條非時間格式的數據,會顯示 默認值 0000-00-00 00:00:00