Bug #111183 When use ON DUPLICATE UPDATE syntax, TIMESTAMP in primary key, get wrong answer

Submitted: 29 May 2023 9:46 Modified: 29 May 2023 12:20
Reporter: linkang zhang (OCA) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: DML Severity:S3 (Non-critical)
Version:5.7 OS:Any
Assigned to: CPU Architecture:Any
Tags: UPDATE DUPLICATE affected_rows

[29 May 2023 9:46] linkang zhang
Description:
When we use ON DUPLICATE UPDATE syntax to insert data, CURRENT_TIMESTAMP in primary key, we get wrong answer.

How to repeat:
// we can repeat this by mysql-test.

// create one file named 'test_update.test' in directory mysql-test/t

// The content of 'test_update.test' is:

CREATE TABLE `test_up`(
    `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `data` int,
    PRIMARY KEY(`ts`) 
);

INSERT INTO test_up VALUES("2023-05-29 17:34:55.01", 1);

INSERT INTO test_up VALUES("2023-05-29 17:34:55.01", 2) ON DUPLICATE KEY UPDATE data=2;
INSERT INTO test_up VALUES("2023-05-29 17:34:55.01", 3) ON DUPLICATE KEY UPDATE data=3;
INSERT INTO test_up VALUES("2023-05-29 17:34:55.01", 4) ON DUPLICATE KEY UPDATE data=4;

// now, we can use this command in directory mysql-test: 
// ./mtr mysql_update

// now, the wrong answer is:

mysqltest: At line 11: Query 'INSERT INTO test_up VALUES("2023-05-29 17:34:55.01", 4) ON DUPLICATE KEY UPDATE data=4' failed.
ERROR 1062 (23000): Duplicate entry '2023-05-29 12:42:03' for key 'test_up.PRIMARY'
[29 May 2023 12:20] MySQL Verification Team
Hello  Mr. zhang,

Thank you for your bug report.

We ran your test case and concluded that it is a bug. Bug is not in the DML statements, but it is in not following of the definition of the TIMESTAMP in the table.

This bug is affecting, both 5.7 and latest 8.0.

Verified as reported.

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.052 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.