mysql数据类型--[日期类型]--year类型

year类型说明

类   型:year
显示效果:年
有效范围:1000 至 9999
插入方式:
       insert into test5(mydate) values(2018);
       insert into test5(mydate) values('2017');
       insert into test5(mydate) values(year(now()));
       insert into test5(mydate) values(year(curdate()));

实践环境准备

-- 创建chenliang库,字符集为utf8
mysql> create database if not exists chenliang character set utf8 collate utf8_general_ci;
Query OK, 1 row affected (0.01 sec)

mysql> show databases like "chenliang";
+----------------------+
| Database (chenliang) |
+----------------------+
| chenliang            |
+----------------------+
1 row in set (0.00 sec)

-- 进入chenliang库,并查看是否成功进入到了chenliang库
mysql> use chenliang;
Database changed

mysql> select database();
+------------+
| database() |
+------------+
| chenliang  |
+------------+
1 row in set (0.00 sec)

通过案例理解

-- 创建test5表,字符集为utf8,存储引擎为innodb,mydate字段类型为year
mysql> create table if not exists test5(
-> mydate year
-> )engine=innodb character set utf8 collate utf8_general_ci;
Query OK, 0 rows affected (0.01 sec)

-- 查看test5表的表结构
mysql> desc test5;
+--------+---------+------+-----+---------+-------+
| Field  | Type    | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+-------+
| mydate | year(4) | YES  |     | NULL    |       |
+--------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

-- 第一种插入数据的方式
mysql> insert into test5(mydate) values(2018);
Query OK, 1 row affected (0.01 sec)

mysql> select * from test5;
+--------+
| mydate |
+--------+
| 2018   |
+--------+
1 row in set (0.00 sec)

-- 第二种插入数据的方式
mysql> insert into test5(mydate) values('2017');
Query OK, 1 row affected (0.00 sec)

mysql> select * from test5;
+--------+
| mydate |
+--------+
| 2018   |
| 2017   |
+--------+
2 rows in set (0.00 sec)

-- 第三种插入数据的方式
mysql> insert into test5(mydate) values(year(now()));
Query OK, 1 row affected (0.00 sec)

mysql> select * from test5;
+--------+
| mydate |
+--------+
| 2018   |
| 2017   |
| 2019   |
+--------+
3 rows in set (0.00 sec)

-- 第四种插入数据的方式
mysql> insert into test5(mydate) values(year(curdate()));
Query OK, 1 row affected (0.00 sec)

mysql> select * from test5;
+--------+
| mydate |
+--------+
| 2018   |
| 2017   |
| 2019   |
| 2019   |
+--------+
4 rows in set (0.00 sec)

 

转载于:https://www.cnblogs.com/chenliangc/articles/11107481.html

你可能感兴趣的:(mysql数据类型--[日期类型]--year类型)