Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.Original topic: 如何重置一个表的AUTO_INCREMENT (自增主键)?

[TiDB Usage Environment] Production Environment / Testing / Poc
Testing Environment
[TiDB Version]
V4.0.9
[Reproduction Path] What operations were performed that caused the issue
It seems that a developer directly inserted the maximum value into the auto-increment ID column, updating the AUTO_INCREMENT
column to the maximum value. How can we reset the AUTO_INCREMENT
value of the table?
[Encountered Issue: Problem Phenomenon and Impact]
The table structure is as follows:
CREATE TABLE `dcs_option_20230111` (
`option_id` int(11) NOT NULL AUTO_INCREMENT,
`survey_id` int(11) DEFAULT '0',
`template_id` int(11) DEFAULT '0' COMMENT 'Template ID',
`question_id` int(11) DEFAULT '0',
`option_value` text DEFAULT NULL,
`option_identity` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT '' COMMENT 'Option Identifier',
PRIMARY KEY (`option_id`),
KEY `question_id` (`question_id`),
KEY `survey_id` (`survey_id`),
KEY `option_identity` (`option_identity`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
A developer inserted a SQL statement specifying option_id
, causing option_id
to become the maximum value, and data cannot be inserted without specifying a value.
The insert statement is as follows:
INSERT INTO `dcs_option_20230111`
(`option_id`, `survey_id`, `template_id`, `question_id`, `option_value`) VALUES
('2147483647', '12689831', '0', '169791211', 'Option 1');
The table creation statement then becomes:
CREATE TABLE `dcs_option_20230111` (
`option_id` int(11) NOT NULL AUTO_INCREMENT,
`survey_id` int(11) DEFAULT '0',
`template_id` int(11) DEFAULT '0' COMMENT 'Template ID',
`question_id` int(11) DEFAULT '0',
`option_value` text DEFAULT NULL,
`option_identity` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT '' COMMENT 'Option Identifier',
PRIMARY KEY (`option_id`),
KEY `question_id` (`question_id`),
KEY `survey_id` (`survey_id`),
KEY `option_identity` (`option_identity`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin AUTO_INCREMENT=2147513648
At this point, we cannot insert data without specifying option_id
, and the following error is reported:
Error Code: 1062 Duplicate entry '2147483647' for key 'PRIMARY'
Using the SQL:
ALTER TABLE dcs_option_20230111 AUTO_INCREMENT=566167158;
and restarting the TiDB component, the table creation statement still shows the original maximum value. Is there any other SQL or method to reset this value?
[Resource Configuration]
[Attachment: Screenshot/Log/Monitoring]