【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博客

相关推荐
java叶新东老师21 分钟前
PowerDesigner 画ER图并生成sql 教程
数据库·sql
Jonariguez1 小时前
Mysql InnoDB存储引擎
数据库·mysql
nbsaas-boot2 小时前
SQL Server 窗口函数全指南(函数用法与场景)
开发语言·数据库·python·sql·sql server
Y.ppm2 小时前
数分思维12:SQL技巧与分析方法
数据库·sql
森叶2 小时前
Claude Code 安装向量数据库MCP服务
数据库
bestsun9992 小时前
Time drifts can result in unexpected behavior such as time-outs.
数据库·oracle
waveee1233 小时前
学习嵌入式的第三十四天-数据结构-(2025.7.29)数据库
数据结构·数据库·学习
何传令3 小时前
SQL优化系统解析
数据库·sql·mysql
找不到、了3 小时前
Redis内存使用耗尽情况分析
数据库·redis·缓存
DarkAthena3 小时前
【GaussDB】内存资源告急:深度诊断一起“memory temporarily unavailable“故障
数据库·gaussdb