牛刀小试MySQL学习-Date and Time Types

本文详细介绍了MySQL中日期和时间类型的存储需求与取值范围,包括DATE、TIME、DATETIME、TIMESTAMP及YEAR等类型,并提供了实例说明这些类型的具体应用。
Storage Requirements for Date and Time Types     

Data TypeStorage Required
DATE3 bytes
TIME3 bytes
DATETIME8 bytes
TIMESTAMP4 bytes
YEAR1 byte

DATE类型只有日期部分没有时间部分,MySQL取值和展示DATE值是已'YYYY-MM-DD'  format,支持的范围'1000-01-01'  to  '9999-12-31'.

DATETIME类型包括了日期和时间部分,MySQL展示的格式:'YYYY-MM-DD HH:MM:SS'  format,支持的范围'1000-01-01 00:00:00'  to  '9999-12-31 23:59:59'.

TIMESTAMP类型包括了其实和时间部分,支持的范围'1970-01-01 00:00:01'  UTC to  '2038-01-19 03:14:07'  UTC( UTC(Universal Time Coordinated),国际时间标准,咱们中国式东八区时间标准, http://space.itpub.net/7607759/viewspace-695482详细查看,三思兄的 MySQL数据库中的timestamp类型与时区
) 

TIME类型以'HH:MM:SS' format,支持的范围是:'838:59:59' to '838:59:59' 

YEAR类型,可以定义为YEAR(4) or YEAR(2),但是表现方式

不一致,如下:

  • As a 4-digit string in the range '1901' to '2155'.

  • As a 4-digit number in the range 1901 to 2155.

  • As a 1- or 2-digit string in the range '0' to '99'. Values in the ranges '0' to '69' and '70' to '99' are converted to YEAR values in the ranges 2000 to 2069 and             1970 to 1999.

  • As a 1- or 2-digit number in the range 1 to 99. Values in the ranges 1 to 69 and 70 to 99 are converted to YEAR values in the ranges 2001 to 2069 and 1970 to 1999.  


    备注小记:

    timestamp除了上面的时区问题,还有一个有趣的现象。

    mysql> create table t2date_zsd(time1 timestamp not null,time2 timestamp not null);
    Query OK, 0 rows affected (0.06 sec)
    mysql> show create table t2date_zsd;
    +------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Table | Create Table |
    +------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | t2date_zsd | CREATE TABLE `t2date_zsd` (
    `time1` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `time2` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
    +------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)

    mysql>

    从上述例子来看,如果不设置default,它默认给你current_timestamp另外赋予零值表示,而且timestamp不能同时CURRENT_TIMESTAMP或者now()存在,否则会报表定义错误(Incorrect table definition)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值