How to set the field to automatically fill the current time and modification time in Mysql and Navicat.

​This article explains how the creation time and modification time can be automatically filled by functions, and demonstrates the following ways to realize the column auto-fill creation time and modification time:

  • When creating a table, set the field as an auto-update time column.
  • Added new field as auto update time column.
  • Update an existing field as an auto-update time column.
  • Set the field as an automatic update time column through navicat.

Function explanation:

CURRENT_TIMESTAMPIndicates that when a new row is inserted, the column will be automatically set to the current time. After the default value is set for create_time CURRENT_TIMESTAMP, the current time will be automatically filled in the create_time field when new data is inserted.
ON UPDATE CURRENT_TIMESTAMPIndicates that when the data row is updated, this column will be automatically set to the current time. After update_time is set to update the data, the current time will be automatically filled in the update_time field.

Note: Both functions can pass parameters, which can pass numbers from 0 to 6 to indicate the accuracy of the time, for example, CURRENT_TIMESTAMP(3)it means that accurate to milliseconds. When setting, the time accuracy of the function and the field must be consistent, otherwise an error will be reported: ERROR 1067 (42000): Invalid default value for 'tmt'.

Supported field types:

MySQL 5.6.5After that, both TIMESTAMPthe and DATETIMEcolumns support automatic update, and one table can set multiple automatic update columns.
MySQL 5.6.5Previously, only TIMESTAMPauto-update was supported, and each table could only have one auto-update time column.

Method to realize:

Set the field to automatically update the time column when creating the table:

CREATE TABLE table_name (
    id INT NOT NULL AUTO_INCREMENT,
    data VARCHAR(255) NOT NULL,
    create_time datetime DEFAULT CURRENT_TIMESTAMP,
    update_time datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

Add a new field as an auto-update time column:

ALTER TABLE `table_name`
ADD COLUMN `create_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间';
ADD COLUMN `update_time ` datetime NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间';

Update an existing field to an auto-update time column:

ALTER TABLE `table_name`
MODIFY COLUMN `create_time` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间';
MODIFY COLUMN `update_time ` datetime NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间';

Set the field as an automatic update time column through navicat:

Write the default value after selecting the column CURRENT_TIMESTAMP. The option below the default value means whether the current column should update the current column time when the content of the data row is updated.
insert image description here

Summarize:

Benefits: There is no need to rely on the business update time, all db operations will be automatically recorded, which is convenient for troubleshooting.
Insufficient: There may be a time difference between the database server and the business server, resulting in a difference between the time of business changes and the database timestamp, which brings obstacles to actual maintenance and use. The server time can only be calibrated as much as possible, but this problem cannot be absolutely avoided.

Guess you like

Origin blog.csdn.net/TangBoBoa/article/details/129958447