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

30 excellent examples of color matching in web design

Today, this article has collected 30 excellent cas...

Summary of commonly used commands for docker competition submission

Log in to your account export DOCKER_REGISTRY=reg...

Implementation of docker-compose deployment project based on MySQL8

1. First, create the corresponding folder accordi...

C# implements MySQL command line backup and recovery

There are many tools available for backing up MyS...

Detailed explanation of the relationship between Vue and VueComponent

The following case reviews the knowledge points o...

Implementation of WeChat applet message push in Nodejs

Select or create a subscription message template ...

jQuery manipulates cookies

Copy code The code is as follows: jQuery.cookie =...

MySQL 8.0.11 installation and configuration method graphic tutorial

The installation and configuration methods of MyS...

WeChat applet uniapp realizes the left swipe to delete effect (complete code)

WeChat applet uniapp realizes the left swipe to d...

This article will show you how to use Vue 3.0 responsive

Table of contents Use Cases Reactive API related ...

A brief discussion on three methods of asynchronous replication in MySQL 8.0

In this experiment, we configure MySQL standard a...

Detailed tutorial on installing ElasticSearch 6.x in docker

First, pull the image (or just create a container...