旧表的数据id为雪花id,想保留旧表的数据顺序,但把id改为从1开始的自增id。通过创建新表,把旧表数据传到新表,后重命名来实现
-- 假设原表名为old_table,原排序字段为id
-- 1. 创建新表,新表自增id设置为1开始
CREATE TABLE `new_table` LIKE `old_table`;
ALTER TABLE `new_table` AUTO_INCREMENT = 1;
-- 2. 插入数据,确保按字段排序
INSERT INTO `new_table` (`operation`, `request_uri`, `request_method`, `request_params`, `request_time`, `user_agent`, `ip`, `status`, `creator_name`, `creator`, `create_date`)
SELECT `operation`, `request_uri`, `request_method`, `request_params`, `request_time`, `user_agent`, `ip`, `status`, `creator_name`, `creator`, `create_date`
FROM `old_table`
ORDER BY `id`;
-- 3. 删除原表并重命名新表为旧表
DROP TABLE `old_table`;
ALTER TABLE `new_table` RENAME TO `old_table`;