Get the App
SLTechnology News&Howtos  ›  Database  › 

A brief Analysis of how MySQL adds default time to Fields

Shulou Source: shulou.com Published: 2022-06-01 03:21:28 11月14日 Update

Differences in date types and their uses

There are five date types for MySQL, which are date, time, year, datetime, and timestamp.

Type byte format usage whether support setting system default date3YYYY-MM-DD date value does not support time3HH:MM:SS time value or duration does not support year1YYYY year does not support datetime8YYYY-MM-DD HH:MM:SS date and time mixed value does not support timestamp4YYYYMMDD HHMMSS mixed date and time, can be used as time stamp support

Application scenarios:

In the data table, it is not necessary for the application to record when each piece of data was created, but the data database obtains the current time to record the creation time automatically; in the database, to record when each piece of data was modified does not need the application to record it, but the data database acquires the current time to record the modification time automatically.

Implementation method:

Set the field type to TIMESTAMP and the default value to CURRENT_TIMESTAMP

Examples of applications:

MySQL script implementation use case

`create_ time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT 'creation time', `datalevel` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'update time', `datalevel` tinyint (1) whether DEFAULT'1' COMMENT 'has been deleted (0 deletion / 1 normal)', ALTER TABLE table_nameADD COLUMN create_time datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'creation time'; ALTER TABLE table_nameADD COLUMN update_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'update time' ALTER TABLE table_nameADD COLUMN datalevel tinyint (1) whether DEFAULT'1' COMMENT 'has been deleted (0 deletion / 1 normal)'

MySQL creates a normal index

ALTER TABLE projectfile ADD INDEX (fileuploadercode, projectid)

Summary

The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support.

Tags: Time data support date type application database content application time purpose program learning update blending field normal value scenario byte Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Xiaomi Shulou Tech Info MySQL OPPO Reno