Rechercher dans le manuel MySQL
11.3.4 Automatic Initialization and Updating for TIMESTAMP and DATETIME
TIMESTAMP
and
DATETIME
columns can be
automatically initializated and updated to the current date and
time (that is, the current timestamp).
For any TIMESTAMP
or
DATETIME
column in a table, you
can assign the current timestamp as the default value, the
auto-update value, or both:
An auto-initialized column is set to the current timestamp for inserted rows that specify no value for the column.
An auto-updated column is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value. An auto-updated column remains unchanged if all other columns are set to their current values. To prevent an auto-updated column from updating when other columns change, explicitly set it to its current value. To update an auto-updated column even when other columns do not change, explicitly set it to the value it should have (for example, set it to
CURRENT_TIMESTAMP
).
In addition, if the
explicit_defaults_for_timestamp
system variable is disabled, you can initialize or update any
TIMESTAMP
(but not
DATETIME
) column to the current date and time
by assigning it a NULL
value, unless it has
been defined with the NULL
attribute to
permit NULL
values.
To specify automatic properties, use the DEFAULT
CURRENT_TIMESTAMP
and ON UPDATE
CURRENT_TIMESTAMP
clauses in column definitions. The
order of the clauses does not matter. If both are present in a
column definition, either can occur first. Any of the synonyms
for CURRENT_TIMESTAMP
have the
same meaning as
CURRENT_TIMESTAMP
. These are
CURRENT_TIMESTAMP()
,
NOW()
,
LOCALTIME
,
LOCALTIME()
,
LOCALTIMESTAMP
, and
LOCALTIMESTAMP()
.
Use of DEFAULT CURRENT_TIMESTAMP
and
ON UPDATE CURRENT_TIMESTAMP
is specific to
TIMESTAMP
and
DATETIME
. The
DEFAULT
clause also can be used to specify a
constant (nonautomatic) default value (for example,
DEFAULT 0
or DEFAULT '2000-01-01
00:00:00'
).
The following examples use DEFAULT 0
, a
default that can produce warnings or errors depending on
whether strict SQL mode or the
NO_ZERO_DATE
SQL mode is
enabled. Be aware that the
TRADITIONAL
SQL mode
includes strict mode and
NO_ZERO_DATE
. See
Section 5.1.11, “Server SQL Modes”.
TIMESTAMP
or
DATETIME
column definitions can
specify the current timestamp for both the default and
auto-update values, for one but not the other, or for neither.
Different columns can have different combinations of automatic
properties. The following rules describe the possibilities:
With both
DEFAULT CURRENT_TIMESTAMP
andON UPDATE CURRENT_TIMESTAMP
, the column has the current timestamp for its default value and is automatically updated to the current timestamp.With a
DEFAULT
clause but noON UPDATE CURRENT_TIMESTAMP
clause, the column has the given default value and is not automatically updated to the current timestamp.The default depends on whether the
DEFAULT
clause specifiesCURRENT_TIMESTAMP
or a constant value. WithCURRENT_TIMESTAMP
, the default is the current timestamp.- );
With a constant, the default is the given value. In this case, the column has no automatic properties at all.
With an
ON UPDATE CURRENT_TIMESTAMP
clause and a constantDEFAULT
clause, the column is automatically updated to the current timestamp and has the given constant default value.With an
ON UPDATE CURRENT_TIMESTAMP
clause but noDEFAULT
clause, the column is automatically updated to the current timestamp but does not have the current timestamp for its default value.The default in this case is type dependent.
TIMESTAMP
has a default of 0 unless defined with theNULL
attribute, in which case the default isNULL
.- );
DATETIME
has a default ofNULL
unless defined with theNOT NULL
attribute, in which case the default is 0.
TIMESTAMP
and
DATETIME
columns have no
automatic properties unless they are specified explicitly, with
this exception: If the
explicit_defaults_for_timestamp
system variable is disabled, the first
TIMESTAMP
column has both
DEFAULT CURRENT_TIMESTAMP
and ON
UPDATE CURRENT_TIMESTAMP
if neither is specified
explicitly. To suppress automatic properties for the first
TIMESTAMP
column, use one of
these strategies:
Enable the
explicit_defaults_for_timestamp
system variable. In this case, theDEFAULT CURRENT_TIMESTAMP
andON UPDATE CURRENT_TIMESTAMP
clauses that specify automatic initialization and updating are available, but are not assigned to anyTIMESTAMP
column unless explicitly included in the column definition.Alternatively, if
explicit_defaults_for_timestamp
is disabled, do either of the following:Define the column with a
DEFAULT
clause that specifies a constant default value.Specify the
NULL
attribute. This also causes the column to permitNULL
values, which means that you cannot assign the current timestamp by setting the column toNULL
. AssigningNULL
sets the column toNULL
, not the current timestamp. To assign the current timestamp, set the column toCURRENT_TIMESTAMP
or a synonym such asNOW()
.
Consider these table definitions:
The tables have these properties:
In each table definition, the first
TIMESTAMP
column has no automatic initialization or updating.The tables differ in how the
ts1
column handlesNULL
values. Fort1
,ts1
isNOT NULL
and assigning it a value ofNULL
sets it to the current timestamp. Fort2
andt3
,ts1
permitsNULL
and assigning it a value ofNULL
sets it toNULL
.t2
andt3
differ in the default value forts1
. Fort2
,ts1
is defined to permitNULL
, so the default is alsoNULL
in the absence of an explicitDEFAULT
clause. Fort3
,ts1
permitsNULL
but has an explicit default of 0.
If a TIMESTAMP
or
DATETIME
column definition
includes an explicit fractional seconds precision value
anywhere, the same value must be used throughout the column
definition. This is permitted:
- );
This is not permitted:
- );
TIMESTAMP Initialization and the NULL Attribute
If the
explicit_defaults_for_timestamp
system variable is disabled,
TIMESTAMP
columns by default are
NOT NULL
, cannot contain
NULL
values, and assigning
NULL
assigns the current timestamp. To permit
a TIMESTAMP
column to contain
NULL
, explicitly declare 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 invalid.) 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:
- (
- );
A TIMESTAMP
column that permits
NULL
values does not
take on the current timestamp at insert time except under one of
the following conditions:
Its default value is defined as
CURRENT_TIMESTAMP
and no value is specified for the columnCURRENT_TIMESTAMP
or any of its synonyms such asNOW()
is explicitly inserted into the column
In other words, a TIMESTAMP
column defined to permit NULL
values
auto-initializes only if its definition includes
DEFAULT CURRENT_TIMESTAMP
:
If the TIMESTAMP
column permits
NULL
values but its definition does not
include DEFAULT CURRENT_TIMESTAMP
, you must
explicitly insert a value corresponding to the current date and
time. Suppose that tables t1
and
t2
have these definitions:
To set the TIMESTAMP
column in
either table to the current timestamp at insert time, explicitly
assign it that value. For example:
If the
explicit_defaults_for_timestamp
system variable is enabled,
TIMESTAMP
columns permit
NULL
values only if declared with the
NULL
attribute. Also,
TIMESTAMP
columns do not permit
assigning NULL
to assign the current
timestamp, whether declared with the NULL
or
NOT NULL
attribute. To assign the current
timestamp, set the column to
CURRENT_TIMESTAMP
or a synonym
such as NOW()
.
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 26/06/2006 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/mysql-rf-timestamp-initialization.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.