mysql自动增加的字段怎么设置初始值

2025-05-10 04:41:34
推荐回答(3个)
回答1:

使用
ALTER TABLE 表名 AUTO_INCREMENT = 100;

下面是详细例子

mysql> CREATE TABLE test_create_tab2 (
-> id INT AUTO_INCREMENT,
-> val VARCHAR(10),
-> PRIMARY KEY (id)
-> );
Query OK, 0 rows affected (0.09 sec)

mysql> INSERT INTO test_create_tab2(val) VALUES ('NO id');
Query OK, 1 row affected (0.03 sec)

mysql> select last_insert_id() as id;
+----+
| id |
+----+
| 1 |
+----+
1 row in set (0.00 sec)

mysql> INSERT INTO test_create_tab2(val) VALUES ('NO id 2');
Query OK, 1 row affected (0.03 sec)

mysql> select last_insert_id() as id;
+----+
| id |
+----+
| 2 |
+----+
1 row in set (0.00 sec)

mysql> select * from test_create_tab2;
+----+---------+
| id | val |
+----+---------+
| 1 | NO id |
| 2 | NO id 2 |
+----+---------+
2 rows in set (0.00 sec)

mysql> ALTER TABLE test_create_tab2 AUTO_INCREMENT = 100;
Query OK, 2 rows affected (0.25 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> INSERT INTO test_create_tab2(val) VALUES ('NO id N');
Query OK, 1 row affected (0.03 sec)

mysql> select * from test_create_tab2;
+-----+---------+
| id | val |
+-----+---------+
| 1 | NO id |
| 2 | NO id 2 |
| 100 | NO id N |
+-----+---------+
3 rows in set (0.00 sec)

回答2:

题目不明确,无法解答。

回答3:

alter table table_name AUTO_INCREMENT=n