mysql自增ID起始值修改方法
mysql自增ID起始值修改方法
在mysql中很多朋友都认为字段为AUTO_INCREMENT类型自增ID值是无法修改,其实这样理解是错误的,下面介绍mysql自增ID的起始值修改与设置方法。
通常的设置自增字段的方法:
创建表格时添加:
|
1 |
create table table1(id int auto_increment primary key,...) |
创建表格后添加:
|
1 |
alter table table1 add id int auto_increment primary key 自增字段,一定要设置为primary key. |
许多时候希望table中数据的id不要从1开始,像qq,
id从10000开始
代码如下:
创建表格时添加:
|
1 2 3 4 5 6 7 8 |
CREATE TABLE `orders` ( `order_num` int(11) NOT NULL auto_increment, `order_date` datetime NOT NULL, `cust_id` int(11) NOT NULL, PRIMARY KEY (`order_num`), KEY `fk_orders_customers` (`cust_id`), CONSTRAINT `fk_orders_customers` FOREIGN KEY (`cust_id`) REFERENCES `customers` (`cust_id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=10000 DEFAULT CHARSET=utf8; |
创建表格后添加:
|
1 |
alter table users AUTO_INCREMENT=10000; |
而且该语句也适用于修改现有表的id上,
比如大批量删除数据后,想id从654321退回123456开始
|
1 |
alter table users AUTO_INCREMENT=123456; |
浙公网安备 33010602011771号