Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of CURRENT_TIMESTAMP in MySQL

Shulou Source: shulou.com Published: 2022-06-02 11:07:38 09月19日 Update

This article mainly explains "what is the use of CURRENT_TIMESTAMP in MySQL". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the use of CURRENT_TIMESTAMP in MySQL".

The use of CURRENT_TIMESTAMP

As we all know, the date type of MySQL can use CURRENT_TIMESTAMP to specify the default value, but this depends on the version of MySQL and the specific type of date. Only versions after 5.6 can use CURRENT_TIMESTAMP as the default value for DATETIME.

For example:

ALTER TABLE t_user ADD update_time DATETIME DEFAULT CURRENT_TIMESTAMP

In versions prior to 5.6, the following error occurred when using CURRENT_TIMESTAMP as the default value

[Err] 1067-Invalid default value for 'update_time'

Prior to MySQL version 5.6.5, DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP was only available for TIMESTAMP, and at most one TIMESTAMP field in a table was allowed to use this feature. Starting from MySQL 5.6.5, DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP is available for both TIMESTAMP and DATETIME, and there is no limit to the number.

Timestamp uses CURRENT_TIMESTAMP to report errors

The following error occurred in the project:

Error updating database.

Cause:com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'createTime' cannot be null

The data model is as follows:

/ * creation time cannot be empty * / createTime timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'creation time', / * update time cannot be empty * / updateTime timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'update time'

After a series of troubleshooting, it is located that it is due to the problem of the global variable "explicit_defaults_for_timestamp" of different versions of the MySQL database.

-- View explicit_defaults_for_timestamp default value SHOW GLOBAL VARIABLES LIKE "explicit_defaults_for_timestamp";-- modify explicit_defaults_for_timestamp default value SET @ @ global.explicit_defaults_for_timestamp=OFF

If the parameter value is "ON":

Thank you for your reading, the above is the content of "what is the use of CURRENT_TIMESTAMP in MySQL". After the study of this article, I believe you have a deeper understanding of the use of CURRENT_TIMESTAMP in MySQL, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Version mode time question learning content situation data date type error update difference as we all know global parameter variable only field is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Redmi Docker MariaDB Shulou Technology