"MySQL"-Date and Time Type@20210225

For DATE and DATETIME range descriptions, "supported" means that although earlier values ​​may work properly, it is not guaranteed.

Types of Size (B) range Display format use
YEAR[(4)] 1 0000,1901 ~ 2155 YYYY Year value
TIME[(fsp)] 3 -838:59:59 ~ 838:59:59 HH:MM:SS[.fraction] Time value or duration. Allows you to insert values ​​using strings or numbers.
TIMESTAMP[(fsp)] 4 UTC: 1970-01-01 00:00:01.000000 ~ 2038-01-19 03:14:07.999999 YYYYMMDD HHMMSS Mixed date and time values, timestamp
DATETIME[(fsp)] 8 1000-01-01 00:00:00.000000 ~ 9999-12-31 23:59:59.999999 YYYY-MM-DD HH:MM:SS[.fraction] Mixed date and time values. Allows you to insert values ​​using strings or numbers.
DATE 4 1000-01-01 ~ 9999-12-31 YYYY-MM-DD Date value. Allows you to insert values ​​using strings or numbers.

In MySQL, TIME, DATETIME, and TIMESTAMP support fractional seconds, and the precision can be as high as microseconds (6 digits).

For fsp
to define a column containing fractional seconds, use the syntax type_name(fsp), where type_name is TIME, DATETIME or TIMESTAMP, and fsp is the fractional second precision. The fsp value (if given) must be between 0 and 6. A value of 0 means that there is no decimal part. If omitted, the default precision is 0. (different from the standard SQL default value of 6, used for compatibility with previous MySQL versions).

Any TIMESTAMP or DATETIME column in the table can have automatic initialization and update properties. You can use the DEFAULT and ON UPDATE clauses to specify the current date and time for automatic initialization and update to the DATETIME column .

About TIMESTAMP The
TIMESTAMP value is stored as the number of seconds that have passed since the epoch ('1970-01-01 00:00:00' UTC).
TIMESTAMP cannot represent the value "1970-01-01 00:00:00" because this is equivalent to 0 seconds from the beginning of the epoch, and the value 0 is reserved to mean "0000-00-00 00:00:00".
The way MySQL handles TIMESTAMP definitions depends on the system variable explicit_defaults_for_timestamp.
If explicit_defaults_for_timestamp is enabled, the DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP attributes will not be automatically assigned to any TIMESTAMP column. They must be explicitly included in the column definition. Moreover, any TIMESTAMP that is not explicitly declared as NOT NULL allows NULL values.
If explicit_defaults_for_timestamp is disabled, MySQL handles it as follows:

	* Unless otherwise specified, the first TIMESTAMP column in the table is defined to be automatically set to the date and time of the most recent modification, if the value is not explicitly specified. This allows TIMESTAMP to be used to record the timestamp of an INSERT or UPDATE operation. You can also set the TIMESTAMP column to the current date and time by assigning it to a NULL value, unless the NULL value has been defined using the NULL attribute.
	* You can use the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP column definition clauses to specify automatic initialization and update to the current date and time. By default, the first TIMESTAMP column has these attributes, as described earlier. However, any TIMESTAMP column in the table can be defined as having these attributes.

About YEAR type The
YEAR(2) data type has been deprecated, and its support has been removed in MySQL 5.7.5. To convert the YEAR(2) column to YEAR(4), you can check Section 11.3.4, "YEAR(2) Limitations and Migrating to YEAR(4)"

Other considerations
SUM() and AVG() aggregate functions are not applicable to time values, because they convert the value to a number and discard everything after the first non-digit character. To solve this problem, you should first convert the time to a value, then perform an aggregation operation, and then convert it to a time value:

SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(time_col))) FROM tbl_name;
SELECT FROM_DAYS(SUM(TO_DAYS(date_col))) FROM tbl_name;

You can run MySQL server with MAXDB SQL mode enabled. In this case, TIMESTAMP is the same as DATETIME. If this mode is enabled when the table is created, the TIMESTAMP column will be created as a DATETIME column. Therefore, these columns use the DATETIME display format, have the same value range, and are not automatically initialized or updated to the current date and time. See Section 5.1.8, “Server SQL Modes”

Field default value

You can set default values ​​for time and date fields.

But this is related to the specific version, and different versions behave differently.

MySQL 5.5

The default value of the date column cannot be set to a function, such as NOW() or CURRENT_DATE, etc.

But you can use CURRENT_TIMESTAMP as the default value for the TIMESTAMP column.

MySQL 5.6.5

The TIMESTAMP and DATETIME columns can be automatically initialized and updated to the current time and date.

There is another usage: ON UPDATE, when the column is updated, it is automatically updated to the current date:

CREATE TABLE t1 (
  ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  dt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

references

Can't default date to CURRENT_TIMESTAMP in MySQL 5.5
How do you set a default value for a MySQL Datetime column?
11.1.2 Date and Time Type Overview
11.3.5 Automatic Initialization and Updating for TIMESTAMP and DATETIME

Guess you like

Origin blog.csdn.net/u013670453/article/details/114106529