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

MySQL multi-master and one-slave data backup method tutorial

Overview Operations on any one database are autom...

Example code for element multiple tables to achieve synchronous scrolling

Element UI implements multiple tables scrolling a...

jQuery achieves the effect of advertisement scrolling up and down

This article shares the specific code of jQuery t...

VMware vSphere 6.7 (ESXI 6.7) graphic installation steps

Environment: VMware VCSA 6.7 (VMware-VCSA-all-6.7...

js to achieve simple drag effect

This article shares the specific code of js to ac...

Implementation of building Kubernetes cluster with VirtualBox+Ubuntu16

Table of contents About Kubernetes Basic environm...

How to use VirtualBox to simulate a Linux cluster

1. Set up HOST on the host Macbook The previous d...

Design and implementation of Vue cascading drop-down box

Table of contents 1. Database design 2. Front-end...

Solution to the problem of passing values ​​between html pages

The first time I used the essay, I felt quite awkw...

jQuery realizes the full function of shopping cart

This article shares the specific code of jQuery t...

Vue batch update dom implementation steps

Table of contents Scene Introduction Deep respons...

Example of using UserMap in IMG

usemap is an attribute of the <img> tag, use...

How to implement the jQuery carousel function

This article shares the implementation code of jQ...

10 SQL statement optimization techniques to improve MYSQL query efficiency

The execution efficiency of MySQL database has a ...