Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

A TIMESTAMP column changes on every UPDATE instead of defaulting to NULL

Your error

`shipped_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP2 more lines it can show

Fix it fast

Most common cause, 1 of 2

The column definition already holds the automatic properties

Redefine the column with the behaviour you want.

Fix
ALTER TABLE orders MODIFY shipped_at TIMESTAMP NULL DEFAULT NULL;

-- creation time that should never move
ALTER TABLE orders MODIFY created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

A TIMESTAMP that refreshes on every UPDATE, or will not stay NULL, carries DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP in its definition. MySQL gives the first TIMESTAMP column both only while explicit_defaults_for_timestamp is OFF; the default is ON. Check SHOW CREATE TABLE, then MODIFY the column to TIMESTAMP NULL DEFAULT NULL. MySQL 8.4 and 9.7.

  • Applies to MySQL 8.4 LTS, MySQL 9.7 LTS
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Print the stored definition of the table and the current value of the setting. The definition shows what the column does today; the setting decides what new tables get.

Run this first
SHOW CREATE TABLE orders\G
SELECT @@GLOBAL.explicit_defaults_for_timestamp, @@SESSION.explicit_defaults_for_timestamp;

Then find the line you see in this list and open its fix:

  1. 1The column definition already holds the automatic propertiesSHOW CREATE TABLE lists DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP on a column you never declared that way, while the setting is now 1
  2. 2explicit_defaults_for_timestamp is switched off on this server or sessionOne of the two values in the first step is 0, and newly created tables get the automatic clauses again

What does the error look like?

  • `shipped_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
  • The column was declared as plain TIMESTAMP, yet it holds the time of the last UPDATE
  • Inserting NULL into the TIMESTAMP column stores the current time instead of NULL

Cause 1 of 2

The column definition already holds the automatic properties

You seeSHOW CREATE TABLE lists DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP on a column you never declared that way, while the setting is now 1

Check

The timestamp refreshes because ON UPDATE CURRENT_TIMESTAMP is part of the stored column definition. With explicit_defaults_for_timestamp disabled, MySQL gives the first TIMESTAMP column of a table both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP when neither is written. A table created that way, on an older server or by a dump that spelled the clauses out, keeps them after the setting changes.

Check
SHOW CREATE TABLE orders\G

Fix

Redefine the column with the behaviour you want. For an optional time, permit NULL and default to NULL. For a creation time, keep DEFAULT CURRENT_TIMESTAMP and leave out ON UPDATE.

Fix
ALTER TABLE orders MODIFY shipped_at TIMESTAMP NULL DEFAULT NULL;

-- creation time that should never move
ALTER TABLE orders MODIFY created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;

Verify

The definition no longer has ON UPDATE, and an UPDATE of another column leaves the timestamp alone.

Verify
SHOW CREATE TABLE orders\G
UPDATE orders SET status = 'paid' WHERE id = 42;
SELECT shipped_at FROM orders WHERE id = 42;

Confirmed by dev.mysql.com

Did this fix it?

Cause 2 of 2

explicit_defaults_for_timestamp is switched off on this server or session

You seeOne of the two values in the first step is 0, and newly created tables get the automatic clauses again

Check

When the setting is off, TIMESTAMP columns that are not declared NULL become NOT NULL, and assigning NULL stores the current timestamp. Its default value is ON, and the variable is deprecated, so a 0 comes from an option file line or a SET in the session or tool that creates the tables. MySQL also logs a warning when it is disabled.

Check
sudo grep -rn -i "explicit.defaults.for.timestamp" /etc/mysql/ /etc/my.cnf /etc/my.cnf.d/ 2>/dev/null

Fix

Delete the line that turns it off and restart the server, and drop any SET of it from migration scripts. Then fix existing columns as in the first cause.

Fix
# remove from the [mysqld] section:
# explicit_defaults_for_timestamp = OFF
sudo systemctl restart mysql    # use the service name of your install

Verify

Both values read 1, and a new table with a plain TIMESTAMP column shows no automatic clauses.

Verify
SELECT @@GLOBAL.explicit_defaults_for_timestamp, @@SESSION.explicit_defaults_for_timestamp;
CREATE TABLE ts_probe (t TIMESTAMP NULL);
SHOW CREATE TABLE ts_probe\G
DROP TABLE ts_probe;

Confirmed by dev.mysql.com

Did this fix it?

How do I stop it coming back?

  • Write every TIMESTAMP column with its NULL or NOT NULL attribute and an explicit DEFAULT, so the setting cannot change what the column does.
  • Review SHOW CREATE TABLE output in schema diffs after imports; dumps carry the ON UPDATE clauses of the source tables.
  • Leave explicit_defaults_for_timestamp at its default and remove it from copied option files.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, QistLock, ShopGo.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.