如何自动增加mysql id

时间:2017-09-20 07:25:26

标签: php mysql sql

我想增加id为6,意味着当下一个entery进入8然后另一个进入14就像这个。怎么做。

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

1 个答案:

答案 0 :(得分:1)

从文档中查看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)

请记住,这是每个数据库而不是每个表。

  

无法将这两个变量的影响限制在一个表中;