在mysql中timestamp数据类型是一个比较特殊的数据类型,他可以自动在你不使用程序更新情况下只要你更新了记录timestamp会自动更新时间
通常表中会有一个create date 创建日期的字段,其它数据库均有默认值的选项。mysql也有默认值timestamp,但在mysql中,不仅是插入就算是修改也会更新timestamp的值!
这样一来,就不是创建日期了,当作更新日期来使用比较好!
因此在mysql中要记录创建日期还得使用datetime 然后使用now() 函数完成!
1,timestamp default current_timestamp on update current_timestamp
在创建新记录和修改现有记录的时候都对这个数据列刷新
2,timestamp default current_timestamp 在创建新记录的时候把这个
字段设置为当前时间,但以后修改时,不再刷新它
3,timestamp on update current_timestamp 在创建新记录的时候把这个字段设置为0
、自动update 和insert 到当前的时间:
表:
---------------------------------
table create table
------ -------------------------- create table `t1` ( `p_c` int(11) not null, `p_time` timestamp not null default current_timestamp on update current_timestamp ) engine=innodb default charset=gb2312
数据:
1 2007-10-08 11:53:35
2 2007-10-08 11:54:00
insert into t1(p_c) select 3;update t1 set p_c = 2 where p_c = 2;
数据:
1 2007-10-08 11:53:35
2 2007-10-08 12:00:37
3 2007-10-08 12:00:37
2、自动insert 到当前时间,不过不自动update。
表:
---------------------------------
table create table
------ ---------------------------
create table `t1` ( `p_c` int(11) not null, `p_time` timestamp not null default current_timestamp ) engine=innodb default charset=gb2312
数据:
insert into t1(p_c) select 4;update t1 set p_c = 3 where p_c = 3;
1 2007-10-08 11:53:35
2 2007-10-08 12:00:37
3 2007-10-08 12:00:37
4 2007-10-08 12:05:19
3、一个表中不能有两个字段默认值是当前时间,否则就会出错。不过其他的可以。
表:
---------------------------------
table create table
------ -------------------------- create table `t1` ( `p_c` int(11) not null, `p_time` timestamp not null default current_timestamp, `p_timew2` timestamp not null default '0000-00-00 00:00:00' ) engine=innodb default charset=gb2312
数据:
1 2007-10-08 11:53:35 0000-00-00 00:00:00
2 2007-10-08 12:00:37 0000-00-00 00:00:00
3 2007-10-08 12:00:37 0000-00-00 00:00:00
4 2007-10-08 12:05:19 0000-00-00 00:00:00
比较之下,我的语句少了“on update current_timestamp”或多了“default current_timestamp”。如此一来,这个timestamp字段只是在数据insert的时间建立时间,而update时就不会有变化了。当然,如果你就是想达到这个目的倒也无所谓 1: 如果定义时default current_timestamp和on update current_timestamp子句都有,列值为默认使用当前的时间戳,并且自动更新。
2: 如果不使用default或on update子句,那么它等同于default current_timestamp on update current_timestamp。
3: 如果只有default current_timestamp子句,而没有on update子句,列值默认为当前时间戳但不自动更新。
4: 如果没用default子句,但有on update current_timestamp子句,列默认为0并自动更新。
5: 如果有一个常量值default,该列会有一个默认值,而且不会自动初始化为当前时间戳。如果该列还有一个on update current_timestamp子句,这个时间戳会自动更新,否则该列有一个默认的常量但不会自动更新。
换句话说,你可以使用当前的时间戳去初始化值和自动更新,或者是其中之一,也可以都不是。(比如,你在定义的时候可以指定自动更新,但并不初始化。)下面的字段定义说明了这些情况:
以上就是MySQL数据库中timestamp自动更新时间的方法的内容,更多相关内容请关注PHP中文网(www.php.cn)!
传统驾校预约方式步骤繁琐,效率低下,随着移动互联网科技和5G的革新,驾校考试领域迫切需要更加简洁、高效的预约方式,便捷人们的生活。因此设计基于微信小程序的驾校预约系统,改进传统驾校预约方式,实现高效的驾校学校预约。 采用腾讯提供的小程序云开发解决方案,无须服务器和域名。驾校预约管理:开始/截止时间/人数均可灵活设置,可以自定义客户预约填写的数据项驾校预约凭证:支持线下到场后校验签到/核销/二维码自
0
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号