TIMESTAMP columns are displayed
in the same format as DATETIME
columns. In other words, the display width is fixed at 19
characters, and the format is 'YYYY-MM-DD
HH:MM:SS'.
TIMESTAMP values are converted
from the current time zone to UTC for storage, and converted
back from UTC to the current time zone for retrieval. (This
occurs only for the TIMESTAMP
data type, not for other types such as
DATETIME.) By default, the
current time zone for each connection is the server's time.
The time zone can be set on a per-connection basis, as
described in Section 9.6, “MySQL Server Time Zone Support”. As long as
the time zone setting remains constant, you get back the same
value you store. If you store a
TIMESTAMP value, and then
change the time zone and retrieve the value, the retrieved
value is different from the value you stored. This occurs
because the same time zone was not used for conversion in both
directions. The current time zone is available as the value of
the time_zone system
variable.
The TIMESTAMP data type offers
automatic initialization and updating. You can choose whether
to use these properties and which column should have them:
For one
TIMESTAMPcolumn in a table, you can assign the current timestamp as the default value and the auto-update value. It is possible to have the current timestamp be the default value for initializing the column, for the auto-update value, or both. It is not possible to have the current timestamp be the default value for one column and the auto-update value for another column.Any single
TIMESTAMPcolumn in a table can be used as the one that is initialized to the current date and time, or updated automatically. This need not be the firstTIMESTAMPcolumn.If a
DEFAULTvalue is specified for the firstTIMESTAMPcolumn in a table, it is not ignored. The default can beCURRENT_TIMESTAMPor a constant date and time value.In a
CREATE TABLEstatement, the firstTIMESTAMPcolumn can be declared in any of the following ways:With both
DEFAULT CURRENT_TIMESTAMPandON UPDATE CURRENT_TIMESTAMPclauses, the column has the current timestamp for its default value, and is automatically updated.With neither
DEFAULTnorON UPDATEclauses, it is the same asDEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.With a
DEFAULT CURRENT_TIMESTAMPclause and noON UPDATEclause, the column has the current timestamp for its default value but is not automatically updated.With no
DEFAULTclause and with anON UPDATE CURRENT_TIMESTAMPclause, the column has a default of 0 and is automatically updated.With a constant
DEFAULTvalue, the column has the given default and is not automatically initialized to the current timestamp. If the column also has anON UPDATE CURRENT_TIMESTAMPclause, it is automatically updated; otherwise, it has a constant default and is not automatically updated.
In other words, you can use the current timestamp for both the initial value and the auto-update value, or either one, or neither. (For example, you can specify
ON UPDATEto enable auto-update without also having the column auto-initialized.) The following column definitions demonstrate each possibility:Auto-initialization and auto-update:
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
Auto-initialization only:
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
Auto-update only:
ts TIMESTAMP DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP
Neither:
ts TIMESTAMP DEFAULT 0
To specify automatic default or updating for a
TIMESTAMPcolumn other than the first one, you must suppress the automatic initialization and update behaviors for the firstTIMESTAMPcolumn by explicitly assigning it a constantDEFAULTvalue (for example,DEFAULT 0orDEFAULT '2003-01-01 00:00:00'). Then, for the otherTIMESTAMPcolumn, the rules are the same as for the firstTIMESTAMPcolumn, except that if you omit both of theDEFAULTandON UPDATEclauses, no automatic initialization or updating occurs.Example:
CREATE TABLE t ( ts1 TIMESTAMP DEFAULT 0, ts2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP);CURRENT_TIMESTAMPor any of its synonyms (CURRENT_TIMESTAMP(),NOW(),LOCALTIME,LOCALTIME(),LOCALTIMESTAMP, orLOCALTIMESTAMP()) can be used in theDEFAULTandON UPDATEclauses. They all mean “the current timestamp.” (UTC_TIMESTAMPis not permitted. Its range of values does not align with those of theTIMESTAMPcolumn anyway unless the current time zone isUTC.)The order of the
DEFAULTandON UPDATEattributes does not matter. If bothDEFAULTandON UPDATEare specified for aTIMESTAMPcolumn, either can precede the other. For example, these statements are equivalent:CREATE TABLE t (ts TIMESTAMP); CREATE TABLE t (ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP); CREATE TABLE t (ts TIMESTAMP ON UPDATE CURRENT_TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
The examples that use DEFAULT 0 will not
work if the NO_ZERO_DATE
SQL mode is enabled because that mode causes
“zero” date values (specified as
0, '0000-00-00, or
'0000-00-00 00:00:00') to be rejected. Be
aware that the TRADITIONAL
SQL mode includes
NO_ZERO_DATE.
TIMESTAMP columns are
NOT NULL by default, cannot contain
NULL values, and assigning
NULL assigns the current timestamp.
However, a TIMESTAMP column can
be permitted to contain NULL by declaring
it with the NULL attribute. In this case,
the default value also becomes NULL unless
overridden with a DEFAULT clause that
specifies a different default value. DEFAULT
NULL can be used to explicitly specify
NULL as the default value. (For a
TIMESTAMP column not declared
with the NULL attribute, DEFAULT
NULL is illegal.) If a
TIMESTAMP column permits
NULL values, assigning
NULL sets it to NULL,
not to the current timestamp.
The following table contains several
TIMESTAMP columns that permit
NULL values:
CREATE TABLE t ( ts1 TIMESTAMP NULL DEFAULT NULL, ts2 TIMESTAMP NULL DEFAULT 0, ts3 TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP );
Note that a TIMESTAMP column
that permits NULL values will
not take on the current timestamp except
under one of the following conditions:
Its default value is defined as
CURRENT_TIMESTAMPNOW()orCURRENT_TIMESTAMPis inserted into the column
In other words, a TIMESTAMP
column defined as NULL will auto-initialize
only if it is created using a definition such as the
following:
CREATE TABLE t (ts TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP);
Otherwise—that is, if the
TIMESTAMP column is defined to
permit NULL values but not using
DEFAULT CURRENT_TIMESTAMP, as shown
here…
CREATE TABLE t1 (ts TIMESTAMP NULL DEFAULT NULL); CREATE TABLE t2 (ts TIMESTAMP NULL DEFAULT '0000-00-00 00:00:00');
…then you must explicitly insert a value corresponding to the current date and time. For example:
INSERT INTO t1 VALUES (NOW()); INSERT INTO t2 VALUES (CURRENT_TIMESTAMP);
The MySQL server can be run with the
MAXDB SQL mode enabled.
When the server runs with this mode enabled,
TIMESTAMP is identical with
DATETIME. That is, if this
mode is enabled at the time that a table is created,
TIMESTAMP columns are created
as DATETIME columns. As a
result, such columns use
DATETIME display format, have
the same range of values, and there is no automatic
initialization or updating to the current date and time.
To enable MAXDB mode, set
the server SQL mode to MAXDB
at startup using the
--sql-mode=MAXDB server option
or by setting the global
sql_mode variable at runtime:
mysql> SET GLOBAL sql_mode=MAXDB;
A client can cause the server to run in
MAXDB mode for its own
connection as follows:
mysql> SET SESSION sql_mode=MAXDB;