Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.
Original topic: 把主键id改成auto_increment报错说这是住建不能改,这个怎么弄?
[TiDB Usage Environment] Production Environment / Testing / Poc
[TiDB Version]
[Reproduction Path] What operations were performed to encounter the issue
[Encountered Issue: Issue Phenomenon and Impact]
[Resource Configuration]
[Attachments: Screenshots / Logs / Monitoring]
The original table is:
CREATE TABLE `model` (
`id` bigint(20) unsigned NOT NULL /*T![auto_rand] AUTO_RANDOM(5) */,
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
KEY `idx_model_group_id` (`model_group_id`),
UNIQUE KEY `uk_unique_identification` (`unique_identification`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![auto_rand_base] AUTO_RANDOM_BASE=150001 */
When I used Navicat’s data synchronization feature to sync data to another database, it became:
CREATE TABLE `model` (
`id` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
KEY `idx_model_group_id` (`model_group_id`),
UNIQUE KEY `uk_unique_identification` (`unique_identification`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
The AUTO_RANDOM attribute in the table was lost, causing data addition to fail with an error saying the id is missing (it can no longer be auto-generated).
Using ALTER TABLE model MODIFY COLUMN id bigint AUTO_RANDOM(5);
to modify it results in an error:
How can this be resolved?
Create a new table using select into; drop the old table; rename the new table to the old table’s name.
– SELECT * INTO unique_check_backup FROM unique_check; is not supported
INSERT INTO unique_check_backup SELECT * FROM unique_check;
Indeed, it cannot be changed; you can only rebuild the table and then insert the data into it.
You can first create the table structure on the target side, and then use Navicat for data synchronization. This way, it will work. As for now, since it has already been generated, it cannot be changed and needs to be rebuilt.
The official documentation also provides explanations on the usage limitations of auto_random
, take a look.
This really cannot be changed.
There’s no way to directly modify it; you can only change it by rebuilding the table. However, you can check whether the driver used by Navicat is the official MySQL driver. The MariaDB driver will ignore comments by default, causing attributes like /*T![auto_rand] AUTO_RANDOM(5) */
to be ignored and resulting in the creation of a regular table.
Clustered index cannot be changed.
Check if the primary key is referenced by other tables. If it is, first remove the reference relationship, then try modifying it.
The only solution is to rebuild the table.
The primary key cannot be changed. The primary key and table ID are used to locate a unique piece of data.
It is used to locate unique data. Now I just want to modify its auto-increment type without changing the field name.
The primary key cannot be changed.
This is a CLUSTERED table, and CLUSTERED tables do not support modifications.