mysql implements adding time automatically adding and updating time automatically updating operation

mysql implements adding time automatically adding and updating time automatically updating operation

Time fields are often used in database usage. Commonly used ones are creation time and update time.

However, when using it, you want the creation time to be automatically set to the current time when creating, and the update time to be automatically updated to the current time when updating.

Create table stu

CREATE TABLE `stu` (
'id' int NOT NULL AUTO_INCREMENT,
'createTime' timestamp DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time',
'moditiyTime' timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Update time',
PRIMARY KEY ('id'));

Set the current time when creating

DEFAULT CURRENT_TIMESTAMP

When updating, set the update time to the current time

DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

Supplement: MySQL adds default time to the field (insert time)

Application scenarios:

1. 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;

2. 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:

1. Set the field type to TIMESTAMP

2. Set the default value to CURRENT_TIMESTAMP

Example application:

1. MySQL script implementation use case

–Add CreateTime to set the default time CURRENT_TIMESTAMP

ALTER TABLE table_name
ADD COLUMN CreateTime datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time';

–Modify CreateTime to set the default time CURRENT_TIMESTAMP

ALTER TABLE table_name
MODIFY COLUMN CreateTime datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time';

– Add UpdateTime Set the default time CURRENT_TIMESTAMP Set the update time to ON UPDATE CURRENT_TIMESTAMP

ALTER TABLE table_name
ADD COLUMN UpdateTime timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Creation time';

– Modify UpdateTime and set the default time to CURRENT_TIMESTAMP and set the update time to ON UPDATE CURRENT_TIMESTAMP

ALTER TABLE table_name
MODIFY COLUMN UpdateTime timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Creation time';

2. MySQL tool settings

MySQL automatically manages and maintains database time consistency.

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • MySQL timestamp automatic update time sharing
  • MySQL database automatically adds creation time and update time

<<:  Example code for implementing a QR code scanning box with CSS

>>:  HTML page jump code

Recommend

How to use sed command to efficiently delete specific lines of a file

Preface Normally, if we want to delete certain li...

Understanding and using React useEffect

Table of contents Avoid repetitive rendering loop...

How to display div on object without being blocked by object animation

Today I made a menu button. When you move the mous...

Linux system repair mode (single user mode)

Table of contents Preface 1. Common bug fixes in ...

How to create a trigger in MySQL

This article example shares the specific code for...

About the correct way to convert time in js when importing excel

Table of contents 1. Basics 2. Problem Descriptio...

What I learned while building my own blog

<br />In one year of blogging, I have person...

Detailed explanation of JavaScript Proxy object

Table of contents 1. What is Proxy? 2. How to use...

MySQL slow query: Enable slow query

1. What is the use of slow query? It can record a...

Solution to Mysql binlog log file being too large

Table of contents 1. Related binlog configuration...

How to create https using nginx and Tencent Cloud free certificate

I have been studying how to get https. Recently I...

How to make ApacheBench support multi-url

Since the standard ab only supports stress testin...