文档首页> 云数据库 GaussDB(for MySQL)> 故障排除> 基本使用类> 表的自增AUTO_INCREMENT初值与步长
更新时间:2023-08-31 GMT+08:00
分享

表的自增AUTO_INCREMENT初值与步长

数据表中自增字段的AUTO_INCREMENT的初值与步长由auto_increment_increment和auto_increment_offset参数决定。

  • auto_increment_offset:AUTO_INCREMENT值的初值。
  • auto_increment_increment:AUTO_INCREMENT值每次增长的步长。
  • 当 auto_increment_offset > auto_increment_increment 时,实际使用时初值会变为auto_increment_increment。
  • 当 auto_increment_offset <= auto_increment_increment,自增值计算方式如下:

    自增值 = auto_increment_offset + N*auto_increment_increment (N为插入的数据条数)

在GaussDB(for MySQL)中这两个参数默认值都为1,参考如下步骤修改。如需修改时需要在控制台-实例详情-参数修改中修改。

  1. “实例管理”页面,选择指定的实例,单击实例名称,进入实例的基本信息页面。
  2. 在左侧导航栏中选择“参数修改”,在“参数”页签修改相应参数。

示例:

  1. auto_increment_offset=1,auto_increment_increment=1,表示初值为1,步长为1。
    show variables like 'auto_inc%'; 
    +--------------------------+-------+ 
    | Variable_name            | Value | 
    +--------------------------+-------+ 
    | auto_increment_increment | 1     | 
    | auto_increment_offset    | 1     | 
    +--------------------------+-------+ 
  2. 修改auto_increment_increment=2,步长变为2。
    set session auto_increment_offset=2; 
    Query OK, 0 rows affected (0.02 sec)  
    show variables like 'auto_inc%'; 
    +--------------------------+-------+ 
    | Variable_name            | Value | 
    +--------------------------+-------+ 
    | auto_increment_increment | 2     | 
    | auto_increment_offset    | 1     | 
    +--------------------------+-------+ 
  3. auto_increment_offset=10,auto_increment_increment=2,由于auto_increment_offset > auto_increment_increment,因此初值为2,步长为2。
    set session auto_increment_offset=10; 
    set session auto_increment_increment=2; 
    show variables like 'auto_inc%'; 
    +--------------------------+-------+ 
    | Variable_name            | Value | 
    +--------------------------+-------+ 
    | auto_increment_increment | 2     | 
    | auto_increment_offset    | 10    | 
    +--------------------------+-------+  
    create table auto_test2(id int NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`)); 
    Query OK, 0 rows affected (0.08 sec)  
    show create table auto_test2; 
    CREATE TABLE `auto_test2` (   `id` int NOT NULL AUTO_INCREMENT,   PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 
    1 row in set (0.01 sec) 
    insert into auto_test2 values(0), (0), (0); 
    Query OK, 3 rows affected (0.00 sec) 
    Records: 3  Duplicates: 0  Warnings: 0  
    select * from auto_test2; 
    +----+ 
    | id | 
    +----+ 
    |  2 | 
    |  4 | 
    |  6 | 
    +----+ 
    3 rows in set (0.01 sec)  
  4. auto_increment_offset=5,auto_increment_increment=10,初值为5,步长为10。
    set session auto_increment_offset=5; 
    set session auto_increment_increment=10; 
    show variables like 'auto_inc%'; 
    +--------------------------+-------+ 
    | Variable_name            | Value | 
    +--------------------------+-------+ 
    | auto_increment_increment | 10    | 
    | auto_increment_offset    | 5     | 
    +--------------------------+-------+ 
    create table auto_test3(id int NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`)); 
    insert into auto_test3 values(0), (0), (0); 
    Query OK, 3 rows affected (0.00 sec) 
    Records: 3  Duplicates: 0  Warnings: 0  
    select * from auto_test3; 
    +----+ 
    | id | 
    +----+ 
    |  5 | 
    | 15 | 
    | 25 | 

分享:

    相关文档

    相关产品