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

发布于 2024-09-16 19:31:59 字数 45 浏览 2 评论 0原文

MySQL中如何将非自增字段设置为自增?如何将自动增量起始值设置为6000?

How can I set a non-auto-increment field to auto increment in MySQL? And how can I set the auto increment start value to 6000?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱桃奶球 2024-09-23 19:31:59

...如何在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)

... 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)
软的没边 2024-09-23 19:31:59
ALTER TABLE tbl_name AUTO_INCREMENT = 6000

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

ALTER TABLE tbl_name AUTO_INCREMENT = 6000

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

浮云落日 2024-09-23 19:31:59

如果您对包含自动增量 PK 和 PK 的表执行以下命令,mysql 将向您显示正确的语法以及更多内容。已经有一些数据:

SHOW CREATE TABLE your_tablename;

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:

SHOW CREATE TABLE your_tablename;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文