mysql單表多timestamp的current_timestamp設置問題
一個表中出現多個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也能成功建表, 但是反過來就不行.
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;