MYSQL—— year类型的使用与注意点!

mysql的日期与时间类型:分为time、date、datetime、timestamp、year,主要总结下year的用法:

 

1、类型支持:year 与 year(4),注意无year(2)的定义方式,否则报错“[Err] 1818 - Supports only YEAR or YEAR(4) column.”

create table if not exists time(
atime YEAR                         #year的定义,可写成year或者year(4)    
)engine=innodb charset = utf8;

 

2、插入值,支持整数和字符串,支持 2位数 或者 4位数

  00~69  将转换为2000~2069之间

   70~99  将转换为1970~1999之间

#测试year类型
insert into time values(78);   #数据库中显示:1978
insert into time values('78'); #数据库中显示:1978
insert into time values('1978'); #数据库中显示:1978

 

3、注意点

  1、支持插入 数字0 或者 字符串0,实际显示的数值不同

insert into time values(0);  #数据库中显示:0
insert into time values('0'); #数据库中显示:2000

   2、year只保存年份,占用空间小

   3、其他和日期有关的可以通过整型保存

       

posted @ 2017-05-22 15:21  Syw_文  阅读(17060)  评论(0编辑  收藏  举报