简体   繁体   English

如何自动增加mysql ID

[英]How to auto increment mysql id

i want increment id by 6, means when next entery enter then 8, then another enter 14 like this one. 我想将id递增6,这意味着当下一个Entery输入8时,然后另一个Enter 14像这样。 how to do that. 怎么做。

                 +--------------------------+-------+
                 | username               | user_id |
                 +--------------------------+-------+
                 | yanki                    | 1     |
                 | dude                     | 2     |
                 +--------------------------+-------+

Check the auto_increment_increment , from the docs: 从文档中检查auto_increment_increment

mysql> SHOW VARIABLES LIKE 'auto_inc%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.00 sec)

mysql> CREATE TABLE autoinc1
    -> (col INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
  Query OK, 0 rows affected (0.04 sec)

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

mysql> SHOW VARIABLES LIKE 'auto_inc%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| auto_increment_increment | 10    |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.01 sec)

mysql> INSERT INTO autoinc1 VALUES (NULL), (NULL), (NULL), (NULL);
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> SELECT col FROM autoinc1;
+-----+
| col |
+-----+
|   1 |
|  11 |
|  21 |
|  31 |
+-----+
4 rows in set (0.00 sec)

Just keep in mind that this is per database not per table. 请记住,这是每个数据库而不是每个表。

It is not possible to restrict the effects of these two variables to a single table; 不可能将这两个变量的作用限制在一个表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM