如何设置一个AUTO_INCREMENT字段与mysql中的值6000开始?

时间:2010-08-26 21:51:55

标签: mysql auto-increment

如何在mysql中没有自动增量键设置字段自动增量,或者如何在mysql中设置字段自动增量为起始值6000?

3 个答案:

答案 0 :(得分:12)

  

...如何在mysql中使用起始值6000设置字段自动增量?

如果你的桌子已经存在:

ALTER TABLE your_table AUTO_INCREMENT = 6000;

如果您是从头开始创建表:

CREATE TABLE your_table () AUTO_INCREMENT = 6000;

来源和进一步阅读:


测试用例:

CREATE TABLE users (
   user_id  int NOT NULL, 
   name     varchar(50),
   PRIMARY KEY (user_id)
);

INSERT INTO users VALUES (1, 'Bob');
INSERT INTO users VALUES (2, 'Joe');
INSERT INTO users VALUES (3, 'Paul');

ALTER TABLE users MODIFY user_id int NOT NULL AUTO_INCREMENT;
ALTER TABLE users AUTO_INCREMENT = 6000;

INSERT INTO users (name) VALUES ('Keith');
INSERT INTO users (name) VALUES ('Steve');
INSERT INTO users (name) VALUES ('Jack');

SELECT * FROM users;
+---------+-------+
| user_id | name  |
+---------+-------+
|       1 | Bob   |
|       2 | Joe   |
|       3 | Paul  |
|    6000 | Keith |
|    6001 | Steve |
|    6002 | Jack  |
+---------+-------+
6 rows in set (0.01 sec)

答案 1 :(得分:2)

ALTER TABLE tbl_name AUTO_INCREMENT = 6000

但请注意,此表中应该没有PK lager 6000!

答案 2 :(得分:0)

如果对包含自动增量PK和某些数据的表执行以下操作,mysql将为您显示正确的语法,以及更多信息:

SHOW CREATE TABLE your_tablename;
相关问题