MySQL error TIMESTAMP column with CURRENT_TIMESTAMP Solution

Source: Internet
Author: User

A problem encountered when deploying a program. The MySQL definition is as follows:
Copy codeThe Code is as follows:
Create table 'example '(
'Id' integer unsigned not null AUTO_INCREMENT,
'Created 'timestamp not null default CURRENT_TIMESTAMP,
'Lastupdated' timestamp not null on update CURRENT_TIMESTAMP,
Primary key ('id ')
) ENGINE = InnoDB;
I extracted this SQL statement from the project. Everything is normal on the test machine, but MySQL reports an error when deploying it on the production machine:
Copy codeThe Code is as follows:
ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or on update clause.

This means that only one timestamp column with CURRENT_TIMESTAMP exists, but why is there no problem in local testing? MySQL version 5.6.13 installed on the local Testing Machine, the version 5.5 is installed on the production machine. After searching for the network, the difference between the two versions for timestamp processing is:


In the MySQL 5.5 document, there is a saying:
Copy codeThe Code is as follows:
One TIMESTAMP column in a table can have the current timestamp as the default value for initializing the column, as the auto-update value, or both. it is not possible to have the current timestamp be the default value for one column and the auto-update value for another column.
In MySQL 5.6.5, the following changes were made:
Copy codeThe Code is as follows:
Previusly, at most one TIMESTAMP column per table cocould be automatically initialized or updated to the current date and time. this restriction has been lifted. any TIMESTAMP column definition can have any combination of DEFAULT CURRENT_TIMESTAMP and on update CURRENT_TIMESTAMP clses. in addition, these clses now can be used with DATETIME column definitions. for more information, see initiic Initialization and Updating for TIMESTAMP and DATETIME.
Based on the online solutions, you can use triggers to replace them:
Copy codeThe Code is as follows:
Create table 'example '(
'Id' integer unsigned not null AUTO_INCREMENT,
'Created 'timestamp not null default CURRENT_TIMESTAMP,
'Lastupdated' datetime not null,
Primary key ('id ')
) ENGINE = InnoDB;
Drop trigger if exists 'Update _ example_trigger ';
DELIMITER //
Create trigger 'Update _ example_trigger 'before update ON 'example'
For each row set new. 'lastupdated' = NOW ()
//
DELIMITER;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.