How to use CURRENT_TIMESTAMP in MySQL

How to use CURRENT_TIMESTAMP in MySQL

Use of CURRENT_TIMESTAMP

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

For example:

ALTER TABLE t_user ADD update_time DATETIME DEFAULT CURRENT_TIMESTAMP

In versions prior to 5.6, when CURRENT_TIMESTAMP is used as the default value, the following error will occur

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

Prior to MySQL 5.6.5, DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP only applies to TIMESTAMP, and at most one TIMESTAMP field in a table is allowed to use this feature. As of MySQL 5.6.5, DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP applies to both TIMESTAMP and DATETIME columns, and to an unlimited number of columns.

timestamp using CURRENT_TIMESTAMP reports an error

The project has the following error:

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 was found that the problem was caused by the global variable "explicit_defaults_for_timestamp" in different versions of MySQL database.

-- View the default value of explicit_defaults_for_timestamp SHOW GLOBAL VARIABLES LIKE "explicit_defaults_for_timestamp"; 

-- Modify the default value of explicit_defaults_for_timestamp SET @@global.explicit_defaults_for_timestamp=OFF;

When the parameter value is "ON":

insert image description here

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of TIMESTAMP usage in MySQL
  • Analyzing the MySql CURRENT_TIMESTAMP function by example
  • Interpretation of the method of setting multiple TimeStamps in MySQL
  • mysql data type TIMESTAMP

<<:  Detailed explanation of using CSS3's var() to change scss variable values ​​at runtime

>>:  Example code for implementing page floating box based on JS

Recommend

MySQL character set viewing and modification tutorial

1. Check the character set 1. Check the MYSQL dat...

MySQL Server IO 100% Analysis and Optimization Solution

Preface During the stress test, if the most direc...

Directory permissions when creating a container with Docker

When I was writing a project yesterday, I needed ...

How to delete special character file names or directories in Linux

Delete a file by its inode number First use ls -i...

Best Practices for Implementing Simple Jira Projects with React+TS

A set of projects for training react+ts Although ...

Detailed tutorial on installing and configuring MySQL 5.7.20 under Centos7

1. Download the MySQL 5.7 installation package fr...

Detailed steps for installing Tomcat, MySQL and Redis with Docker

Table of contents Install Tomcat with Docker Use ...

Examples of optimistic locking and pessimistic locking in MySQL

The task of concurrency control in a database man...

Explanation of the problem of selecting MySQL storage time type

The datetime type is usually used to store time i...

Detailed tutorial on using the Prettier Code plugin in vscode

Why use prettier? In large companies, front-end d...

MySQL slow log online problems and optimization solutions

MySQL slow log is a type of information that MySQ...

CentOS 7.5 deploys Varnish cache server function

1. Introduction to Varnish Varnish is a high-perf...