mysql timestamp写法_MySQL timestamp用法

与timestamp类型相关的类型包括:date类型与datetime类型。date类型只包含日期部分,不包含时间部分,它的格式为'YYYY-MM-DD',支持的范围为'1000-01-01' to '9999-12-31'。datetime类型包含日期和时间两部分,它的格式为'YYYY-MM-DD HH:MM:SS',支持的范围为'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。

MySQL将timestamp值从当前时区转换为UTC存储,查询时再从UTC转换为当前时区的值。

timestamp支持自动初始化和更新到当前日期和时间。下面是自动初始化和更新到当前日期和时间的几种组合方式:

With both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP, the column has the current timestamp for its default value and is automatically updated to the current timestamp.

CREATE TABLE t1 (

ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

);

With neither DEFAULT CURRENT_TIMESTAMP nor ON UPDATE CURRENT_TIMESTAMP, it is the same as specifying both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP.

CREATE TABLE t1 (

ts TIMESTAMP

);

With a DEFAULT clause but no ON UPDATE CURRENT_TIMESTAMP clause, the column has the given default value and is not automatically updated to the current timestamp.

The default depends on whether the DEFAULT clause specifies CURRENT_TIMESTAMP or a constant value. With CURRENT_TIMESTAMP, the default is the current timestamp.

CREATE TABLE t1 (

ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP

);

With a constant, the default is the given value. In this case, the column has no automatic properties at all.

CREATE TABLE t1 (

ts TIMESTAMP DEFAULT 0

);

With an ON UPDATE CURRENT_TIMESTAMP clause and a constant DEFAULT clause, the column is automatically updated to the current timestamp and has the given constant default value.

CREATE TABLE t1 (

ts TIMESTAMP DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP

);

With an ON UPDATE CURRENT_TIMESTAMP clause but no DEFAULT clause, the column is automatically updated to the current timestamp. The default is 0 unless the column is defined with the NULL attribute, in which case the default is NULL.

CREATE TABLE t1 (

ts TIMESTAMP ON UPDATE CURRENT_TIMESTAMP -- default 0

);

CREATE TABLE t2 (

ts TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP -- default NULL

);

datetime或者timestamp类型可在末尾包含小数部分,用于表示毫秒数。

无效的date、datetime、timestamp值会转换为'0000-00-00' 或者 '0000-00-00 00:00:00'。

MySQL采用如下规则处理两位数年的情况:

00-69被转换为2000-2069

70-99被转换为1970-1999

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL的CURRENT_TIMESTAMP是一个用于设置日期类型字段默认值的函数。在MySQL 5.6.5版本之前,DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP只适用于TIMESTAMP类型字段,并且一张表中最多只能有一个字段采用该特性。\[1\]从MySQL 5.6.5开始,DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP也适用于DATETIME类型字段,并且不再限制数量。\[1\]在创建表时,可以使用CURRENT_TIMESTAMP来指定TIMESTAMP或DATETIME字段的默认值,例如CREATE TABLE语句中的示例代码。\[3\]需要注意的是,使用CURRENT_TIMESTAMP作为DATETIME字段的默认值只适用于MySQL 5.6版本及以上的版本。\[2\] #### 引用[.reference_title] - *1* *2* [MySQL中的CURRENT_TIMESTAMP](https://blog.csdn.net/yaxuan88521/article/details/121899768)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [MySql的CURRENT_TIMESTAMP](https://blog.csdn.net/ab15176142633/article/details/107057497)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值