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

How to deploy k8s in docker

K8s k8s is a cluster. There are multiple Namespac...

Robots.txt detailed introduction

Robots.txt is a plain text file in which website ...

Mysql 5.7.17 winx64 installation tutorial on win7

Software version and platform: MySQL-5.7.17-winx6...

CSS3 flip card number sample code

I received a task from the company today, and the...

Common functions of MySQL basics

Table of contents 1. Common function classificati...

A brief discussion on the design of Tomcat multi-layer container

Table of contents Container Hierarchy The process...

How to use js to communicate between two html windows

Scenario: When page A opens page B, after operati...

The difference between html block-level tags and inline tags

1. Block-level element: refers to the ability to e...

Docker uses the Prune command to clean up the none image

Table of contents The creation and confusion of n...

Application nesting of HTML ul unordered tables

Application nesting of unordered lists Copy code T...

Various front-end printing methods of web: CSS controls web page printing style

CSS controls the printing style of web pages : Use...

Two ways to use react in React html

Basic Use <!DOCTYPE html> <html lang=&qu...

8 Reasons Why You Should Use Xfce Desktop Environment for Linux

For several reasons (including curiosity), I star...

In-depth analysis of MySQL 8.0 redo log

Table of contents Preface Generation of redo log ...