# [FAQ] TiDB 中如何设置 auto_random 属性

## Key Words

auto_random

## Description

1. 首先，在开启 [`tidb_allow_remove_auto_inc`](https://docs.pingcap.com/zh/tidb/v5.1/system-variables#tidb_allow_remove_auto_inc-%E4%BB%8E-v2118-%E5%92%8C-v304-%E7%89%88%E6%9C%AC%E5%BC%80%E5%A7%8B%E5%BC%95%E5%85%A5) 后，TiDB 允许移除主键的 `auto_increment` 属性。
2. 其次，在 [auto_random 限制中](https://docs.pingcap.com/zh/tidb/stable/auto-random#%E4%BD%BF%E7%94%A8%E9%99%90%E5%88%B6)描述 TiDB 支持将 AUTO_INCREMENT 属性改为 AUTO_RANDOM 属性。
3. 但如下实验分别因 `Invalid auto random: auto_random option must be defined on bigint column, but not on int column`，`Invalid auto random: modifying the auto_random column type is not supported` 和 `Invalid auto random: auto_random can only be converted from auto_increment clustered primary key` 导致修改 auto_increment 为 auto_random 失败。

    ```sql
    mysql> CREATE TABLE zy184_7 (
        id_syc_bte_bk_mt_special int NOT NULL auto_increment COMMENT '主键id',
        is_active varchar(1) NOT NULL DEFAULT 'Y' COMMENT '是否可用',
        created_by varchar(100) DEFAULT 'SYS' COMMENT '创建人',
        date_created datetime(6) DEFAULT CURRENT_TIMESTAMP(6) COMMENT '创建时间',
        updated_by varchar(100) DEFAULT 'SYS' COMMENT '修改人',
        date_updated datetime(6) DEFAULT CURRENT_TIMESTAMP(6) COMMENT '修改时间',
        PRIMARY KEY (id_syc_bte_bk_mt_special)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin

    mysql> insert into zy184_7(is_active) values('1');
    mysql> insert into zy184_7(is_active) values('1');

    mysql> set @@tidb_allow_remove_auto_inc=1;

    mysql> alter table zy184_7 modify id_syc_bte_bk_mt_special int(11) not null auto_random;
    ERROR 8216 (HY000): Invalid auto random: auto_random option must be defined on bigint column, but not on int column

    mysql> alter table zy184_7 modify id_syc_bte_bk_mt_special bigint not null auto_random;
    ERROR 8216 (HY000): Invalid auto random: modifying the auto_random column type is not supported

    mysql> alter table zy184_7 modify id_syc_bte_bk_mt_special bigint not null ;
    Query OK, 0 rows affected (0.51 sec)

    mysql> alter table zy184_7 modify id_syc_bte_bk_mt_special bigint not null auto_random;
    ERROR 8216 (HY000): Invalid auto random: auto_random can only be converted from auto_increment clustered primary key
    ```

## Content

1. 官网描述 [auto_random 限制中](https://docs.pingcap.com/zh/tidb/stable/auto-random#%E4%BD%BF%E7%94%A8%E9%99%90%E5%88%B6)，指出 “该属性必须指定在 BIGINT 类型的主键列上” 及 “不支持使用 ALTER TABLE 来修改 AUTO_RANDOM 属性，包括添加或移除该属性”，**上述 2 个使用限制是导致本例修改失败的主要原因**。
2. 上述实验中报错 `auto_random option must be defined on bigint column, but not on int column` 是因为 auto_random 必须作用于 bigint 类型列上。
3. 上述实验中报错 `modifying the auto_random column type is not supported` 是因为修改列数据类型（int -> bigint）同时修改 auto_increment 为 auto_random 是不支持的。  
4. 上述实验中报错 `auto_random can only be converted from auto_increment clustered primary key` 是因为先将 `id_syc_bte_bk_mt_special` 列数据类型改为 bigint, 随后的添加 AUTO_RANDOM 属性操作不被支持。  
5. 综上，可理解为由于 “auto_random 使用限制” 和 “auto_random 修改限制” 导致的修改失败，可使用如下方法绕过，即：先在不丢失 auto_increment 属性的前提下修改列数据类型，再修改 auto_increment 属性为 auto_random 属性。步骤如下：

    ```sql
    mysql> CREATE TABLE zy184_7 (
        id_syc_bte_bk_mt_special int NOT NULL auto_increment COMMENT '主键id',
        is_active varchar(1) NOT NULL DEFAULT 'Y' COMMENT '是否可用',
        created_by varchar(100) DEFAULT 'SYS' COMMENT '创建人',
        date_created datetime(6) DEFAULT CURRENT_TIMESTAMP(6) COMMENT '创建时间',
        updated_by varchar(100) DEFAULT 'SYS' COMMENT '修改人',
        date_updated datetime(6) DEFAULT CURRENT_TIMESTAMP(6) COMMENT '修改时间',
        PRIMARY KEY (id_syc_bte_bk_mt_special)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
    
    mysql>insert into zy184_7(is_active) values('1');  
    mysql>insert into zy184_7(is_active) values('1');  
    
    mysql>set @@tidb_allow_remove_auto_inc=1;
    
    mysql>alter table zy184_7 modify id_syc_bte_bk_mt_special bigint not null auto_increment;
    mysql>alter table zy184_7 modify id_syc_bte_bk_mt_special bigint not null auto_random;
    
    mysql>insert into zy184_7(is_active) values('1');      
    mysql>select * from zy184_7;
    +--------------------------+-----------+------------+----------------------------+------------+----------------------------+
    | id_syc_bte_bk_mt_special | is_active | created_by | date_created               | updated_by | date_updated               |
    +--------------------------+-----------+------------+----------------------------+------------+----------------------------+
    |                        1 | 1         | SYS        | 2022-10-31 21:13:42.782031 | SYS        | 2022-10-31 21:13:42.782031 |
    |                        2 | 1         | SYS        | 2022-10-31 21:13:43.463441 | SYS        | 2022-10-31 21:13:43.463441 |
    |      6917529027641123797 | 1         | SYS        | 2022-10-31 21:14:10.589334 | SYS        | 2022-10-31 21:14:10.589334 |
    +--------------------------+-----------+------------+----------------------------+------------+----------------------------+
    3 rows in set (0.01 sec)>
    >select version();
    ```

## Affected Versions

TiDB Version > v3.0.4
