mysql添加自增id 顺序_MySQL自增主键ID重新排序

一、不清空数据

无外键约束:

1、删除原有主键

alter table '表名' drop 'id';

2、添加新主键字段并设置主键

alter table '表名' add 'id' int(8) primary key not null auto_increment first;

有外键约束

1、删除外键关系

alter table test1(外键表) drop foreign key test1_ibfk_1(外键名);

2、删除主键字段

alter table test(主键表) drop 'tid'(主键字段);

3、重新设置主键字段,以及自增初始值

alter table test add column 'tid' int(8) primary key auto_increment,auto_incremet=100;

4、重新设置外键

alter table test1 add foreign key(ttid)(需要添加的外键字段) references test(tid);

二、清空数据

set foreign_key_checks=0;   取消外键约束

truncate table 表名

set foreign_key_checks=1;设置外键约束

truncate:可以在快速清空表数据的同时还可以使自增列重新从1开始

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值