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开始

 

原文链接:https://www.cnblogs.com/xing-29391/p/12896476.html

posted @   花开如梦  阅读(753)  评论(1编辑  收藏  举报
编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示