Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alter table add new field "ON UPDATE CURRENT_TIMESTAMP"

Tags:

mysql

This is my table:

CREATE TABLE `megssage`(
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(256) DEFAULT NULL,
  `time_create` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

I would like to add new column as time_updated with default value "ON UPDATE CURRENT_TIMESTAMP".

I tried this:

ALTER TABLE `megssage` 
   CHANGE `time_updated` `time_updated` TIMESTAMP NULL DEFAULT  ON UPDATE CURRENT_TIMESTAMP

But I am getting an error. Can someone help me?

like image 1000
Anand Avatar asked Jun 13 '12 10:06

Anand


2 Answers

Try

ALTER TABLE `megssage` 
    CHANGE COLUMN `time_updated` `time_updated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ;
like image 104
SimSimY Avatar answered Oct 12 '22 20:10

SimSimY


If you want to add a field to the megssage table:

ALTER TABLE `megssage` ADD time_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
like image 21
Joseph Samuel Avatar answered Oct 12 '22 21:10

Joseph Samuel