### 11.2.5 Automatic Initialization and Updating for TIMESTAMP and DATETIME

[]()

[`TIMESTAMP`](datetime.html) and [`DATETIME`](datetime.html) columns can be automatically initializated and updated to the current date and time (that is, the current timestamp).

 For any [`TIMESTAMP`](datetime.html) or [`DATETIME`](datetime.html) 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`](date-and-time-functions.html#function_current-timestamp)).

 In addition, if the [`explicit_defaults_for_timestamp`](server-system-variables.html#sysvar_explicit_defaults_for_timestamp) system variable is disabled, you can initialize or update any [`TIMESTAMP`](datetime.html) (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`](date-and-time-functions.html#function_current-timestamp) have the same meaning as [`CURRENT_TIMESTAMP`](date-and-time-functions.html#function_current-timestamp). These are [`CURRENT_TIMESTAMP()`](date-and-time-functions.html#function_current-timestamp), [`NOW()`](date-and-time-functions.html#function_now), [`LOCALTIME`](date-and-time-functions.html#function_localtime), [`LOCALTIME()`](date-and-time-functions.html#function_localtime), [`LOCALTIMESTAMP`](date-and-time-functions.html#function_localtimestamp), and [`LOCALTIMESTAMP()`](date-and-time-functions.html#function_localtimestamp).

 Use of `DEFAULT CURRENT_TIMESTAMP` and `ON UPDATE CURRENT_TIMESTAMP` is specific to [`TIMESTAMP`](datetime.html) and [`DATETIME`](datetime.html). 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'`).

Note

 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.html#sqlmode_no_zero_date) SQL mode is enabled. Be aware that the [`TRADITIONAL`](sql-mode.html#sqlmode_traditional) SQL mode includes strict mode and [`NO_ZERO_DATE`](sql-mode.html#sqlmode_no_zero_date). See [Section 5.1.11, “Server SQL Modes”](sql-mode.html).

[`TIMESTAMP`](datetime.html) or [`DATETIME`](datetime.html) 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` and `ON UPDATE CURRENT_TIMESTAMP`, the column has the current timestamp for its default value and is automatically updated to the current timestamp.

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

* With a `DEFAULT` clause but no `ON 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 specifies `CURRENT_TIMESTAMP` or a constant value. With `CURRENT_TIMESTAMP`, the default is the current timestamp.

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

   With a constant, the default is the given value. In this case, the column has no automatic properties at all.

  ```
  CREATE TABLE t1 (
    ts TIMESTAMP DEFAULT 0,
    dt DATETIME DEFAULT 0
  );
  ```

* With an `ON UPDATE CURRENT_TIMESTAMP` clause and a constant `DEFAULT` clause, the column is automatically updated to the current timestamp and has the given constant default value.

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

* With an `ON UPDATE CURRENT_TIMESTAMP` clause but no `DEFAULT` 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`](datetime.html) has a default of 0 unless defined with the `NULL` attribute, in which case the default is `NULL`.

  ```
  CREATE TABLE t1 (
    ts1 TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,     -- default 0
    ts2 TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP -- default NULL
  );
  ```

  [`DATETIME`](datetime.html) has a default of `NULL` unless defined with the `NOT NULL` attribute, in which case the default is 0.

  ```
  CREATE TABLE t1 (
    dt1 DATETIME ON UPDATE CURRENT_TIMESTAMP,         -- default NULL
    dt2 DATETIME NOT NULL ON UPDATE CURRENT_TIMESTAMP -- default 0
  );
  ```

[`TIMESTAMP`](datetime.html) and [`DATETIME`](datetime.html) columns have no automatic properties unless they are specified explicitly, with this exception: If the [`explicit_defaults_for_timestamp`](server-system-variables.html#sysvar_explicit_defaults_for_timestamp) system variable is disabled, the *first* [`TIMESTAMP`](datetime.html) column has both `DEFAULT CURRENT_TIMESTAMP` and `ON UPDATE CURRENT_TIMESTAMP` if neither is specified explicitly. To suppress automatic properties for the first [`TIMESTAMP`](datetime.html) column, use one of these strategies:

* Enable the [`explicit_defaults_for_timestamp`](server-system-variables.html#sysvar_explicit_defaults_for_timestamp) system variable. In this case, the `DEFAULT CURRENT_TIMESTAMP` and `ON UPDATE CURRENT_TIMESTAMP` clauses that specify automatic initialization and updating are available, but are not assigned to any [`TIMESTAMP`](datetime.html) column unless explicitly included in the column definition.

* Alternatively, if [`explicit_defaults_for_timestamp`](server-system-variables.html#sysvar_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 permit `NULL` values, which means that you cannot assign the current timestamp by setting the column to `NULL`. Assigning `NULL` sets the column to `NULL`, not the current timestamp. To assign the current timestamp, set the column to [`CURRENT_TIMESTAMP`](date-and-time-functions.html#function_current-timestamp) or a synonym such as [`NOW()`](date-and-time-functions.html#function_now).

 Consider these table definitions:

```
CREATE TABLE t1 (
  ts1 TIMESTAMP DEFAULT 0,
  ts2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                ON UPDATE CURRENT_TIMESTAMP);
CREATE TABLE t2 (
  ts1 TIMESTAMP NULL,
  ts2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                ON UPDATE CURRENT_TIMESTAMP);
CREATE TABLE t3 (
  ts1 TIMESTAMP NULL DEFAULT 0,
  ts2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                ON UPDATE CURRENT_TIMESTAMP);
```

 The tables have these properties:

* In each table definition, the first [`TIMESTAMP`](datetime.html) column has no automatic initialization or updating.

* The tables differ in how the `ts1` column handles `NULL` values. For `t1`, `ts1` is `NOT NULL` and assigning it a value of `NULL` sets it to the current timestamp. For `t2` and `t3`, `ts1` permits `NULL` and assigning it a value of `NULL` sets it to `NULL`.

* `t2` and `t3` differ in the default value for `ts1`. For `t2`, `ts1` is defined to permit `NULL`, so the default is also `NULL` in the absence of an explicit `DEFAULT` clause. For `t3`, `ts1` permits `NULL` but has an explicit default of 0.

 If a [`TIMESTAMP`](datetime.html) or [`DATETIME`](datetime.html) column definition includes an explicit fractional seconds precision value anywhere, the same value must be used throughout the column definition. This is permitted:

```
CREATE TABLE t1 (
  ts TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)
);
```

 This is not permitted:

```
CREATE TABLE t1 (
  ts TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP(3)
);
```

#### TIMESTAMP Initialization and the NULL Attribute

 If the [`explicit_defaults_for_timestamp`](server-system-variables.html#sysvar_explicit_defaults_for_timestamp) system variable is disabled, [`TIMESTAMP`](datetime.html) columns by default are `NOT NULL`, cannot contain `NULL` values, and assigning `NULL` assigns the current timestamp. To permit a [`TIMESTAMP`](datetime.html) 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`](datetime.html) column not declared with the `NULL` attribute, `DEFAULT NULL` is invalid.) If a [`TIMESTAMP`](datetime.html) column permits `NULL` values, assigning `NULL` sets it to `NULL`, not to the current timestamp.

 The following table contains several [`TIMESTAMP`](datetime.html) columns that permit `NULL` values:

```
CREATE TABLE t
(
  ts1 TIMESTAMP NULL DEFAULT NULL,
  ts2 TIMESTAMP NULL DEFAULT 0,
  ts3 TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP
);
```

 A [`TIMESTAMP`](datetime.html) 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`](date-and-time-functions.html#function_current-timestamp) and no value is specified for the column

* [`CURRENT_TIMESTAMP`](date-and-time-functions.html#function_current-timestamp) or any of its synonyms such as [`NOW()`](date-and-time-functions.html#function_now) is explicitly inserted into the column

 In other words, a [`TIMESTAMP`](datetime.html) column defined to permit `NULL` values auto-initializes only if its definition includes `DEFAULT CURRENT_TIMESTAMP`:

```
CREATE TABLE t (ts TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP);
```

 If the [`TIMESTAMP`](datetime.html) 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:

```
CREATE TABLE t1 (ts TIMESTAMP NULL DEFAULT '0000-00-00 00:00:00');
CREATE TABLE t2 (ts TIMESTAMP NULL DEFAULT NULL);
```

 To set the [`TIMESTAMP`](datetime.html) column in either table to the current timestamp at insert time, explicitly assign it that value. For example:

```
INSERT INTO t2 VALUES (CURRENT_TIMESTAMP);
INSERT INTO t1 VALUES (NOW());
```

 If the [`explicit_defaults_for_timestamp`](server-system-variables.html#sysvar_explicit_defaults_for_timestamp) system variable is enabled, [`TIMESTAMP`](datetime.html) columns permit `NULL` values only if declared with the `NULL` attribute. Also, [`TIMESTAMP`](datetime.html) 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`](date-and-time-functions.html#function_current-timestamp) or a synonym such as [`NOW()`](date-and-time-functions.html#function_now).

