How to add default time to a field in MySQL

How to add default time to a field in MySQL

Date type differences and uses

MySQL has five date types: date, time, year, datetime, and timestamp.

type byte Format use Whether to support setting system default values
date 3 YYYY-MM-DD Date Value Not supported
time 3 HH:MM:SS Time value or duration Not supported
year 1 YYYY years Not supported
datetime 8 YYYY-MM-DD HH:MM:SS Mixed date and time values Not supported
timestamp 4 YYYYMMDD HHMMSS Mixed date and time, can be used as timestamp support

Application scenarios:

  • In the data table, to record when each piece of data was created, the application does not need to record it specifically, but the data database obtains the current time and automatically records the creation time;
  • In the database, to record when each piece of data was modified, the application does not need to record it specifically, but the data database obtains the current time and automatically records the modification time;

Implementation:

  • Set the field type to TIMESTAMP
  • Set the default value to CURRENT_TIMESTAMP

Example application:

MySQL script implementation use case

`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time',
`update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Update time',
`datalevel` tinyint(1) DEFAULT '1' COMMENT 'Has it been deleted (0 deleted/1 normal)',

ALTER TABLE table_name
ADD COLUMN create_time datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time';
ALTER TABLE table_name
ADD COLUMN update_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Update time';
ALTER TABLE table_name
ADD COLUMN datalevel tinyint(1) DEFAULT '1' COMMENT 'Has it been deleted (0 deleted/1 normal)';

MySQL creates a normal index

ALTER TABLE projectfile ADD INDEX (fileuploadercode, projectid);

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • MySQL table field setting default value (graphic tutorial and pay attention to details)
  • Add a field to the table in the MySQL command line (field name, whether it is empty, default value)

<<:  Analysis of the project process in idea packaging and uploading to cloud service

>>:  Analysis of the method of setting up scheduled tasks in mysql

Recommend

Monitor the size change of a DOM element through iframe

A common problem encountered during the developme...

5 VueUse libraries that can speed up development (summary)

Table of contents What utilities does VueUse have...

Detailed explanation of tinyMCE usage and experience

Detailed explanation of tinyMCE usage initializat...

How to use Axios asynchronous request API in Vue

Table of contents Setting up a basic HTTP request...

Summary of common optimization operations of MySQL database (experience sharing)

Preface For a data-centric application, the quali...

Html+CSS drawing triangle icon

Let’s take a look at the renderings first: XML/HT...

JavaScript to achieve magnifying glass effect

This article shares the specific code for JavaScr...

Key features of InnoDB - insert cache, write twice, adaptive hash index details

The key features of the InnoDB storage engine inc...

React+Amap obtains latitude and longitude in real time and locates the address

Table of contents 1. Initialize the map 2. Map Po...

Web Design: Script Materials Reconstruct User Experience

<br />Original text: http://blog.rexsong.com...

How to connect to MySQL database using Node-Red

To connect Node-red to the database (mysql), you ...

Introduction to MySQL isolation level, lock and MVCC

This article aims to clarify the relationship bet...

A brief discussion on event-driven development in JS and Nodejs

Table of contents Event-driven and publish-subscr...

Apache Spark 2.0 jobs take a long time to finish when they are finished

Phenomenon When using Apache Spark 2.x, you may e...