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

Detailed explanation of the correct way to open em in CSS

Why do we say “usually 1em=16px”? The default tex...

Detailed tutorial on using cmake to compile and install mysql under linux

1. Install cmake 1. Unzip the cmake compressed pa...

Combining XML and CSS styles

student.xml <?xml version="1.0" enco...

Implementation of MySQL GRANT user authorization

Authorization is to grant certain permissions to ...

Solution to mysql server 5.5 connection failure

The solution to the problem that mysql cannot be ...

A solution to the abnormal exit of Tomcat caused by semaphore

I'm playing with big data recently. A friend ...

Understanding MySQL index pushdown in five minutes

Table of contents What is index pushdown? The pri...

img usemap attribute China map link

HTML img tag: defines an image to be introduced in...

How to add Nginx proxy configuration to allow only internal IP access

location / { index index.jsp; proxy_next_upstream...

Detailed Analysis of Explain Execution Plan in MySQL

Preface How to write efficient SQL statements is ...

Real-time refresh of long connection on Vue+WebSocket page

Recently, the Vue project needs to refresh the da...