MySQL table field time setting default value

MySQL table field time setting default value

Application Scenario

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

Get the current time in the database

  • oracle: select sysdate from dual;
  • sqlserver: select getdate();
  • mysql: select sysdate(); select now();

The difference between NOW() and SYSDATE() in MySQL

NOW() takes the time when the statement starts executing, and SYSDATE() takes the dynamic real-time time.

Because NOW() is taken from a MySQL variable "TIMESTAMP", and this variable is set when the statement starts executing, it will not change during the entire statement execution process.

Execute the following example to understand:

SELECT NOW(),SYSDATE(),SLEEP(3),NOW(),SYSDATE()

First, NOW() and SYSDATE() are queried, then sleep for 3 seconds, and then NOW() and SYSDATE() are queried again. The results are as follows:

Implementation

  1. Set the field type to TIMESTAMP.
  2. Set the default value to CURRENT_TIMESTAMP.

Example Application

Create a table time with the primary key id and one field date, which defaults to the current system time:

CREATE TABLE time(
id INT PRIMARY KEY,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

Insert a piece of data:

INSERT INTO time(id) VALUES(1);

Query results:

This is the end of this article about setting default values ​​for MySQL table field time. For more information about default values ​​for MySQL field time, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to set the default value of a MySQL field
  • How to assign default values ​​to fields when querying MySQL
  • Add a field to the table in the MySQL command line (field name, whether it is empty, default value)
  • MySQL table field setting default value (graphic tutorial and pay attention to details)

<<:  Problems installing TensorRT in docker container

>>:  Elements of user experience or elements of web design

Recommend

Vue realizes the sliding cross effect of the ball

This article example shares the specific code of ...

How to view and set the mysql time zone

1. Check the database time zone show variables li...

WeChat applet development form validation WxValidate usage

I personally feel that the development framework ...

Detailed explanation of creating, calling and managing MySQL stored procedures

Table of contents Introduction to stored procedur...

Detailed explanation of Vue Notepad example

This article example shares the specific code of ...

In-depth understanding of Vue's data responsiveness

Table of contents 1. ES syntax getter and setter ...

Pure CSS implementation of radio and checkbox effect example

radio-and-checkbox Pure CSS to achieve radio and ...

How to install docker on ubuntu20.04 LTS

Zero: Uninstall old version Older versions of Doc...

How to use axios to filter multiple repeated requests in a project

Table of contents 1. Introduction: In this case, ...

Vue implements video upload function

This article example shares the specific code of ...

Summary of seven sorting algorithms implemented in JavaScript (recommended!)

Table of contents Preface Bubble Sort Basic Algor...

How to implement adaptive container with equal aspect ratio using CSS

When developing a mobile page recently, I encount...

Linux system file sharing samba configuration tutorial

Table of contents Uninstall and install samba Cre...

innodb_flush_method value method (example explanation)

Several typical values ​​of innodb_flush_method f...