如何在 MySQL 中设置初始值和自动增量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1485668/
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
How to set initial value and auto increment in MySQL?
提问by bbtang
How do I set the initial value for an "id" column in a MySQL table that start from 1001?
如何为从 1001 开始的 MySQL 表中的“id”列设置初始值?
I want to do an insert "INSERT INTO users (name, email) VALUES ('{$name}', '{$email}')";
我想做一个插入 "INSERT INTO users (name, email) VALUES ('{$name}', '{$email}')";
Without specifying the initial value for the id column.
不指定 id 列的初始值。
回答by Anatoliy
Use this:
用这个:
ALTER TABLE users AUTO_INCREMENT=1001;
or if you haven't already added an id column, also add it
或者如果您还没有添加 id 列,也添加它
ALTER TABLE users ADD id INT UNSIGNED NOT NULL AUTO_INCREMENT,
ADD INDEX (id);
回答by Eric Leschinski
MySQL - Setup an auto-incrementing primary key that starts at 1001:
MySQL - 设置一个从 1001 开始的自动递增主键:
Step 1, create your table:
第 1 步,创建您的表:
create table penguins(
my_id int(16) auto_increment,
skipper varchar(4000),
PRIMARY KEY (my_id)
)
Step 2, set the start number for auto increment primary key:
第二步,设置自增主键的起始编号:
ALTER TABLE penguins AUTO_INCREMENT=1001;
Step 3, insert some rows:
第 3 步,插入一些行:
insert into penguins (skipper) values("We need more power!");
insert into penguins (skipper) values("Time to fire up");
insert into penguins (skipper) values("kowalski's nuclear reactor.");
Step 4, interpret the output:
第 4 步,解释输出:
select * from penguins
prints:
印刷:
'1001', 'We need more power!'
'1002', 'Time to fire up'
'1003', 'kowalski\'s nuclear reactor'
回答by Bojan Hrnkas
MySQL Workbench
MySQL 工作台
If you want to avoid writing sql, you can also do it in MySQL Workbench by right clicking on the table, choose "Alter Table ..." in the menu.
如果你想避免写sql,也可以在MySQL Workbench中通过右键单击表,在菜单中选择“Alter Table ...”来完成。
When the table structure view opens, go to tab "Options" (on the lower bottom of the view), and set "Auto Increment" field to the value of the next autoincrement number.
当表结构视图打开时,转到选项卡“选项”(在视图的底部),并将“自动增量”字段设置为下一个自动增量编号的值。
Don't forget to hit "Apply" when you are done with all changes.
完成所有更改后,不要忘记点击“应用”。
PhpMyAdmin:
PhpMyAdmin:
If you are using phpMyAdmin, you can click on the table in the lefthand navigation, go to the tab "Operations" and under Table Options change the AUTO_INCREMENT value and click OK.
如果您使用的是 phpMyAdmin,您可以单击左侧导航中的表格,转到“操作”选项卡,然后在“表格选项”下更改 AUTO_INCREMENT 值,然后单击“确定”。
回答by John
First you need to add column for auto increment
首先,您需要添加用于自动增量的列
alter table users add column id int(5) NOT NULL AUTO_INCREMENT FIRST
This query for add column at first.
Now you have to reset auto increment initial value. So use this query
此查询首先添加列。现在您必须重置自动增量初始值。所以使用这个查询
alter table users AUTO_INCREMENT=1001
Now your table started with 1001
现在你的表以 1001 开头
回答by ospider
You could also set it in the create table
statement.
您也可以在create table
语句中设置它。
`CREATE TABLE(...) AUTO_INCREMENT=1000`
回答by Mostafa Fallah
Also , in PHPMyAdmin, you can select table from left side(list of tables) then do this by going there.
Operations Tab->Table Options->AUTO_INCREMENT.
Now, Set your values and then press Go under the Table Options Box.
此外,在PHPMyAdmin 中,您可以从左侧选择表格(表格列表),然后去那里执行此操作。
操作选项卡->表选项->AUTO_INCREMENT。
现在,设置您的值,然后在表选项框下按Go。
回答by Arun Kasyakar
For this you have to set AUTO_INCREMENT
value
为此,您必须设置AUTO_INCREMENT
值
ALTER TABLE tablename AUTO_INCREMENT = <INITIAL_VALUE>
Example
例子
ALTER TABLE tablename AUTO_INCREMENT = 101