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

Let's talk about Vue's mixin and inheritance in detail

Table of contents Preface Mixin Mixin Note (dupli...

Data Structure - Tree (III): Multi-way Search Tree B-tree, B+ tree

Multi-way search tree Height of a complete binary...

Detailed steps to use Redis in Docker

1. Introduction This article will show you how to...

What is HTML?

History of HTML development: HTML means Hypertext...

Simple use of Vue vee-validate plug-in

Table of contents 1. Installation 2. Import 3. De...

An article to help you understand Js inheritance and prototype chain

Table of contents Inheritance and prototype chain...

VMware installation of Centos8 system tutorial diagram (Chinese graphical mode)

Table of contents 1. Software and system image 2....

Try Docker+Nginx to deploy single page application method

From development to deployment, do it yourself Wh...

Differences between proxy_pass in two modules in nginx

1. The proxy_pass directive of the 1.ngx_stream_p...

Mini Program Development to Implement Unified Management of Access_Token

Table of contents TOKEN Timer Refresher 2. Intern...

Solve the problem of docker pull image error

describe: Install VM under Windows 10, run Docker...

A collection of common uses of HTML meta tags

What is a mata tag The <meta> element provi...

Example code for implementing fullpage.js full-screen scrolling effect with CSS

When I was studying CSS recently, I found that I ...