===时间和日期类型测试:year、date、time、date
(作用:用于存储用户的注册时间,文章的发布时间,文章的更新时间,员工的入职时间等)
一、LAB1:(日期,时间)
日期date和时间time类型测试
1、创建一个表
示例:
mysql> create table test_time(
d date,
t time,
dt datetime
);
Query OK, 0 rows affected (0.03 sec)
2、查看表结构
示例:
mysql> desc test_time;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| d | date | YES | | NULL | |
| t | time | YES | | NULL | |
| dt | datetime | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.01 sec)
3、插入时间
了解一个函数
MySQL [(none)]> select now();
+---------------------+
| now() |
+---------------------+
| 2016-11-14 13:35:51 |
+---------------------+
1 row in set (0.00 sec)
示例:
1)mysql> insert into test_time values(now(),now(),now());
Query OK, 1 row affected, 1 warning (0.02 sec)
2)insert into test5 (d) values (20170412);
4、查看表内容
示例:
mysql> select * from test_time;
+------------+----------+---------------------+
| d | t | dt |
+------------+----------+---------------------+
| 2016-12-18 | 00:06:10 | 2013-12-18 00:06:10 |
+------------+----------+---------------------+
1 row in set (0.00 sec)
(时间日期分类储存)
二、LAB2:(当前时间)(了解)
当前时间timestamp类型测试
1、创建一个表
示例:
mysql> create table t(id timestamp);
Query OK, 0 rows affected (0.01 sec)
2、查询表结构
示例:
mysql> desc t;
+-------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------+------+-----+-------------------+-----------------------------+
| id | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+-----------+------+-----+-------------------+-----------------------------+
1 row in set (0.00 sec)
(| CURRENT_TIMESTAMP |)
3、插入空值
示例:
mysql> insert into t values (121112111211);
(年月日时分秒)
4、查询内容
示例:
mysql> select * from t;
+---------------------+
| id |
+---------------------+
| 2016-12-18 00:08:41 |
+---------------------+
1 row in set (0.00 sec)
三、LAB3:(年)
年YEAR类型测试
{(注意)其它的时间,按要求插入
==插入年份时,尽量使用4位值
==插入两位年份时,<=69,默认以20开头,比如65, 结果2065
>=70,默认以19开头,比如82,结果1982
}
1、创建表
示例:mysql> create table t3(born_year year);
Query OK, 0 rows affected (0.40 sec)
2、表结构
示例:
mysql> desc t3;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| born_year | year(4) | YES | | NULL | |
+-----------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
3、插入数据
示例:
mysql> insert into t3 values (12),(80);
Query OK, 2 rows affected (0.06 sec)
Records: 2 Duplicates: 0 Warnings: 0
4、查询结果
示例:
mysql> select * from t3;
+-----------+
| born_year |
+-----------+
| 2012 |
| 1980 |
+-----------+
2 rows in set (0.00 sec)
5、请思考插入四位值会怎样?
结果:2099/1901