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

How to create, start, and stop a Docker container

1. A container is an independently running applic...

How to use the concat function in mysql

As shown below: //Query the year and month of the...

JavaScript implements mouse drag to adjust div size

This article shares the specific code of JavaScri...

Detailed examples of using JavaScript event delegation (proxy)

Table of contents Introduction Example: Event del...

VMware vSAN Getting Started Summary

1. Background 1. Briefly introduce the shared sto...

TypeScript namespace explanation

Table of contents 1. Definition and Use 1.1 Defin...

MySQL foreign key (FOREIGN KEY) usage case detailed explanation

Introduction: The disadvantages of storing all da...

How to get the maximum or minimum value of a row in sql

Original data and target data Implement SQL state...

VUE implements timeline playback component

This article example shares the specific code of ...

Java example code to generate random characters

Sample code: import java.util.Random; import java...

How to quickly deploy Redis as a Docker container

Table of contents getting Started Data storage Co...

Looping methods and various traversal methods in js

Table of contents for loop While Loop do-while lo...

The whole process of developing a Google plug-in with vue+element

Simple function: Click the plug-in icon in the up...

Disable input text box input implementation properties

Today I want to summarize several very useful HTML...