Example of how to set automatic creation time and modification time in mysql

Example of how to set automatic creation time and modification time in mysql

This article describes how to set the automatic creation time and modification time of MySQL. Share with you for your reference, the details are as follows:

The first one is defined by ddl

CREATE TABLE `course` (
 `course` varchar(255) DEFAULT NULL,
 `user` varchar(255) DEFAULT NULL,
 `score` int(11) DEFAULT NULL,
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time',
 `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Update time',
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

The second method is to modify the table design directly through the tool

The third method is to modify the fields through SQL statements.

-- Modify create_time to set the default time CURRENT_TIMESTAMP 
ALTER TABLE `course`
MODIFY COLUMN `create_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Creation time';

-- Add update_time and set the default time to CURRENT_TIMESTAMP. Set the update time to ON UPDATE CURRENT_TIMESTAMP. 
ALTER TABLE `course`
ADD COLUMN `update_time` datetime NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Update time';

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL transaction operation skills", "MySQL stored procedure skills", "MySQL database lock related skills summary" and "MySQL common function summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • Analyze the method of setting the current time as the default value in MySQL
  • mysql sets the default time value
  • An in-depth summary of MySQL time setting considerations

<<:  Vue.js manages the encapsulation of background table components

>>:  Specific use of Linux man command

Recommend

js object-oriented method to achieve drag effect

This article shares the specific code for impleme...

CentOS 7 Forgot Password Solution Process Diagram

need Whether it is a Windows system or a Linux sy...

MySQL 5.7.21 installation and configuration tutorial

The simple installation configuration of mysql5.7...

How to export CSV file with header in mysql

Refer to the official document http://dev.mysql.c...

Reasons and solutions for not being able to detect array changes in Vue2

Table of contents Workaround Why can't I moni...

How to add abort function to promise in JS

Table of contents Overview Promise Race Method Re...

Vue implements a simple timer component

When doing a project, it is inevitable to encount...

About CSS floating and canceling floating

Definition of Float Sets the element out of the n...

Tips for turning pixels into comprehensive brand experiences

Editor: This article discusses the role that inte...

MySQL PXC builds a new node with only IST transmission (recommended)

Demand scenario: The existing PXC environment has...

MySQL data aggregation and grouping

We often need to summarize data without actually ...