Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.Original topic: DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE字段在INSERT INTO ON DUPLICATE KEY UPDATE没有指定时自动更新失效
[TiDB Version] 5.7.25-TiDB-v5.4.1
Create Table Statement:
CREATE TABLE test_on_update
(
id
bigint(20) NOT NULL,
user_id
bigint(20) DEFAULT NULL,
description
VARCHAR(255) DEFAULT NULL,
updated_time
TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
PRIMARY KEY(id
)
);
Insert Test Data:
INSERT INTO test_on_update (id, user_id, description, updated_time) VALUES (1, 1001, ‘test1’, “2000-01-01 00:00:00”);
INSERT INTO test_on_update (id, user_id, description, updated_time) VALUES (2, 1002, ‘test2’, “2000-01-01 00:00:00”);
INSERT INTO test_on_update (id, user_id, description, updated_time) VALUES (3, 1003, ‘test3’, “2000-01-01 00:00:00”);
When executing the following statement, updated_time will automatically update to the current time:
UPDATE test_on_update SET description=‘test_description_update’ WHERE id=1;
[Question] Why doesn’t updated_time automatically update when executing the following statement:
INSERT INTO test_on_update (id) VALUES (2) ON DUPLICATE KEY UPDATE str = “test_description_insert”;
Why does updated_time only update when specified as follows:
INSERT INTO test_on_update (id) VALUES (2) ON DUPLICATE KEY UPDATE str = “test_description_insert”, updated_time=NOW();