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;