简体   繁体   中英

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

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

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

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 ('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)
ALTER TABLE tbl_name AUTO_INCREMENT = 6000

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

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

SHOW CREATE TABLE your_tablename;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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