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

相关推荐
ChinaRainbowSea13 分钟前
7. LangChain4j + 记忆缓存详细说明
java·数据库·redis·后端·缓存·langchain·ai编程
小马学嵌入式~1 小时前
嵌入式 SQLite 数据库开发笔记
linux·c语言·数据库·笔记·sql·学习·sqlite
Java小白程序员2 小时前
MyBatis基础到高级实践:全方位指南(中)
数据库·mybatis
Monly212 小时前
人大金仓:merge sql error, dbType null, druid-1.2.20
数据库·sql
不宕机的小马达2 小时前
【Mysql|第一篇】Mysql的安装与卸载、Navicat工具的使用
数据库·mysql
孔丘闻言2 小时前
python调用mysql
android·python·mysql
float_六七2 小时前
数据库连接池:性能优化的秘密武器
数据库·oracle·性能优化
码界奇点2 小时前
MongoDB vs MySQLNoSQL与SQL数据库的架构差异与选型指南
数据库·sql·mongodb·系统架构
IT 小阿姨(数据库)2 小时前
PgSQL中pg_stat_user_tables 和 pg_stat_user_objects参数详解
linux·运维·数据库·sql·postgresql·oracle
倔强的石头_2 小时前
Windows系统下KingbaseES数据库保姆级安装教程(附常见问题解决)
数据库