75

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.

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
kuba
  • 3,670
  • 3
  • 31
  • 41
  • 1
    I believe the error is clear and what you seek is the alternative way to use do the same thing like current_timestamp – GusDeCooL Jun 16 '12 at 15:40
  • 1
    Use recent version of MySQL(5.6.5+), that should work for you! – Amol Fasale Jul 13 '18 at 05:30
  • as per current documentation this syntax should be working:https://dev.mysql.com/doc/refman/8.0/en/timestamp-initialization.html – Channa Apr 03 '21 at 16:39

8 Answers8

20

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.

avinash v p
  • 531
  • 2
  • 8
  • 15
7

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
iKing
  • 667
  • 10
  • 15
  • '0000-00-00 00:00:00' is not valid timestamp, though mysql will allow, many other databases do not allow '0000-00-00 00:00:00' as default to timestamp datatype, so at certain time, if you are migrating away from mysql, this would be painful. – imVJ Aug 30 '16 at 09:48
  • This is actually painful also in MySQL, you shouldn't use zeros to initialise a timestamp or date/time column. – David Vartanian Oct 18 '17 at 08:15
  • Whether zeroes are allowed as a valid timestamp depends on the current SQL mode. Docs: https://dev.mysql.com/doc/refman/5.5/en/sql-mode.html#sqlmode_no_zero_date – alexg Jul 13 '18 at 14:20
5

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

1

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

clemquinones
  • 251
  • 2
  • 6
0

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

0

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

0

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.

Brian
  • 6,391
  • 3
  • 33
  • 49
0

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.

Nanne
  • 64,065
  • 16
  • 119
  • 163
  • you can't have a ON UPDATE TIMESTAMP if you have another field with TIMESTAMP in DEFAULT – Jeger May 24 '12 at 12:57
  • I know. But the question was not "why does this happen", but "how can I avoid it". This answers that question I believe better then "change the datatypes and make a trigger". – Nanne Feb 21 '13 at 10:14