Detailed explanation of TIMESTAMP (timestamp) usage of mysql

Most of the content comes from:  mysql's TIMESTAMP (timestamp) usage details

1. Variants of TIMESTAMP

TIMESTAMP timestamps can have multiple different characteristics when they are created, such as:

1. Refresh this data column when creating new records and modifying existing records:

TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

2. Set this field to the current time when creating a new record, but do not refresh it when you modify it later:

TIMESTAMP DEFAULT CURRENT_TIMESTAMP

3. Set this field to 0 when creating a new record, and refresh it when modifying it later:

TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

4. Set this field to the given value when creating a new record, and refresh it when you modify it later:

TIMESTAMP DEFAULT ‘yyyy-mm-dd hh:mm:ss' ON UPDATE CURRENT_TIMESTAMP

MySQL currently does not support the default of a column as a function. If you achieve the function that the default value of a column is the current update date and time, you can use the TIMESTAMP column type. The TIMESTAMP column type is described in detail below (I reproduced this Original blog written in 2014)

That was before, now it can! (I use mysql 5.7)

CURRENT_TIMESTAMP can also be written in function form

 

Second, the TIMESTAMP column type

TIMESTAMP values ​​can be from the beginning of sometime in 1970 until the year 2037, with a precision of one second, and the value is displayed as a number.
The format of the display size of the TIMESTAMP value is shown in the following table:

The "full" TIMESTAMP format is 14 bits, but TIMESTAMP columns can also be created with shorter display sizes, the most common display sizes being 6, 8, 12, and 14.
You can specify an arbitrary display size when creating a table, but defining a column length of 0 or greater than 14 will force a column length of 14.
Odd-valued sizes with column lengths ranging from 1 to 13 are forced to the next larger even number.

All TIMESTAMP columns have the same storage size, using the full precision (14 bits) of the specified epoch time value to store legal values ​​regardless of display size. Invalid date, will be forced to 0 to store

This has several implications:

1. Although you define the column TIMESTAMP(8) when you create the table, the TIMESTAMP column actually saves 14-bit data (including the year, month, day, hour, minute, and second) when you insert and update data, but when you query What MySQL returns to you is 8-digit year, month, and day data. If you use ALTER TABLE to widen a narrow TIMESTAMP column, previously "hidden" information will be displayed.

2. Also, shrinking a TIMESTAMP column does not cause information to be lost, except that it feels like less information is displayed when the value is displayed.

3. Although TIMESTAMP values ​​are stored with full precision, the only function that directly manipulates the stored value is UNIX_TIMESTAMP(); since MySQL returns the column value of a TIMESTAMP column is the value that has been formatted for retrieval, this means that you may not be able to use a Some functions to operate on TIMESTAMP columns (such as HOUR() or SECOND()), unless the relevant part of the TIMESTAMP value is included in the formatted value.
For example, the HH portion of a TIMESTAMP column will only be displayed if a TIMESTAMP column is defined above TIMESTAMP(10), so using HOUR() on a shorter TIMESTAMP value will produce an unpredictable result.

4. An illegal TIMESTAMP value is converted to a "zero" value of the appropriate type (00000000000000). (DATETIME, DATE as well)

3. Beware of certain pitfalls when specifying date values:

1. The loose format that allows specifying a value as a string can be spoofed. For example, the value '10:11:12' might look like a time value because of the use of the ':' separator, but if used in a date, the context would be interpreted as '2010-11-12' as a year. The value '10:45:15' will be converted to '0000-00-00' because '45' is not a valid month.
 
2. The year value specified with 2 digits is ambiguous because the century is unknown. MySQL interprets 2-digit year values ​​using the following rules: Year values
​​in the range 00-69 are converted to 2000-2069. Year values ​​in the range 70-99 are transformed to 1970-1999.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324434861&siteId=291194637