MySQL 如何设置一个AUTO_INCREMENT字段以在mysql中以值6000开头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3579829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:58:56  来源:igfitidea点击:

How to set an AUTO_INCREMENT field with to start with the value 6000 in mysql?

mysqlauto-increment

提问by Ajay

How to set a field auto increment without auto increment key in mysql or how set a field auto increment with start value 6000 in mysql?

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

回答by Daniel Vassallo

... how set a field auto increment with start value 6000 in mysql?

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

If your table already exists:

如果您的表已经存在:

ALTER TABLE your_table AUTO_INCREMENT = 6000;

If you are creating your table from scratch:

如果您从头开始创建表:

CREATE TABLE your_table () AUTO_INCREMENT = 6000;

Source and further reading:

来源和进一步阅读:



Test case:

测试用例:

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 ('Hyman');

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

回答by Rufinus

ALTER TABLE tbl_name AUTO_INCREMENT = 6000

but be aware you should have no PK lager 6000 in this table !

但请注意,此表中不应有 PK 啤酒 6000!

回答by Mark B

mysql will show you the correct syntax for this, and more, if you execute the following for a table that contains an auto increment PK & some data already:

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

SHOW CREATE TABLE your_tablename;