MySQL自增屬性auto_increment_increment和auto_increment_offset

MySQL的系統變量或會話變量auto_increment_increment(自增步長)和auto_increment_offset(自增偏移量)控制着數據表的自增列ID。mysql

mysql> show tables;
Empty set (0.00 sec)

mysql> CREATE TABLE `test_tb1` ( `id` int(4) NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` varchar(20)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.16 sec)

mysql> insert into test_tb1(name) values('Andy'),('Danny');
Query OK, 2 rows affected (0.13 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from test_tb1;
+----+-------+
| id | name  |
+----+-------+
|  1 | Andy  |
|  2 | Danny |
+----+-------+
2 rows in set (0.04 sec)

mysql> show session variables like '%auto_incre%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.01 sec)

mysql> set session auto_increment_increment=10;
Query OK, 0 rows affected (0.00 sec)

mysql> show session variables like '%auto_incre%'; -- 改變自增步長
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 10    |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.01 sec)

mysql> show global variables like '%auto_incre%'; -- 全局變量並無變化
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.05 sec)

mysql> truncate table test_tb1;
Query OK, 0 rows affected (0.09 sec)

mysql> insert into test_tb1(name) values('Andy'),('Bonny'),('Lisa'),('Jack'),('Robin');
Query OK, 5 rows affected (0.06 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from test_tb1;
+----+-------+
| id | name  |
+----+-------+
|  1 | Andy  |
| 11 | Bonny |
| 21 | Lisa  |
| 31 | Jack  |
| 41 | Robin |
+----+-------+
5 rows in set (0.00 sec)

獲取指定數據表的下一個auto_increment自增值的兩種方式:sql

mysql> show table status \G
*************************** 1. row ***************************
           Name: test_tb1
         Engine: InnoDB
        Version: 10
     Row_format: Dynamic
           Rows: 5
 Avg_row_length: 3276
    Data_length: 16384
Max_data_length: 0
   Index_length: 0
      Data_free: 0
 Auto_increment: 51
    Create_time: 2018-12-19 14:43:53
    Update_time: 2018-12-19 14:54:23
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options:
        Comment:
1 row in set (0.00 sec)

mysql> select auto_increment from information_schema.`TABLES` where table_name='test_tb1';
+----------------+
| auto_increment |
+----------------+
|             51 |
+----------------+
1 row in set (0.03 sec)

auto_increment_increment和auto_increment_offset的取值範圍: 1 ~ 65535,這個範圍和自增列的值不要緊,列的數據大小是對應的值類型控制的。session

mysql> set auto_increment_increment=-1;
Query OK, 0 rows affected, 1 warning (0.05 sec)

mysql> show variables like '%auto_incre%';  -- 小於1時用默認最小值1
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.00 sec)

mysql> set auto_increment_increment=65536;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show variables like '%auto_incre%'; -- 大於65535時用默認最大值65536  
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 65535 |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.00 sec)

mysql> truncate table test_tb1;
Query OK, 0 rows affected (0.49 sec)

mysql>  insert into test_tb1(name) values('Andy'), ('Bonny'), ('Danny'), ('Jack'), ('Robin');
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from test_tb1; -- 值能夠大於65535
+--------+-------+
| id     | name  |
+--------+-------+
|      1 | Andy  |
|  65536 | Bonny |
| 131071 | Danny |
| 196606 | Jack  |
| 262141 | Robin |
+--------+-------+
5 rows in set (0.00 sec)

mysql> truncate table test_tb1;
Query OK, 0 rows affected (0.06 sec)


mysql> insert into test_tb1(id, name) values(-3, 'Andy'), (-2, 'Bonny'), (-1, 'Danny');
Query OK, 3 rows affected (0.06 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from test_tb1; -- 值能夠小於1
+----+-------+
| id | name  |
+----+-------+
| -3 | Andy  |
| -2 | Bonny |
| -1 | Danny |
+----+-------+
3 rows in set (0.00 sec)

全局(global)與會話(session)自增屬性,會話針對當前鏈接的全部操做,全局變量會在新的鏈接會話中生效。code

mysql> show global variables like 'auto_incre%'; -- 全局的自增屬性值
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.01 sec)

mysql> show variables like 'auto_incre%';  -- 會話的自增屬性值,同show session variables like 'auto_incre%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 65535 |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.00 sec)

mysql> set global auto_increment_increment=10;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'auto_incre%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 65535 |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.01 sec)

mysql> show global variables like 'auto_incre%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 10    |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.00 sec)
相關文章
相關標籤/搜索