Why can't I add an auto-increment ID to a table when creating it in TiDB using Navicat?

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 为什么使用navcat在tidb创建表的时候,没办法往表里面添加自增id呢?

| username: TiDBer_微风轻吟

To improve efficiency, please provide the following information. A clear problem description can help resolve the issue faster:

[Problem] Unable to add auto-increment ID to the table

[TiDB Version] 7.3.0

| username: Soysauce520 | Original post link

You can only add it when creating the table. Refer to AUTO_INCREMENT | PingCAP 文档中心

| username: TiDBer_微风轻吟 | Original post link

Okay, thank you.

| username: zhanggame1 | Original post link

You can manually modify the table creation script in Navicat. If there are no extreme performance requirements, you can add the AUTO_ID_CACHE=1 parameter. This way, the increments will not be large or small and uncontrollable.

| username: TiDBer_微风轻吟 | Original post link

Oh? Is there something special about this? Isn’t it auto-increment? Another question is where to add AUTO_ID_CACHE=1 :sweat_smile: :sob: :sob:

| username: zhanggame1 | Original post link

There is a link above AUTO_INCREMENT | PingCAP Documentation Center. Please read it carefully. By default, TiDB auto-increment works like this: if there are 3 TiDB instances, one will handle IDs from 1 to 30000, another from 30001 to 60000, and the third from 60001 to 90000. Once exhausted, it will request more.

You can set AUTO_ID_CACHE=1 in the table creation statement’s comment section like this:
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![auto_id_cache] AUTO_ID_CACHE=1 */ COMMENT=‘Personnel table’;

| username: TiDBer_微风轻吟 | Original post link

Okay, thank you, boss.

| username: redgame | Original post link

CREATE TABLE t(id INT PRIMARY KEY AUTO_INCREMENT, c INT);

| username: WinterLiu | Original post link

ALTER TABLE is not supported for adding the AUTO_INCREMENT attribute :grin:

| username: TiDBer_小阿飞 | Original post link

AUTO_INCREMENT is a column attribute used to automatically fill in default column values. When the INSERT statement does not specify a specific value for the AUTO_INCREMENT column, the system will automatically assign a value to that column.

For performance reasons, auto-increment numbers are allocated in batches to each TiDB server (by default, 30,000 values), ensuring uniqueness. However, the values assigned to the INSERT statement are only monotonic on a single TiDB server.

| username: dba远航 | Original post link

Add it in advance when creating the table.

| username: TiDBer_微风轻吟 | Original post link

Yes, thank you, it has been resolved.

| username: kelvin | Original post link

Add it when creating the table.

| username: chris-zhang | Original post link

Add when creating a table

| username: 呢莫不爱吃鱼 | Original post link

Just add it when creating the table.

| username: Hacker_QGgM2nks | Original post link

Clustered tables do not support altering the primary key, right?

| username: TiDBer_fbU009vH | Original post link

Need to rebuild the table.

| username: wangkk2024 | Original post link

Learned.

| username: system | Original post link

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.