Sample Header Ad - 728x90

How to make MySQL 8 alert when integer overflow happens during INSERT ... ON DUPLICATE KEY?

1 vote
2 answers
548 views
Let's consider we have a simple table with auto-incrementing integer ID and some unique column:
CREATE TABLE test (
  id int unsigned NOT NULL AUTO_INCREMENT,
  value tinyint DEFAULT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY value (value)
);
Imagine we inserted into it so many times that ID reached the maximum:
INSERT INTO test VALUES 
(4294967294, 1),
(4294967295, 2);
Now we are trying to insert one more row and if it's duplicate then change the value to 3:
INSERT INTO test (value) VALUES (1) ON DUPLICATE KEY UPDATE value = 3;
**What is the expected behaviour?**
id     value  
----------  --------
4294967294         3
4294967295         2
**What we get in fact:**
id     value  
----------  --------
4294967294         1
4294967295         3
**How?** Because of the integer overflow the auto-generated ID of the new row was again 4294967295. __So now the question. Is it possible to make MySQL 8 alert if the overflow happens during query?__ In my case such silent overflow caused huge data loss. Because all newly added rows just overwrote the last row. PS I'm not asking how to fix or prevent such overflow. That's obvious. I'm asking exactly how to catch such errors.
Asked by Stalinko (201 rep)
May 22, 2020, 01:09 PM
Last activity: May 31, 2020, 02:21 AM