mysql 表设计时的update_time自动更新
11.3.5 Automatic Initialization and Updating for TIMESTAMP and DATETIME
原文地址:https://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html
As of MySQL 5.6.5, TIMESTAMP and DATETIME columns can be automatically initializated and updated to the current date and time (that is, the current timestamp). Before 5.6.5, this is true only for TIMESTAMP, and for at most one TIMESTAMP column per table. The following notes first describe automatic initialization and updating for MySQL 5.6.5 and up, then the differences for versions preceding 5.6.5.
For any TIMESTAMP or DATETIME column in a table, you can assign the current timestamp as the default value, the auto-update value, or both:
An auto-initialized column is set to the current timestamp for inserted rows that specify no value for the column.
An auto-updated column is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value. An auto-updated column remains unchanged if all other columns are set to their current values. To prevent an auto-updated column from updating when other columns change, explicitly set it to its current value. To update an auto-updated column even when other columns do not change, explicitly set it to the value it should have (for example, set it to
CURRENT_TIMESTAMP).
In addition, you can initialize or update any TIMESTAMP column to the current date and time by assigning it a NULLvalue, unless it has been defined with the NULL attribute to permit NULL values.
To specify automatic properties, use the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMPclauses in column definitions. The order of the clauses does not matter. If both are present in a column definition, either can occur first. Any of the synonyms for CURRENT_TIMESTAMP have the same meaning asCURRENT_TIMESTAMP. These are CURRENT_TIMESTAMP(), NOW(), LOCALTIME, LOCALTIME(), LOCALTIMESTAMP, and LOCALTIMESTAMP().
Use of DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP is specific to TIMESTAMP andDATETIME. The DEFAULT clause also can be used to specify a constant (nonautomatic) default value; for example,DEFAULT 0 or DEFAULT '2000-01-01 00:00:00'.
The following examples use DEFAULT 0, a default that can produce warnings or errors depending on whether strict SQL mode or the NO_ZERO_DATE SQL mode is enabled. Be aware that the TRADITIONAL SQL mode includes strict mode and NO_ZERO_DATE. See Section 5.1.7, “Server SQL Modes”.
TIMESTAMP or DATETIME column definitions can specify the current timestamp for both the default and auto-update values, for one but not the other, or for neither. Different columns can have different combinations of automatic properties. The following rules describe the possibilities:
With both
DEFAULT CURRENT_TIMESTAMPandON 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,
dt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);With a
DEFAULTclause but noON UPDATE CURRENT_TIMESTAMPclause, the column has the given default value and is not automatically updated to the current timestamp.The default depends on whether the
DEFAULTclause specifiesCURRENT_TIMESTAMPor a constant value. WithCURRENT_TIMESTAMP, the default is the current timestamp.CREATE TABLE t1 (
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
dt DATETIME 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,
dt DATETIME DEFAULT 0
);With an
ON UPDATE CURRENT_TIMESTAMPclause and a constantDEFAULTclause, 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,
dt DATETIME DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP
);With an
ON UPDATE CURRENT_TIMESTAMPclause but noDEFAULTclause, the column is automatically updated to the current timestamp but does not have the current timestamp for its default value.The default in this case is type dependent.
TIMESTAMPhas a default of 0 unless defined with theNULLattribute, in which case the default isNULL.CREATE TABLE t1 (
ts1 TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, -- default 0
ts2 TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP -- default NULL
);DATETIMEhas a default ofNULLunless defined with theNOT NULLattribute, in which case the default is 0.CREATE TABLE t1 (
dt1 DATETIME ON UPDATE CURRENT_TIMESTAMP, -- default NULL
dt2 DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP -- default 0
);
TIMESTAMP and DATETIME columns have no automatic properties unless they are specified explicitly, with this exception: By default, the first TIMESTAMP column has both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP if neither is specified explicitly. To suppress automatic properties for the first TIMESTAMPcolumn, use one of these strategies:
Enable the
explicit_defaults_for_timestampsystem variable. If this variable is enabled, theDEFAULT CURRENT_TIMESTAMPandON UPDATE CURRENT_TIMESTAMPclauses that specify automatic initialization and updating are available, but are not assigned to anyTIMESTAMPcolumn unless explicitly included in the column definition.Alternatively, if
explicit_defaults_for_timestampis disabled (the default), do either of the following:Define the column with a
DEFAULTclause that specifies a constant default value.Specify the
NULLattribute. This also causes the column to permitNULLvalues, which means that you cannot assign the current timestamp by setting the column toNULL. AssigningNULLsets the column toNULL.
Consider these table definitions:
CREATE TABLE t1 (
ts1 TIMESTAMP DEFAULT 0,
ts2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP);
CREATE TABLE t2 (
ts1 TIMESTAMP NULL,
ts2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP);
CREATE TABLE t3 (
ts1 TIMESTAMP NULL DEFAULT 0,
ts2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP);
The tables have these properties:
In each table definition, the first
TIMESTAMPcolumn has no automatic initialization or updating.The tables differ in how the
ts1column handlesNULLvalues. Fort1,ts1isNOT NULLand assigning it a value ofNULLsets it to the current timestamp. Fort2andt3,ts1permitsNULLand assigning it a value ofNULLsets it toNULL.t2andt3differ in the default value forts1. Fort2,ts1is defined to permitNULL, so the default is alsoNULLin the absence of an explicitDEFAULTclause. Fort3,ts1permitsNULLbut has an explicit default of 0.
If a TIMESTAMP or DATETIME column definition includes an explicit fractional seconds precision value anywhere, the same value must be used throughout the column definition. This is permitted:
CREATE TABLE t1 (
ts TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)
);
This is not permitted:
CREATE TABLE t1 (
ts TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP(3)
);
Automatic Timestamp Properties Before MySQL 5.6.5
Before MySQL 5.6.5, support for automatic initialization and updating is more limited:
DEFAULT CURRENT_TIMESTAMPandON UPDATE CURRENT_TIMESTAMPcannot be used withDATETIMEcolumns.DEFAULT CURRENT_TIMESTAMPandON UPDATE CURRENT_TIMESTAMPcan be used with at most oneTIMESTAMPcolumn per table. It is not possible to have the current timestamp be the default value for one column and the auto-update value for another column.
You can choose whether to use these properties and which TIMESTAMP column should have them. It need not be the first one in a table that is automatically initialized or updated to the current timestamp. To specify automatic initialization or updating for a different TIMESTAMP column, you must suppress the automatic properties for the first one, as previously described. Then, for the other TIMESTAMP column, the rules for the DEFAULT and ON UPDATE clauses are the same as for the first TIMESTAMP column, except that if you omit both clauses, no automatic initialization or updating occurs.
TIMESTAMP Initialization and the NULL Attribute
By default, TIMESTAMP columns are NOT NULL, cannot contain NULL values, and assigning NULL assigns the current timestamp. To permit a TIMESTAMP column to contain NULL, explicitly declare it with the NULL attribute. In this case, the default value also becomes NULL unless overridden with a DEFAULT clause that specifies a different default value. DEFAULT NULL can be used to explicitly specify NULL as the default value. (For a TIMESTAMPcolumn not declared with the NULL attribute, DEFAULT NULL is invalid.) If a TIMESTAMP column permits NULLvalues, assigning NULL sets it to NULL, not to the current timestamp.
The following table contains several TIMESTAMP columns that permit NULL values:
CREATE TABLE t
(
ts1 TIMESTAMP NULL DEFAULT NULL,
ts2 TIMESTAMP NULL DEFAULT 0,
ts3 TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP
);
A TIMESTAMP column that permits NULL values does not take on the current timestamp at insert time except under one of the following conditions:
Its default value is defined as
CURRENT_TIMESTAMPand no value is specified for the columnCURRENT_TIMESTAMPor any of its synonyms such asNOW()is explicitly inserted into the column
In other words, a TIMESTAMP column defined to permit NULL values auto-initializes only if its definition includesDEFAULT CURRENT_TIMESTAMP:
CREATE TABLE t (ts TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP);
If the TIMESTAMP column permits NULL values but its definition does not include DEFAULT CURRENT_TIMESTAMP, you must explicitly insert a value corresponding to the current date and time. Suppose that tables t1 and t2 have these definitions:
CREATE TABLE t1 (ts TIMESTAMP NULL DEFAULT '0000-00-00 00:00:00');
CREATE TABLE t2 (ts TIMESTAMP NULL DEFAULT NULL);
To set the TIMESTAMP column in either table to the current timestamp at insert time, explicitly assign it that value. For example:
INSERT INTO t1 VALUES (NOW());
INSERT INTO t2 VALUES (CURRENT_TIMESTAMP);
mysql 表设计时的update_time自动更新的更多相关文章
- EF5修改edmx表结构保存后不自动更新tt (转)
http://blog.csdn.net/panderman/article/details/8172968 不知道这算不算一个bug,当你新建一个从数据库生成的edmx时,他能正确的生成所有的tt文 ...
- 【JDBC】向数据表插入数据时,自动获取生成的主键
数据表设计时,一般都会有一个主键(Key)(自己指定),有时也可以使用联合主键: 有许多数据库提供了隐藏列为表中的每行记录分配一个唯一键值(如:rowid): 当我们没有指定哪一列作为主键key时,数 ...
- mysql每次update数据,自动更新对应表中时间字段
mysql 已经创建完成表的情况下, 使得其中的时间字段 在每次 uodate 数据的时候 自动更新事件, 运行如下sql ALTER TABLE tab_name MODIFY COLUMN upd ...
- Mysql 添加 create_time, update_time 创建时间 更新时间 自动更新
# 添加 创建 更新 时间字段 ALTER TABLE `表名` ADD COLUMN `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTA ...
- [mysql] timestamp自动更新和初始化
1.概述 在我们设计表的时候,考虑将行数据的创建时间和最后更新时间记录下来是很好的实践.尤其是可能需要做数据同步或者对数据新鲜度有要求的表.举些应用场景,更新距上次更新超过2小时的行数据,或者是将一个 ...
- MySQL的timestamp类型自动更新问题
今天建了一个表,里面有一个列是timestamp类型,我本意是在数据更新时,这个字段的时间能自动更新.岂知对这个类型的值还不甚了解,导致出错.发现这个字段只是在这行数据建立的时候有值,在更新的却无变化 ...
- Mysql自动更新字段时间
Mysql中更新某天数据可设置该条数据中的某个字段自动更新 ALTER TABLE `表名` MODIFY `字段名` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON U ...
- Mysql中,update语句引起的时间戳自动更新问题
前几天遇到一个奇怪的问题. 在Mysql数据库中有一张表,表中有一个字段是timestamp类型的.我在update别的字段时,这个timestamp字段的时间会自动更新为当前时间. 后来发现,是My ...
- MySQL表内更新时,自动记录时间
1.创建表: create table test_time(id int primary key not null,status varchar(24),create_time datetime d ...
随机推荐
- Cocos2d-x第三方类库不支持arm64的问题解决(64位架构)
32位能够兼容64位操作系统. ipad mini2 64位编译有问题. 各种第三方库不支持64位操作系统. 设置build setting 直接上图:
- poj--1237--Drainage Ditches(最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u Sub ...
- SpringMVC+MyBatis (druid、logback)
数据库连接池是阿里巴巴的druid.日志框架式logback 1.整合SpringMVCspringMybatis-servlet.xml: <?xml version="1.0&qu ...
- linux大于2T的磁盘使用GPT分区的方法分享
(parted)表示在parted中输入的命令,其他为自动打印的信息 1.首先类似fdisk一样,先选择要分区的硬盘,此处为/dev/sdb ey: parted /dev/sdb 2.选择了/dev ...
- HDU 5446 Unknown Treasure Lucas+中国剩余定理+按位乘
HDU 5446 Unknown Treasure 题意:求C(n, m) %(p[1] * p[2] ··· p[k]) 0< n,m < 1018 思路:这题基本上算是模版题了 ...
- JS实现页面跳转 浏览器地址栏保持不变
JS实现页面跳转 浏览器地址栏保持不变 在公司内部框架中,发现点击超链接,页面发生跳转,而浏览器地址栏URL始终保持不变.分析其实现机制,响应A标签onclick事件,通过Ajax向服务器端发送htt ...
- excel操作小技巧
excel拼接sql语句时,时间格式问题 问题:若直接插入时间的单元格 :="insert into t_entity_car (create_time,name,age) value (' ...
- 安装SQL2008后需要打开的端口为 1433,4630
- 如何利用Python词云和wordart可视化工具对朋友圈数据进行可视化展示
大前天我们通过Python网络爬虫对朋友圈的数据进行了抓取,感兴趣的朋友可以点击进行查看,如何利用Python网络爬虫抓取微信朋友圈的动态(上)和如何利用Python网络爬虫爬取微信朋友圈动态——附代 ...
- js获取当前时间年份,处理年月日
js中获得当前时间年份.月份.日期 //获取完整的日期 var date=new Date; var y = date.getFullYear()var m = date.getMonth ...