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

it2022-05-05  271

Storage Requirements for Date and Time Types      Data TypeStorage RequiredDATE3 bytesTIME3 bytesDATETIME8 bytesTIMESTAMP4 bytesYEAR1 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)


最新回复(0)