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

相关推荐
l1t13 小时前
利用DuckDB列表一句SQL输出乘法口诀表
数据库·sql·算法·duckdb
q***996313 小时前
SQL 中 COUNT 的用法详解
数据库·sql
wind_one113 小时前
9.基础--SQL--DML-插入
数据库·sql
float_六七13 小时前
SQL中=与IS的区别:关键用法解析
java·数据库·sql
rit843249913 小时前
配置Spring框架以连接SQL Server数据库
java·数据库·spring
qq_3660862213 小时前
sql server中日期类型转字符串几种写法比较
运维·服务器·数据库
oh-pinpin13 小时前
【BurpSuite】【SQLmap】sql注入漏洞
sql·web安全·安全性测试
l***749413 小时前
Spring Boot 中使用 @Transactional 注解配置事务管理
数据库·spring boot·sql
nvd1113 小时前
Pytest 中使用 SQLAlchemy 进行异步数据库测试
数据库·oracle·pytest
2501_9411113415 小时前
Python上下文管理器(with语句)的原理与实践
jvm·数据库·python