【mysql】id主键列乱了之后,重新排序(可根据日期顺序)

一、ID中断不连续的,重新设置为连续的ID

复制代码
alter table table_name drop id;
alter table table_name add id int not null first;
alter table table_name modify column id int not null auto_increment, add primary key(id);
select * from table_name order by id;

二、根据日期顺序,重新排列ID

复制代码
alter table table_name drop id;
alter table table_name add id int not null first;

SET @row_number = 0;
UPDATE table_name
SET id = (@row_number := @row_number + 1)
ORDER BY ref_date;

alter table table_name modify column id int not null auto_increment, add primary key(id);
select * from table_name order by id;

参考文章:

mysql根据时间对数据重新更改序号_mob649e81593bda的技术博客_51CTO博客

mysql--id主键列乱了之后,重新排序_mysql 主键排序异常-CSDN博客

相关推荐
tant1an32 分钟前
MySQL面试题(一)
数据库·mysql
喜欢的名字被抢了1 小时前
MongoDB生产实践:架构设计、分片集群与运维监控
数据库·mongodb·教程
红糖奶茶1 小时前
MySQL 8.0的自增主键持久化特性
数据库·mysql·adb
Macbethad2 小时前
基于TwinCAT的半导体刻蚀设备SCADA管理程序技术方案
运维·网络·数据库
ai_coder_ai2 小时前
编写自动化脚本,在自己后端服务中使用云原生Baas服务进行设备相关操作
数据库·云原生·自动化
oradh2 小时前
Oracle DG主备架构的基本维护操作总结
数据库·oracle·架构·dg主备架构的基本维护操作总结
xieliyu.3 小时前
MySQL 六大基础约束详解:NOT NULL/DEFAULT/UNIQUE/ 主键 / 外键 / CHECK
android·数据库·sql·mysql
Anokata3 小时前
MYSQL SQL 执行系列1
android·sql·mysql
JSON_L4 小时前
MongoDB文档关系详解
数据库·mongodb
卓怡学长4 小时前
w268基于springboot + vue 物流系统
java·数据库·vue.js·spring boot·spring·intellij-idea