Type | Range | Remark |
DATE | '1000-01-01' to '9999-12-31' |
只有日期部分,没有时间部分 |
DATETIME | '1000-01-01 00:00:00' to '9999-12-31 23:59:59' |
时间格式为 Y YYY-MM-DD hh:mm:ss ,默认精确到秒 |
TIMESTAMP | '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC |
默认精确到秒 |
5.7 以后的版本(其实应该说5.6.5),在默认的秒精确度上,能够带小数,最多带6位小数,便可以精确到 microseconds (6 digits) precision。java
Type | Range | Remark |
DATETIME | '1000-01-01 00:00:00.000000' to '9999-12-31 23:59:59.999999' |
'YYYY-MM-DD hh:mm:ss [.fraction ]' |
TIMESTAMP | '1970-01-01 00:00:01.000000' to '2038-01-19 03:14:07.999999' |
'YYYY-MM-DD hh:mm:ss [.fraction ]' |
MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.)
By default, the current time zone for each connection is the server's time. The time zone can be set on a per-connection basis.
As long as the time zone setting remains constant, you get back the same value you store. If you store a TIMESTAMP value, and then change the time zone and retrieve the value,
the retrieved value is different from the value you stored. This occurs because the same time zone was not used for conversion in both directions.
The current time zone is available as the value of the time_zone system variable.
可能上面的几句英文很差理解,咱们举个例子。mysql
建立2张测试表:git
create table testtime(id int,hiredate timestamp); create table testtime1(id int,hiredate datetime);
向这两个测试表中分别插入一笔测试数据sql
insert into testtime values(1,'20151208000000'); insert into testtime1 values(1,'20151208000000');
查看这种显示的时区时间设置测试
查询命令spa
show variables like '%time_zone%';
上述“CST”指的是MySQL所在主机的系统时间,是中国标准时间的缩写,China Standard Time UT+8:00code
修改time_zoneserver
set time_zone='+0:00';
经过结果能够看出,testtime中返回的时间提早了8个小时,而testtime1中时间则不变。blog
若是新建一个客户端链接,这个时区的修改不影响新链接。ci
TIMESTAMP 在mysql 5.6.5以后,TIMESTAMP(fraction
)中的fraction
表明的是小数位数,即默认秒,以秒为单位的小数点位数。 up to microseconds (6 digits) precision,最大为6.
超过6则报错:
ERROR 1426 (42000): Too-big precision 7 specified for 'hiredate'. Maximum is 6.
在比较久的版本上,这个数字就表明不一样的意义,如下内容为旧版本的关于TIMESTAMP的知识。
TIMESTAMP(fraction
)中fraction
值显示尺寸的格式以下表所示:
列类型 | 显示格式 |
TIMESTAMP(14) | YYYYMMDDHHMMSS |
TIMESTAMP(12) | YYMMDDHHMMSS |
TIMESTAMP(10) | YYMMDDHHMM |
TIMESTAMP(8) | YYYYMMDD |
TIMESTAMP(6) | YYMMDD |
TIMESTAMP(4) | YYMM |
TIMESTAMP(2) | YY |
就版本中“完整”TIMESTAMP格式是14位,但TIMESTAMP列也能够用更短的显示尺寸,创造最多见的显示尺寸是六、八、十二、和14。
在建立表时能够指定一个任意的显示尺寸,可是定义列长为0或比14大均会被强制定义为列长14。列长在从1~13范围的奇数值尺寸均被强制为下一个更大的偶数。