MySQL CURRENT_TIMESTAMP on create and on update

MysqlTimestampMysql Error-1293

Mysql Problem Overview


I want to define table which will have 2 TIMESTAMP fields, someting like this:

CREATE TABLE `msgs` (
    `id` INT PRIMARY KEY AUTO_INCREMENT,
    `msg` VARCHAR(256),
    `ts_create` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    `ts_update` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)

How to do this avoiding error:

ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

Point is to keep desired behavior of ts_create and ts_update in table schema.

Mysql Solutions


Solution 1 - Mysql

Guess this is a old post but actually i guess mysql supports 2 TIMESTAMP in its recent editions mysql 5.6.25 thats what im using as of now.

Solution 2 - Mysql

i think it is possible by using below technique

`ts_create` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`ts_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

Solution 3 - Mysql

You are using older MySql version. Update your myqsl to 5.6.5+ it will work.

Solution 4 - Mysql

You cannot have two TIMESTAMP column with the same default value of CURRENT_TIMESTAMP on your table. Please refer to this link: http://www.mysqltutorial.org/mysql-timestamp.aspx

Solution 5 - Mysql

I think you maybe want ts_create as datetime (so rename -> dt_create) and only ts_update as timestamp? This will ensure it remains unchanging once set.

My understanding is that datetime is for manually-controlled values, and timestamp's a bit "special" in that MySQL will maintain it for you. In this case, datetime is therefore a good choice for ts_create.

Solution 6 - Mysql

I would say you don't need to have the DEFAULT CURRENT_TIMESTAMP on your ts_update: if it is empty, then it is not updated, so your 'last update' is the ts_create.

Solution 7 - Mysql

This is the tiny limitation of Mysql in older version , actually after version 5.6 and later multiple timestamps works...

Solution 8 - Mysql

you can try this ts_create TIMESTAMP DEFAULT CURRENT_TIMESTAMP, ts_update TIMESTAMP DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkubaView Question on Stackoverflow
Solution 1 - Mysqlavinash v pView Answer on Stackoverflow
Solution 2 - MysqliKingView Answer on Stackoverflow
Solution 3 - MysqlHimanshu ShekharView Answer on Stackoverflow
Solution 4 - MysqlclemquinonesView Answer on Stackoverflow
Solution 5 - MysqlBrianView Answer on Stackoverflow
Solution 6 - MysqlNanneView Answer on Stackoverflow
Solution 7 - MysqlSonpal singh SengarView Answer on Stackoverflow
Solution 8 - MysqlAJavaLoverView Answer on Stackoverflow