mysql timestamp

来源:互联网 发布:java static和instance 编辑:程序博客网 时间:2024/05/22 03:00

imestamp有两个属性,分别是CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP两种,使用情况分别如下:


1.

 

CURRENT_TIMESTAMP 

当要向数据库执行insert操作时,如果有个timestamp字段属性设为

 

CURRENT_TIMESTAMP,则无论这个字段有没有set值都插入当前系统时间

 



2.

 

ON UPDATE CURRENT_TIMESTAMP

当执行update操作是,并且字段有ON UPDATE CURRENT_TIMESTAMP属性。则字段无论值有没有变化,它的值也会跟着更新为当前UPDATE操作时的时间。



mysql单表多timestamp的current

 (2012-07-21 07:02:36)
转载
标签: 

娱乐

 

一个表中出现多个timestamp并设置其中一个为current_timestamp的时候经常会遇到

#1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

原因是当你给一个timestamp设置为on update current_timestamp的时候,其他的timestamp字段需要显式设定default值

但是如果你有两个timestamp字段,但是只把第一个设定为current_timestamp而第二个没有设定默认值,mysql也能成功建表, 但是反过来就不行...参见这里

参考文章:

Why there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT clause?

MySQL的timestamp类型自动更新问题

mysqldoc - timestamp

eg:

?

CREATE TABLE `users` (
? `iUId` int(10) unsigned NOT NULL AUTO_INCREMENT,
? `szUsername` varchar(45) NOT NULL,
? `szEmail` varchar(60) DEFAULT NULL,
? `szPassword` varchar(64) NOT NULL, ?
? `szInsertTime` timestamp NOT NULL DEFAULT 0,
? `szLastModTime` timestamp ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
? `iStatus` int(11) NOT NULL DEFAULT '0',
? PRIMARY KEY (`iUId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

0 0