Home >Database >Mysql Tutorial >MySQLTIMESTAMP(时间戳)详解_MySQL

MySQLTIMESTAMP(时间戳)详解_MySQL

WBOY
WBOYOriginal
2016-06-01 13:02:251003browse

在创建表时如果表中有一个字段类型为TIMESTAMP,则该字段默认的生成语句为:

CREATE TABLE `test` (
  `id` int(11) DEFAULT NULL,
  `ctime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=gbk

如果有两个字段类型为TIMESTAMP,则生成语句为:

CREATE TABLE `test` (
  `id` INT(11) DEFAULT NULL,
  `ctime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `utime` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=INNODB DEFAULT CHARSET=gbk
TIMESTAMP设置默认值是Default CURRENT_TIMESTAMP、

TIMESTAMP设置随着表变化而自动更新是ON UPDATE CURRENT_TIMESTAMP
1、TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

在创建新记录和修改现有记录的时候都对这个数据列刷新
2、TIMESTAMP DEFAULT CURRENT_TIMESTAMP

在创建新记录的时候把这个字段设置为当前时间,但以后修改时,不再刷新它
3、TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

在创建新记录的时候把这个字段设置为0,以后修改时刷新它

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn