Feature introduction
ON UPDATE CURRENT_TIMESTAMP is a column attribute in MySQL-compatible mode. It automatically sets the value of a column to the current timestamp whenever the row data is updated (UPDATE), primarily used to record the last modification time of the data row.
In OceanBase Database, both TIMESTAMP and DATETIME columns support this attribute, and their behavior is consistent with that of MySQL.
Overview
When the data in a row of a table is updated, the system automatically sets the columns with the ON UPDATE CURRENT_TIMESTAMP attribute to the current timestamp, without requiring an explicit assignment by the application. This attribute is typically used together with DEFAULT CURRENT_TIMESTAMP: DEFAULT CURRENT_TIMESTAMP records the creation time during an INSERT, while ON UPDATE CURRENT_TIMESTAMP automatically maintains the last modification time during an update.
Application scenarios
- Audit logs: Record the last modification time of data and track the data change history.
- Data synchronization: Automatically maintains the update time of records, facilitating incremental synchronization.
- Business table design: scenarios where the last modification time of rows needs to be recorded, to reduce application development workload
Syntax
You can specify the data type in the column definition of the CREATE TABLE statement. The syntax is as follows:
column name TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
You can also specify the timestamp precision, which must match the precision defined in the column:
column name TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)
The usage of the DATETIME column is the same:
column DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
Note
- This attribute applies only to
TIMESTAMPandDATETIMEcolumns; other types of columns do not support it.- The precision after
ON UPDATE CURRENT_TIMESTAMPmust match the precision in the column definition; otherwise, an error will be reported. For example, if a column is defined asTIMESTAMP(6), you must useON UPDATE CURRENT_TIMESTAMP(6).- In addition to
CREATE TABLE, this attribute is also supported for theADD COLUMN,MODIFY COLUMN, andCHANGE COLUMNoperations ofALTER TABLE.
Examples
Create a table with columns for creation time and modification time:
obclient> CREATE TABLE t1 (
id INT PRIMARY KEY,
c1 VARCHAR(20),
gmt_create TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
gmt_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
Query OK, 0 rows affected
obclient> INSERT INTO t1(id, c1) VALUES (1, 'a');
Query OK, 1 row affected
obclient> SELECT id, c1, gmt_create, gmt_modified FROM t1;
+----+------+---------------------+---------------------+
| id | c1 | gmt_create | gmt_modified |
+----+------+---------------------+---------------------+
| 1 | a | 2026-08-20 15:38:28 | 2026-08-20 15:38:28 |
+----+------+---------------------+---------------------+
1 row in set
obclient> UPDATE t1 SET c1 = 'b' WHERE id = 1;
Query OK, 1 row affected
obclient> SELECT id, c1, gmt_modified FROM t1;
+----+------+---------------------+
| id | c1 | gmt_modified |
+----+------+---------------------+
| 1 | b | 2026-08-20 15:38:30 |
+----+------+---------------------+
1 row in set
As shown in the results, after executing the UPDATE statement, the value of the gmt_modified column is automatically updated to the current timestamp, while the value of the gmt_create column remains unchanged.
Examples with precision:
obclient> CREATE TABLE t2 (
c1 TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)
);
Query OK, 0 rows affected
Considerations
- Exclusive to MySQL-compatible mode: This attribute is a feature of the MySQL-compatible mode and is not supported in Oracle-compatible mode.
- Consistent precision: The precision of
ON UPDATE CURRENT_TIMESTAMPmust be consistent with the precision defined for the column; otherwise, an error will be reported. - Used with
DEFAULT CURRENT_TIMESTAMP: It is recommended to also specifyDEFAULT CURRENT_TIMESTAMPto ensure an initial value during insertion. - Implicit default attributes: When the system variable
explicit_defaults_for_timestampis set to OFF (the default value), if the firstTIMESTAMPcolumn in a table is not explicitly specified with theNULLattribute and a default value, the system automatically adds theDEFAULT CURRENT_TIMESTAMPandON UPDATE CURRENT_TIMESTAMPattributes to that column. For more information about this variable, see explicit_defaults_for_timestamp. - Trigger only when data changes: The
ON UPDATE CURRENT_TIMESTAMPcolumn is updated only if theUPDATEstatement actually changes the values of other columns in the row. If the row data does not change after the update (for example,UPDATE t1 SET c1 = c1), this column will not be refreshed. - Limitations on parallel DML: Tables containing the
ON UPDATE CURRENT_TIMESTAMPcolumn do not support parallel DML. For more information, see Parallel execution categories.