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、其他和日期有關的可以通過整型保存